Skip to content
Advertisement

Dynamically fill an SQL table by detecting a name?

I want to dynamically fill a SQL table by detecting duplicate names. So, when I use an insert statement on this table – if the name already exists it adds an entry with the same ID.

What I mean by this, for example I’ve got this empty table:

Table Domain:

DomainID Name URL StatusID ErrorID Date

So when I use these insert statements (please ignore StatusID, ErrorID and Date):

INSERT INTO dbo.Domain (Name, URL, StatusID, ErrorID, Date) VALUES('google', 'google.com', 0, 0, null)   
        
INSERT INTO dbo.Domain (Name, URL, StatusID, ErrorID, Date) VALUES('yahoo', 'yahoo.com', 0, 0, null) 
          
INSERT INTO dbo.Domain (Name, URL, StatusID, ErrorID, Date) VALUES('google', 'google.com', 0, 0, null)

The table should look like this:

DomainID Name URL StatusID ErrorID Date
1 google https://google.com 0 0 null
2 yahoo https://yahoo.com 0 0 null
1 google https://google.com 0 0 null

Is there a way that it can detect an existing entry for ‘google’, checks the ID and inserts the same ID as the already existing entry?

Advertisement

Answer

The ID of a row has to be unique for each rows otherwise it’s not an identifier. In your table schema DomainID is reduntant: it does not add any relevant information. You can remove this field and use URL but your table schema will lack a primary key.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement