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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 If exists statement performance
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

maddyslayer
Yak Posting Veteran

57 Posts

Posted - 05/15/2013 :  15:52:14  Show Profile  Reply with Quote
Here's the if exists statement i wrote in a stored procedure, Is this efficient? Is there are a better way so there is not much of a delay? Any suggestions are appreciated.

If Exists (select * from products where product_id = @id and prod_cd in (select prod_cd from prod where prod_type = 'toys' and prod_Active = 'Y')and prod_state = 'Y')
update CUSTOMER set FLAG = 'Y',DATE = GETDATE() where product_id = @id and EMAIL_ADDRESS = @EMAIL
else
update CUSTOMER set FLAG = 'N',DATE = GETDATE() where product_id = @id and EMAIL_ADDRESS = @EMAIL

Bustaz Kool
Flowing Fount of Yak Knowledge

USA
1834 Posts

Posted - 05/15/2013 :  17:58:44  Show Profile  Reply with Quote
Have you looked at the execution plan to see where the time is being spent and what indexes are being used?

=================================================
I am not one of those who in expressing opinions confine themselves to facts. (Mark Twain)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 05/16/2013 :  00:24:08  Show Profile  Reply with Quote
i would have replaced in with join or using EXISTS

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next 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.08 seconds. Powered By: Snitz Forums 2000