Solved

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

Posted on 2014-12-08
3
126 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

12 Experts available now in Live!

Get 1:1 Help Now