0

Table:

emergency_tab (user_id, emergency_no)

and

Constraints (user_id [primary key])

When id and no are received, I'm required to insert those values to user_id and emergency_no columns in the emergency_tab. And if there exist a column with same user_id, I just wanted to update the emergency_no only, no need to insert new row.

I'm using Visual Studio 2010 and MS SQL Server 2008 inbuilt in vs10

1
  • Show the code you are working on so far Commented Sep 18, 2018 at 2:21

2 Answers 2

1

I believe this is called an "upsert", you can use the MERGE keyword, e.g.:

MERGE [devLaserViso].[dbo].[Machine] t WITH (HOLDLOCK) USING [devLaserViso].[dbo].[TempMachine] s ON (s.MachineName = t.MachineName) WHEN MATCHED THEN UPDATE SET t.MachineName = s.MachineName, t.ProgramName =s.ProgramName WHEN NOT MATCHED BY TARGET THEN INSERT (MachineName,ProgramName) VALUES (s.MachineName, s.ProgramName); 

From https://stackoverflow.com/a/50232866

Sign up to request clarification or add additional context in comments.

Comments

0

It is working

 IF (SELECT COUNT(*) FROM emergency_tab WHERE user_id='?')<>0 BEGIN --UPDATE END ELSE BEGIN --INSERT END 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.