troubleshooting Question

How to use IF keyword in SQL 2005

Avatar of TheGorby
TheGorbyFlag for United States of America asked on
Microsoft SQL ServerProgrammingMicrosoft SQL Server 2005
10 Comments3 Solutions321 ViewsLast Modified:
I'm fairly new to SQL, but being forced to learn it due to our database guy being laid off. Some of the differences in language between SQL and others (VB, Batch, AutoIT, etc.) are proving to be too different for me to easily figure out on my own.

Specifically, how can I use an IF...ELSE keyword? No matter what I do, the SELECT statement in my expression produces multiple results which causes it to fail. See my current working code below, without the use of IF...ELSE:
UPDATE #tempmain
SET PU_Confirmation = 'Y'
FROM stops s, #tempmain t
WHERE s.ord_hdrnumber = t.OrderNumber
	AND s.stp_type = 'PUP'
	AND s.stp_status = 'DNE'

UPDATE #tempmain
SET PU_Confirmation = 'N'
FROM stops s, #tempmain t
WHERE s.ord_hdrnumber = t.OrderNumber
	AND s.stp_type = 'PUP'
	AND s.stp_status <> 'DNE'
As you can see, they're almost identical. If the stp_status is 'DNE', set the column to 'Y'. If the stp_status is not 'DNE', set the column to 'N'. For some reason I can't get it to work without using two separate statements like this.

I suspect this is easy for those that have worked with SQL more, but if not let me know and I'll bump up the point value for this question.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 3 Answers and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros