• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

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

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
2 Solutions
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
     -- Perform SQL
   -- Perform SQL

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
HuaMinChenBusiness AnalystCommented:
Try like this
	if not exists(
		select ...
		select ...

Open in new window

brgdotnetcontractorAuthor Commented:
IF EXISTS is so awesome!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now