0

I have 10 field in Database and I have 10 different button in my UI page. I am trying to update my Database field individually. When i click 1 button it will update one field and 2nd for another and so on but using only one SQL update Query. Is this possible in SQL server 2008 If Yes then kindly suggest.

Thnx

2 Answers 2

1

Try this

UPDATE yourtable set
  field1 = COALESCE(@Button1Param, field1),
  field2 = COALESCE(@Button2Param, field2),
  ...
  field10 = COALESCE(@Button10Param, field10)
WHERE Your filter clause

And when pressing the Button1 provide value only for @Button1Param parameter to the query, leaving others = DBNull.Value

So, you have not to use dynamic or multiple different queries

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

Comments

0

Only through dynamic SQL, and still then not quite trivial.

You would need code attached to each button that says which column it represents, so your query would look something like

DECLARE @SQL = NVARCHAR(MAX)
SET @SQL = 'UPDATE [db].[dbo].[table] SET ' + @__upd_button_col + ' = ''' + @__upd_button_Value + ''' WHERE ID = ''' + @__upd_ID + ''''
sp_executesql @SQL

However this is really bad design (unless specifically required by your requirements). Why don't you edit your 10 fields then have -one- commit button in your application and update all columns at once?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.