Change
Change

Reputation: 438

difference between if and if exists

I have this stored procedure, I was wondering if "if exists" will actually compare the value returned by select statement or it will just check if that condition can be executed or not, sorry for such a stupid question but I am new to SQL,

declare @m_ID_v int
set @m_ID_v = ( select ID_C from M_T where MName_C = @MName_parameter)

declare @g 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_variable = 0

my select statement

select G_L_Column from G_L_table Where M_ID_Column = @M_ID_variable

Is returning either true or false, so just wanna make sure if "if exists" will work as "if"

Upvotes: 1

Views: 1546

Answers (2)

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

EXISTS (Transact-SQL)

Specifies a subquery to test for the existence of rows.

Returns TRUE if a subquery contains any rows.

Upvotes: 2

Guffa
Guffa

Reputation: 700372

Using if exists is not working as if. It doesn't check if the returned value is true or false, it checks if there exists a value.

If your query always returns a value, the if exists will always evaluate to true.

Upvotes: 4

Related Questions