Mac Luc
Mac Luc

Reputation: 1001

Insert SQL data into row with same id?

Is there a SQL command for inserting data into a row where the id = the table id, and if it doesnt exists then insert into a new row?

Something like: ('INSERT INTO USERS IF EXISTS WHERE userID=ID')

Upvotes: 0

Views: 1670

Answers (2)

user2097234
user2097234

Reputation:

You can write something simple like this:

IF (SELECT COUNT(t.ID) FROM t WHERE t.ID=@id)=1
    UPDATE
ELSE
    INSERT

Upvotes: 1

Narayan Yerrabachu
Narayan Yerrabachu

Reputation: 1823

I understood your question like this

INSERT INTO table1(value1, value2) 
SELECT value1, value2 FROM table2where table2.userID =  Id

Upvotes: 1

Related Questions