Solved

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

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

739 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