Keep Original value of cell if NULL is selected MS SQL Server 2008

UPDATE  [MyDatabase].[dbo].[Device] 
SET nDeviceTypeID =(SELECT nDeviceTypeID FROM
        DeviceType WHERE sDisplayName =COALESCE (@Role,sDisplayName))
WHERE nDeviceID  IN (SELECT nDeviceID FROM [MyDatabase].[dbo].[AddressList]
WHERE sNetworkAddress LIKE '%' + @NetworkAddress2 + '%')

I have the above update statement to update a cell when parameter is selected. However when parameter is set to null the update doesn't executes which is good, but instead of sending back an error message I would prefer to keep the original value of the cell without executing any updates. Any ideas on what or where am I doing ot wrong ?

EDIT 1 The network address parameter is never null so I only have to make sure that @Role is not NULL. IF @Role is NULL I would like to keep the original value of the cell.

Answers


Try this :

IF (@Role IS NOT NULL AND @NetworkAddress2 IS NOT NULL)
BEGIN

UPDATE  [MyDatabase].[dbo].[Device] 
SET nDeviceTypeID =(SELECT nDeviceTypeID FROM
        DeviceType WHERE sDisplayName =COALESCE (@Role,sDisplayName))
WHERE nDeviceID  IN (SELECT nDeviceID FROM [MyDatabase].[dbo].[AddressList]
WHERE sNetworkAddress LIKE '%' + @NetworkAddress2 + '%')

END

Need Your Help

navigation from tableview to another view

iphone ios objective-c uitableview storyboard

I am working in a table view app using story board . I want to perform event on selection

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.