Solved

NULL Values for Parameters in the WHERE clause of an UPDATE query

Posted on 2010-09-17
8
506 Views
Last Modified: 2012-05-10
I have an update statement along these lines

UPDATE myTable Set Field1 = @Field1
WHERE ProductType = @ProductType AND Location = @Location AND Status = @Status

I originally created this command using the code in Example 1 Below, but it failed if the parameter value for either @Location or @Status was NULL.

I worked around this by creating the Code in Example 2, which builds the WHERE clause based on whether the data value is NULL. However if I'm updating multiple rows at a time, building the Command each time like this feels like it would be inefficient.

So, it this the correct approach or is there a better way?


Example 1 - Fails

Dim intRowsAffected as Integer
Using cmd As New SqlCommand("", SQLConn)
 cmd.commandtext = "UPDATE myTable Set Field1 = @Field1 
WHERE ProductType = @ProductType AND Location = @Location AND Status = @Status"
  SQLConn.Open()
  For each rw as datarow in changes.rows     cmd.parameters.addwithvalue("@Field1",rw,item("Field1"))
cmd.parameters.addwithvalue("@ProductType",rw,item("ProductType", DataRowVersion.Original)))
cmd.parameters.addwithvalue("@Location",rw,item("Location", DataRowVersion.Original)))
cmd.parameters.addwithvalue("@Status",rw,item("Status", DataRowVersion.Original)))
 intRowsAffected = cmd.ExecuteNonQuery
 cmd.Parameters.Clear()
Next
End using

Example 2 - Works

Dim intRowsAffected as Integer
Using cmd As New SqlCommand("", SQLConn)
  SQLConn.Open()
  For each rw as datarow in changes.rows
    cmd.commandtext = "UPDATE myTable Set Field1 = @Field1 
WHERE ProductType = @ProductType"
 If isdbnull(rw,item("Location", DataRowVersion.Original)) then
 cmd.commandtext &= " AND Location IS NULL "
 else
  cmd.commandtext &= " AND Location = @Location "
end if
 If isdbnull(rw,item("Status", DataRowVersion.Original)) then
 cmd.commandtext &= " AND Status IS NULL "
 else
  cmd.commandtext &= " AND Status = @Status "
end if
   cmd.parameters.addwithvalue("@Field1",rw,item("Field1"))
cmd.parameters.addwithvalue("@ProductType",rw,item("ProductType", DataRowVersion.Original)))
cmd.parameters.addwithvalue("@Location",rw,item("Location", DataRowVersion.Original)))
cmd.parameters.addwithvalue("@Status",rw,item("Status", DataRowVersion.Original)))
 intRowsAffected = cmd.ExecuteNonQuery
 cmd.Parameters.Clear()
Next
End using

Open in new window

0
Comment
Question by:WestcountryBusiness
  • 4
  • 2
  • 2
8 Comments
 
LVL 13

Assisted Solution

by:Philip Pinnell
Philip Pinnell earned 125 total points
ID: 33699675
tryUPDATE myTable Set Field1 = @Field1 WHERE( ProductType = @ProductType OR @ProductType  IS NULL)AND (Location = @Location OR @Location IS NULL)AND (Status = @Status OR @Status IS NULL)
0
 
LVL 4

Assisted Solution

by:antonybrahin
antonybrahin earned 125 total points
ID: 33699682
Instead u can make a stored proc to do the updation and pass empty string or null  to the stored proc.
0
 
LVL 4

Expert Comment

by:antonybrahin
ID: 33699685
what is the actual error message u r getting?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 1

Author Comment

by:WestcountryBusiness
ID: 33699788
Thanks for your feedback.

Andy - If I use the OR option, then an update where (for example) @Location = 'MyBay', would also update any other rows where ProductType & Status match but Location is NULL - and I wouldn't want to update them. I'd only want to update where Location = 'MyBay'

Antony - As stored procedure is an option, but I'd still have to do the test within the stored procedure, wouldn't I?  Is than any more efficient than doing them in the program?

Also I don't get an error.  I just get zero matching records found to update.

I just thought there must be a way because if I were to create a tableadapter & allow it to use Optimistic Concurrency then the Update Command that is automatically created tests for a match on all the original values of all fields retrieved, many of which will have NULL values.  So if that works, why can't I reproduce the functionality myself?
0
 
LVL 1

Accepted Solution

by:
WestcountryBusiness earned 0 total points
ID: 34018708
Problem sorted by creating a function to create the Update Command with all parameters which I call within a loop.  So for each row to be updated I create the Update command, passing the datarow to the function which creates the command needed, then perform the Update.

I'm still not convinced there isn't a better way, but it works & I've moved on.  Thanks for the help guys - just going to share points.
0
 
LVL 1

Author Comment

by:WestcountryBusiness
ID: 34018722
?
0
 
LVL 1

Author Closing Comment

by:WestcountryBusiness
ID: 34049728
My comment accepted as solution as it was the action taken to solve problem I was facing
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 34050220
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question