0

So I want to be able to update a row in my table. I know how to do this

UPDATE Param                
SET(Param1=@Param1, Param2=@Param2, Param3=@Param3, ...)
WHERE ParamId = @ParamId

The problem is that I don't know which column will be updated and which isn't, it depends on what the user changed. And I have a lot of param to to check.

--idk if this will work, I am just trying to limit the amont of code posted
ISNULL(@Param1, Select Param1 from Param where WHERE ParamId = @ParamId) 

I was thinking about dynamic SQL, but it poses to much of a security risk. Is there a way for me to do this faster? Or is there no way around this? If you need more info please ask.

4

1 回答 1

0

You could use COALESCE/ISNULL but you don't need to select the old value as default as sub query, you can use it directly:

UPDATE Param                
SET Param1=COALESCE(@Param1,Param1), Param2=COALESCE(@Param2,Param2), ...
WHERE ParamId = @ParamId

However,on this way you cannot set a nullable column to NULL. I assume this is desired.

于 2013-11-12T21:32:10.133 回答