Reputation: 438
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.
Upvotes: 2
Views: 716
Reputation: 5588
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
Upvotes: 1
Reputation: 51504
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
Upvotes: 1
Reputation: 28695
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
Upvotes: 2