1

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"

4
  • The block in if exists will execute if your select query inside it return at least 1 record, equal to if((select query).records.count > 0) Commented Oct 15, 2012 at 9:21
  • how can I use if statement then as it gives me error and I asked a question and they asked me to use "exists" stackoverflow.com/questions/12892194/… Commented Oct 15, 2012 at 9:26
  • @Ignacio - Saw you removed the other question. Just for what it's worth, I did not down-vote your question(s). Commented Oct 15, 2012 at 10:33
  • @user569711 I was getting negative votes without explanation so I had to remove it, but thanks for letting me know :) Commented Oct 15, 2012 at 10:52

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Ignacio: If the select returns a bit, can't you simpy assign it to the variable?
I can actually but for the moment I just checking it against a value which solved the problem, ta
2

EXISTS (Transact-SQL)

Specifies a subquery to test for the existence of rows.

Returns TRUE if a subquery contains any rows.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.