Solved

How to execute code based upon the result of a select statement

Posted on 2014-12-08
3
129 Views
Last Modified: 2014-12-08
How do I execute a block of code, if the result of my select statement is true or false? See lines 6 through 13 below, where I have pseudo coded it.

-- I have a table named zoop with a column named Code
1    SET @CODE = RandomCode()
2    SET @COunter = 0
3     SET @ItemFound = 0
4     WHILE @Counter < 1000
5     BEGIN

6     SELECT IF zoop.Code = @CODE  THEN
7     -- Perform SQL
8     -- Perform SQL
10  ELSE
11     -- Perform SQL
12   -- Perform SQL
13    SET @CODE = RandomCode() -- Part of ELSE code bloc
 



14  SET @Counter = @Counter + 1 -- Always do this as the last line in the loop
15   END
0
Comment
Question by:brgdotnet
3 Comments
 
LVL 24

Assisted Solution

by:mankowitz
mankowitz earned 100 total points
ID: 40488077
I'm not sure exactly what you are trying to do, but you don't need the select keyword.

IF zoop.Code = @CODE  THEN
     -- Perform SQL
     -- Perform SQL
ELSE
     -- Perform SQL
   -- Perform SQL
END IF


Also, you probably don't want to do this. You rarely need looping in SQL

maybe you want

UPDATE zoop SET  xxxx=yyyyy WHERE Code=@CODE
0
 
LVL 10

Accepted Solution

by:
HuaMinChen earned 400 total points
ID: 40488091
Hi,
Try like this
	if not exists(
		select ...
	)
	begin
		select ...
	end

Open in new window

0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 40488101
IF EXISTS is so awesome!
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now