Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a SQL table with Project Column value as 'Project1,Project2,Project3' I need to update this row if they select a different value like 'Project4' from the telerik dropdown list as 'Project1,Project2,Project3,Project4'

I get the value from the dropdown same as 'Project1,Project2,Project3',so I will send this as a paramter to SQL.

Suppose if they select 'Project5,Project1'...Project1 should not be added as its already there.

Can some one suggest how do I check for new and existing values and update accordingly. My simple update is not working for this scenario.Kind of struck.

Thanks

share|improve this question

1 Answer

You can create a stored procedure and use merge to insert or update as necessary like the example below

DECLARE @nameField    VarChar(50) = 'some data'

MERGE dbo.MyTable t
USING (SELECT @nameField [field]) s
    ON t.myData = s.field
WHEN MATCHED THEN
    UPDATE
    SET t.myData = @nameField
WHEN NOT MATCHED THEN
    INSERT (myData)
    VALUES (@nameField);

If you want to limit redundant updates, e.g. if updating Project Column with the exact same data and block such updates, then you'll need to create an update trigger to check and block the update.

share|improve this answer
Its not working...as they are not single values... I need to update this row if they select a different value like 'Project4' from the telerik dropdown list as 'Project1,Project2,Project3,Project4' – user1046415 Jun 7 at 17:18
@user1046415 What does your table look like, and what does the data you are sending to your db look like? From your question I understood it's one column you are checking against and if different update, if non existing insert, otherwise do nothing, is this not right? – Jason Jun 7 at 17:24
Mytable has several columns but I am only updating one value which is the ProjectName.And data into the column gets inserted as 'Project1,Project2,Project3'.So I must be able to update it as 'Project1,Project2,Project3,Project4' if they select Project4 from the dropdown list.Hope it helps.. – user1046415 Jun 7 at 17:34
@user1046415 Is Project1,Project2,Project3 what you insert into ProjectName column? What is you key, how do you identify a row in the table? – Jason Jun 7 at 17:37
I have ID as PrimaryKey...Yes we have such a scenario where employee can work for multiple projects. – user1046415 Jun 7 at 17:42
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.