开发者

Skip updating SQL row with empty values?

I have a table with several columns that allow NULLs. How would I go about writing a SQL Query that will allow me to skip that column in an update if the value is "empty". Should I use a boolean flag letting the query know to update that value? I do something similar in a SELECT like this

SELECT * FROM table WHERE (@boolFlag = 1 OR col1 = @col1Val)

But trying开发者_如何学JAVA to do that for an update has proven to be an exercise in futility. Ideally I'd be able to say skip this value if true, otherwise update over my 20ish columns. Right now I can't pass in DBNull.Value, so instead I'm being forced to insert "" which is converted into whitespace (for instance, my nvarchar(50) has 50 spaces).

I'm using a SQL Server DB/Table and a DataSet in VB.NET, constructing my queries in VS2k8 if that matters.


You could do:

update MyTable
set MyColumn = case when @MyColumnValue = '' then null else @MyColumnValue end

A short form of the above would be:

update MyTable
set MyColumn = case when @MyColumnValue <> '' then @MyColumnValue end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜