Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Do query if a particular field has a NULL value

Author  Topic 

PGG_CA
Starting Member

24 Posts

Posted - 2012-01-18 : 15:43:05
How do I do this?

If field1 has a null value
then do this query

It's the if statement that I'm having trouble writing.

Thanks.

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2012-01-18 : 15:47:47
How about something like the following structure:

IF NOT EXISTS (SELECT <field1> FROM <yourTable> WHERE <criteria equates to NOT null>)
BEGIN
--do this query
END
ELSE
--do something else


HTH.
Go to Top of Page

PGG_CA
Starting Member

24 Posts

Posted - 2012-01-18 : 16:01:07
Thanks ehorn, but I need to explain further what I want to do.

In this particular record, if field1 is null
then I do this
else I do that

In the condition statement, if this "select field1 from table where PK=1)" returns null then ...
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2012-01-18 : 16:04:33
I see. What is the 'this' or 'that' you need to do? We can perform certain things inline using a case statement. Such as :


SELECT CASE WHEN field1 IS NULL THEN <some scalar value> ELSE <some other scalar value> END
FROM <yourTable>


If we need to perform complex operations, we likely have to us seperate queries to perform such tasks. It really depends on the 'this' and 'that' that you require. :)

HTH.
Go to Top of Page

PGG_CA
Starting Member

24 Posts

Posted - 2012-01-18 : 16:09:13
There's a very complicated update procedure to do when the condition statement returns a NULL. I don't want to modify the UPDATE, just want to insert this condition that if this particular field for this record is NULL then perform the update; otherwise, don't do anything.
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2012-01-18 : 16:11:04
Can you simply add the NULL condition to the UPDATE statement's WHERE criteria?

Go to Top of Page

PGG_CA
Starting Member

24 Posts

Posted - 2012-01-18 : 16:48:25
I'd rather not.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-18 : 16:50:05
If it's very complicated..why don't you just do what ehorn said?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-01-18 : 17:27:36
quote:
Originally posted by PGG_CA

There's a very complicated update procedure to do when the condition statement returns a NULL. I don't want to modify the UPDATE, just want to insert this condition that if this particular field for this record is NULL then perform the update; otherwise, don't do anything.

From what you've said, it sounds like you will hit the DB multiple times when you only need to do it once. Assuming that is true, why would you want to perform unneeded DB operations and slow things down?

Perhaps, I don't understand what you are trying to achomplish. If I've not understood you correctly, please refer to these links to help clarify what you are trying to do:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Go to Top of Page
   

- Advertisement -