?
Solved

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

Posted on 2014-12-08
3
Medium Priority
?
139 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 24

Assisted Solution

by:mankowitz
mankowitz earned 400 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 11

Accepted Solution

by:
HuaMinChen earned 1600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

765 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