getting error for if/else in a stored procedure - sql

I am relatively very new to SQL queries but I have this stored procedure where I am trying to get value of a declared variable as mentioned below but getting error,
First line is 20 here,
declare #m_ID_v int
set #m_ID_v = ( select ID_C from M_T where MName_C = #MName_parameter)
declare #g bit
if (select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable)
set #g_v = 1
else
set #g_variable = 0
Exception I get:
Msg 4145, Level 15, State 1, Procedure GetID, Line 20
An expression of non-boolean type specified in a context where a
condition is expected, near 'set'. Msg 156, Level 15, State 1,
Procedure GetID, Line 21 Incorrect syntax near the keyword 'else'.
Now if I remove declare #g... and try to parse it, no error occurs
EDIT
I want my code to check for returned value by my select statement so "if exists" is not really what am looking for, sorry.

try use if exists:
declare #g_v bit
if exists(select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable)
set #g_v = 1
else
set #g_v = 0

declare #m_ID_v int
set #m_ID_v = ( select ID_C from M_T where MName_C = #MName_parameter)
declare #g bit
if ((select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable) = value )
set #g_v = 1
else
set #g_variable = 0

You can't say
if (select ...
You have to compare something with something else in a if statement, or use a boolean function such as exists

Related

Simple ISEMPTY() function in SQL Server throws a non-boolean type error

I'm trying to write a simple ISEMPTY function in Microsoft SQL Server:
DROP FUNCTION IF EXISTS ISEMPTY;
GO
CREATE FUNCTION ISEMPTY
(#charsequence nvarchar(max))
RETURNS BIT
AS
BEGIN
DECLARE #result BIT;
IF (#charsequence IS NULL OR LEN(#charsequence) = 0)
SET #result = 1
ELSE
SET #result = 0;
RETURN #result;
END
GO
When I want to test it with:
SELECT CASE WHEN dbo.ISEMPTY('') THEN 'REACHED!' END;
I get the following error:
[S0001][4145] Line 1: An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
What goes wrong here?
A boolean is expected after when in the case expression, but your function returns a bit.
Try this instead:
SELECT CASE WHEN dbo.ISEMPTY('') = 1 THEN 'REACHED!' END;
The function returns a number so you need a comparison with a number to get a boolean value
CREATE FUNCTION ISEMPTY( #charsequence nvarchar(max))
returns BIT AS
begin
DECLARE #result BIT;
IF (#charsequence IS NULL OR LEN(#charsequence) = 0 )
SET #result = 1;
ELSE
SET #result = 0;
RETURN #result;
end
GO
SELECT CASE WHEN dbo.ISEMPTY('') = 1 THEN 'REACHED!' END;
GO
| (No column name) |
| :--------------- |
| REACHED! |
db<>fiddle here

sybase script don't understand trouble

I 'm not a regular of Sybase ASE.
Tring to make a script to add line into a tempory table.
sybase version 12.5.4
BEGIN
declare and init variable
--boucle
select * into #parTemp from Parution where 1=2
WHILE #dateCourante <= #dateFin
BEGIN
select #dpart1=datepart(mm,#dateCourante)
select #dpart2=datepart(dd,#dateCourante)
select #dpart3=datepart(dw,#dateCourante)
--si on est pas le 1er mai
if #dpart1 <> 5 AND #dpart2 <> 1
begin
--id parution
select #idPar = #idPar + 1
select #idParChaine = convert(varchar(10),#idPar)
--num parution
select #numPar = #numPar + 1
select #numParChaine = convert(varchar(20),#numPar)
--prix du jour courant
if datepart(dw,#dateCourante)=6
select #prixCourant = #prixVen
else
if datepart(dw,#dateCourante)=1
select #prixCourant = #prixDim
else
select #prixCourant = #prix
end
end
insert into #parTemp values (#idParChaine,#dateCourante,#numParChaine,#nbPagination,#prixCourant,#poids,#parCompt,#parOJD,#resVal)
end
select #dateCourante = dateadd(dd,1,#dateCourante)
END
END
Errors i get :
Error code 156, SQL state ZZZZZ: Incorrect syntax near the keyword 'WHILE'.
Error code 156, SQL state ZZZZZ: Incorrect syntax near the keyword 'end.
Error code 156, SQL state ZZZZZ: Incorrect syntax near the keyword 'End'.
i really don't understand, thank for help.
Pierre

Rerun Error Message While Creating a Function

i am fairly new to SQL programming, and I am currently learning to create FUNCTIONS.
The problem that I am having, is creating the following FUNCTION.
create function CreatePI
(
)
returns decimal(10,6)
with returns null on null input
as
begin
declare #P as decimal(10,6)
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15)
return #P
end
go
The above function is supposed to replicate the number PI. But the problem that I am having is:
Msg 156, Level 15, State 1, Procedure CreatePI, Line 11
Incorrect syntax near the keyword 'return'.
If anyone could help me out with why I am getting this problem, it would be greatly appriciatead.
You are missing a closing paren on the set line:
set #P = 4*(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+(1/13)-(1/15))
----------------------------------------------------------^

Incorrect syntax near the keyword 'TOP'. SQL Server

I have a function whose return value I am assigning it to a variable and I am getting an error
Incorrect syntax near the keyword 'TOP'. SQL Server
if #Miracle is null OR #Miracle =''
select #Miracle = TOP(1) M.MiracleName
FROM Miracle M where M.MiracelID = #MiracelID
how can I assign functions like TOP to a variable ??
I think you just need to move TOP 1 before your variable:
select TOP 1 #Miracle = M.MiracleName
...
Your aren't assigning TOP to a variable, but rather using TOP to tell SQL Server to only return a single row.
Good luck.
It is simple as this.
if #Miracle is null OR #Miracle =''
select TOP 1 #Miracle = M.MiracleName
FROM Miracle M where M.MiracelID = #MiracelID

Calling a custom SQL function

This feels like a basic question, but I have tried everything! I'm writing a custom function SQL function to convert integers into dates:
CREATE FUNCTION convert_to_date (#fin INT)
RETURNS DATE
AS
BEGIN
DECLARE #fout DATE
SET #fout = CASE WHEN #fin IN ('','99999999','0','1') THEN NULL
ELSE CONVERT(DATE,CAST(#fin AS CHAR(8)))
END
RETURN #fout
END
SELECT dbo.convert_to_date(DtSurgDischarge) AS DischargeDate
FROM [TR_MASTER.registry].[dbo].[mgh_tumor]
I get the following error message:
Msg 156, Level 15, State 1, Procedure convert_to_date, Line 16
Incorrect syntax near the keyword 'SELECT'.
What am I doing wrong? Feels like it must be obvious. Thanks everyone!
At the very least you need a GO after the END statement of the function, if you're attempting to execute both the function create and the SELECT in one script - assuming you're using SSMS to run the script, that is.
CREATE FUNCTION convert_to_date (#fin INT)
RETURNS DATE
AS
BEGIN
DECLARE #fout DATE
SET #fout = CASE WHEN #fin IN ('','99999999','0','1') THEN NULL
ELSE CONVERT(DATE,CAST(#fin AS CHAR(8)))
END
RETURN #fout
END
GO
SELECT dbo.convert_to_date(DtSurgDischarge) AS DischargeDate
FROM [TR_MASTER.registry].[dbo].[mgh_tumor]