SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Using the IF statement on a table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Xercister
Starting Member

USA
6 Posts

Posted - 03/28/2013 :  17:03:18  Show Profile  Reply with Quote
Is it possible to use an IF statement to check a value in a row of a table?

My whole idea is to have a query that checks for a certain value and if that value exists then do say an update statement. If it doesn't find that value then it will need to do something else.


IF row = xxxvaluexx
THEN do whatever
ELSE
do whatever

Is that possible?

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 03/28/2013 :  17:13:33  Show Profile  Reply with Quote
IF EXISTS (select * from yourTable where somefield = 'somevalue')

Do whatever

ELSE
do something else


Jim
Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3593 Posts

Posted - 03/28/2013 :  17:14:42  Show Profile  Reply with Quote
You would use CASE expressions rather than IF statements. IF construct in SQL is for control flow; while you can use IF in sort of a roundabout way for doing what you are attempting to do, CASE expressions are probably better suited. See examples below:
update tablename set
	yourColumnA = case when YourColumnB = 'X' then 'Updated' end;
	
	
update tablename set
	yourColumnA = case when YourColumnB = 'X' then 'Updated' ELSE 'Not Updated' end;
Go to Top of Page

Xercister
Starting Member

USA
6 Posts

Posted - 03/28/2013 :  20:05:46  Show Profile  Reply with Quote
Ah, thanks for both replies guys. I'll look into both!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000