Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have wrote a select query but which one is correct i am little but confused below the both queries

SQL
IF @IDStatus IS NOT NULL
		IF EXISTS(SELECT 0 FROM tabBlobItem AS tbi		
				WHERE tbi.IDBlobItem = @IDBlobItem
				AND tbi.UrlRandom = @UrlRandom
				AND tbi.IDStatus = @IDStatus)
			BEGIN 		          
				SET @ReturnValue = 1
			END
			ELSE
			BEGIN
				SET @ReturnValue = 0
			END 
	
	Print @ReturnValue

SQL
IF @IDStatus IS NOT NULL
BEGIN --This i have added above this i have not written
		IF EXISTS(SELECT 0 FROM tabBlobItem AS tbi		
				WHERE tbi.IDBlobItem = @IDBlobItem
				AND tbi.UrlRandom = @UrlRandom
				AND tbi.IDStatus = @IDStatus)
			BEGIN 		          
				SET @ReturnValue = 1
			END
			ELSE
			BEGIN
				SET @ReturnValue = 0
			END 
END 
	
	Print @ReturnValue


What I have tried:

I have tried both but need to know which one is correct
Posted
Updated 28-Jan-19 5:06am

The are both equally correct.

IF statements in SQL can only handle a single statement or statement block. You can handle lots of statements by enclosing them in BEGIN. . . END - which defines a single statement (block). Reference IF...ELSE (Transact-SQL) - SQL Server | Microsoft Docs[^]

You could have also use
SQL
IF @IDStatus IS NOT NULL
		IF EXISTS(SELECT 0 FROM tabBlobItem AS tbi		
				WHERE tbi.IDBlobItem = @IDBlobItem
				AND tbi.UrlRandom = @UrlRandom
				AND tbi.IDStatus = @IDStatus)
			SET @ReturnValue = 1
			ELSE
			SET @ReturnValue = 0
	
	Print @ReturnValue
In my opinion the use of BEGIN . . .END helps to clarify your intent
 
Share this answer
 
v2
Comments
OriginalGriff 28-Jan-19 11:06am    
:thumbsup:
I'd have to agree with CHill60 - they are the same, but the first version is more maintainable because it's obvious what is in- and out-side the IF.

In the same way that C, C++, or C# uses curly brackets for start and end of a block, it's safer and more maintainable to use them even if they aren't needed:
SQL
if (condition)
   BEGIN
   if (otherCondition)
      BEGIN      
      statement;
      END
   else
      BEGIN
      statement;
      END
   END
SELECT ...
It's obvious that the SELECT if not conditional.
It's also obvious if you leave them out:
SQL
if (condition)
   if (otherCondition)
      statement;
   else
      statement;
SELECT ...
But ... SQL doesn't enforce indentation, it could all be on the same line:
SQL
if (condition) BEGIN if (otherCondition) BEGIN statement; END else BEGIN statement; END END SELECT ...
That's hard to read, but pretty obvious what is going on.
But take 'em out:
SQL
if (condition) if (otherCondition) statement; else statement; SELECT ...
And work that out at your leisure!

Adding them (and indenting your code) means that when you have to change it it's a lot easier to work out what is going on.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900