Multiple conditions under IF in transact SQL - tsql

I am trying to use 'and' in an If condition as follows:
Begin
If condition1 and condition2
then begin result1
end else begin result2;
End;
end;
what is wrong in the statement. Could anybody help in thei matter? Thanks!

Remove your THEN. Example
DECLARE #variable INT = 0
DECLARE #variable2 INT = 10
IF #variable > 0 AND #variable2 > 0
BEGIN
SELECT 'Condition was met'
END
ELSE
BEGIN
SELECT 'Condition was not met'
END
If you want to chain IFs:
DECLARE #variable INT = 0
DECLARE #variable2 INT = 10
IF #variable > 0 AND #variable2 > 0
BEGIN
SELECT 'Condition 1 was met'
END
ELSE IF #variable = 0 AND #variable2 > 0
BEGIN
SELECT 'Condition 2 was met'
END
ELSE
BEGIN
SELECT 'No condition was met'
END
Also, you can avoid the BEGIN ... END if you just need one statement, but it can be tricky to read if expressions are long.
DECLARE #variable INT = 0
DECLARE #variable2 INT = 10
IF #variable > 0 AND #variable2 > 0
SELECT 'Condition was met'
ELSE
SELECT 'Condition was not met'

As a wild guess, lose the 'then' (SFrejofsky's comment), and lose the first ; which is inside of a begin..end block.
Also if there is only executable line in a block then begin..end is not needed, so your code should look like this
If condition1 and condition2
result1
else
result2

Begin
If condition1 and condition2
result1
else
result2
END

Related

Recursive SQL function, <= operator doesn't work

I just wonder why this works. But what I have comment doesn't work.
ALTER PROC spFactorial
#ValueIn int,
#ValueOut int OUTPUT
AS
DECLARE #InWorking int;
DECLARE #OutWorking int;
IF #ValueIn <> 1 -- this <= doesn't work??
BEGIN
SELECT #InWorking = #ValueIn - 1;
EXEC spFactorial #InWorking, #OutWorking OUTPUT;
SELECT #ValueOut = #ValueIn * #OutWorking;
END
ELSE
BEGIN
SELECT #ValueOut = 1;
END
RETURN;
GO
DECLARE #WorkingOut int;
DECLARE #WorkingIn int;
SELECT #WorkingIn = 3;
EXEC spFactorial #WorkingIn, #WorkingOut OUTPUT;
PRINT CAST(#WorkingIn AS varchar ) + ' factorial is '
+ CAST(#WorkingOut AS VARCHAR );
This IF #ValueIn <= 1 doesn't work - why?
In C# or java it works.
Thank you
<= is not an operator in SQL. In c# and Java it is.
For <= operator functionality in Sql, you can do this
IF(#value < 1 OR #value = 1)

Few errormessager in one raiserrror

I have following problem, I need to display few error messages from my if statements. If, for example, 2 of 3 conditions are met I need to raisevent display 2 messages.
IF #var1 > 1
BEGIN
SET #err = 'ASD'
END
IF #var1 <4
BEGIN
SET #err = 'ZXC'
END
IF #var = 3
BEGIN
SET #err = 'QWE'
END
For #var1 = 2
I need to display 2 errormessages in one raiserror. How can I do this?
Is your code inside BEGIN CACTH block?
How about raising error like this with severity less than 11 that:
IF #var1 > 1
BEGIN
SET #err = 'ASD';
RAISEERROR(#err, <your_severity>, <state>)
END
IF #var1 <4
BEGIN
SET #err = 'ZXC'
RAISEERROR(#err, <your_severity>, <state>)
END
IF #var = 3
BEGIN
SET #err = 'QWE'
RAISEERROR(#err, <your_severity>, <state>)
END
Or combining message_erorr:
IF #var1 > 1
BEGIN
SET #err += ' ASD';
END
IF #var1 <4
BEGIN
SET #err += ' ZXC'
END
IF #var = 3
BEGIN
SET #err += ' QWE'
END
RAISEERROR(#err, <your_severity>, <state>)

DECLARE statement make variables to be global? [duplicate]

I would like to declare a variable within an if/else statement in a SQL Server stored procedure. I understand that this is fairly impossible because SQL Server doesn't do memory management with respect to declaration of variables within procedures. Is there a way to have a variable scoped in an if/else statement, then redeclare a variable with the same name in another if/else statement? For example:
create procedure Foo
as
begin
if exists (x)
begin
declare #bob int
set bob = 1
end
else
begin
declare #bob int
set bob = 2
end
end
From books online:
The scope of a variable is the range of Transact-SQL statements that can reference the variable. The scope of a variable lasts from the point it is declared until the end of the batch or stored procedure in which it is declared.
However. Nothing keeps you from doing this:
create procedure Foo as begin
declare #bob int
if exists (x)
begin
set #bob = 1
end
else
begin
set #bob = 2
end
end
No, SQL is pretty funny/weird like that
Declare the variable before the if exists block of code
so
declare #bob int
set #bob = 2
if exists(x)
begin
set #bob = 1
end
Now, take a look at these examples and try to guess what happens
WHILE 1 = 2 --not true of course
BEGIN
DECLARE #VAR INT;
END
SET #VAR = 1;
SELECT #VAR;
This of course works, but it is not initialized every time
DECLARE #loop INT
SET #loop = 0
WHILE #loop <=6
BEGIN
DECLARE #VAR INT
SET #VAR = COALESCE(#VAR,0) + 1
SET #loop = #loop +1
END
SELECT #VAR
is there some reason why you can't do :
declare #bob int
if exists(x)
begin set #bob = 1 end
else
begin set #bob = 2 end
You could resort to using dynamic SQL:
if exists (x)
begin
exec sp_executesql N'
declare #bob int
set #bob = 1
';
end
else
begin
exec sp_executesql N'
declare #bob int
set #bob = 2
';
end

Removing leading zeros from column in sql

how to remove leading zero from this
number RESULT WOULD BE LIKE THIS
00000.9 .9
A0001.1 A1.1
G0101.3 G101.3
00808.8 808.8
J0000.5 J.5
declare #input varchar(10);
declare #output varchar(10);
set #input = '00000.9';
while ((ISNUMERIC(substring(#input,1,1)) = 0) or (substring(#input,1,1) = '0'))
begin
if substring(#input,1,1) = '0'
begin
set #input = substring(#input,2,len(#input) )
end
else
if ISNUMERIC(substring(#input,1,1)) = 0
begin
set #output = substring(#input,1,1);
set #input = substring(#input,2,len(#input))
end
end
if LEN(#output) > 0
set #input = #output + #input
select #input
#input is your input
ALTER FUNCTION NUMBER(#NUMBER VARCHAR(7))
RETURNS VARCHAR(7)
AS
BEGIN
DECLARE #NUM VARCHAR(1)
DECLARE #NUM1 VARCHAR(7)
SET #NUM= SUBSTRING(#NUMBER,1,1)
IF(#NUM LIKE '[A-Z%]')
BEGIN
SET #NUM1=#NUM+''+CAST(CONVERT(FLOAT,SUBSTRING(#NUMBER,2,7),2)AS VARCHAR(7))
END
ELSE
BEGIN
SET #NUM1=LTRIM(STR(cast(#NUMBER as float),case when len(cast(#NUMBER as float)) > 7 then 7 else len(cast(#NUMBER as float)) end,1))
END
RETURN #NUM
END

T-SQL: if exists always return true?

What do you think , does the Stored Procedure always return 1 ?
I am concerned about the if exists(..)
BEGIN
DECLARE #IsUserExisting bit
SET NOCOUNT ON
IF Exists
(
Select null FROM G_User WHERE
SamAccountName = #SamAccountName
AND NetBIOSDomainName = #NetBIOSDomainName
)
BEGIN
SET #IsUserExisting = 1
END
ELSE
BEGIN
SET #IsUserExisting = 0
END
Select #IsUserExisting
END
No, if the WHERE clause doesn't return anything IF Exists() returns false and consequently #IsUserExisting is set to 0.
Makis already answered your question, but i would like to suggest the following
You could simplify your code with:
declare #IsUserExisting bit;
set #IsUserExisting = (
select count(*) from G_User
where SamAccountName = #SamAccountName and
NetBIOSDomainName = #NetBIOSDomainName);
select #IsUserExisting;
I think the following is even shorter in your case.
select count(*) from G_User
where SamAccountName = #SamAccountName and
NetBIOSDomainName = #NetBIOSDomainName)
BEGIN
DECLARE #IsUserExisting bit
SET NOCOUNT ON
IF Exists
(
Select null FROM G_User WHERE
SamAccountName = #SamAccountName
AND NetBIOSDomainName = #NetBIOSDomainName
)
BEGIN
SET #IsUserExisting = 1
END
ELSE
BEGIN
SET #IsUserExisting = 0
END
Select #IsUserExisting
END
i tried using this but when true it sets #IsUserExisting = -1 !!
You should not use Select Count() if you are only testing to see if something exists. While it should return fairly quickly in this case, If EXISTS() will return true immediately when it finds a matching record. Select Count() will look at all records in order to give you a complete count, thus adding unnecessary overhead.