Groups | Blog | Home
all groups > sql server programming > april 2007 >

sql server programming : Return 1 of 2 Select Statements


Derek Hart
4/26/2007 5:57:55 PM
I simply want to do this in a stored procedure:

If @myValue = 1
Select Field1 From myTable
Else
If @myValue = 2
Select Field1, Field2 From myTable

In query analyzer I get the right select statement, but in dotnet or access
I get only the first select statement, unless I change it to this:

If @myValue = 1
Select Field1 From myTable
Return
Else
If @myValue = 2
Select Field1, Field2 From myTable
Return

Now adding the returns made this work, but I am not sure why. Any solid way
to do this?

Derek

Erland Sommarskog
4/27/2007 7:33:46 AM
Derek Hart (derekmhart@yahoo.com) writes:
[quoted text, click to view]

If I understand this correctly, the rows returned are the same in both
cases, only that you return more columns when @myValue = 2.

My suspicion is that the client API for some reason do not get the
differences, and always display the smaller set of columns. You could
verify this by using different WHERE clauses in the two IF branches.
If you then see different values depending on @myValue, my theory is
right.

And if not... it would help to see the actual code, and not the least the
code you have in Access or .Net.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Derek Hart
4/27/2007 7:43:08 AM
The system returns the first select statement even if @myValue=2, unless I
put in the returns. Not sure why SQL Server parses this procedure
differently with the returns. Is the 2nd way I wrote the code done
properly?

Derek

[quoted text, click to view]

Derek Hart
4/27/2007 9:11:29 AM
I did not write the begin ends here. But they are in my code. Any ideas on
how to write this better?

Derek

[quoted text, click to view]

David W
4/27/2007 11:48:59 AM
The second way shouldn't even compile without BEGIN-END after the if and
before the else.

[quoted text, click to view]

Erland Sommarskog
4/27/2007 10:23:31 PM
Derek Hart (derekmhart@yahoo.com) writes:
[quoted text, click to view]

But how do you know that it runs the first SELECT statement? Did you verify
this in Profiler? Did you check with different WHERE clauses to make
sure that you actually getting the data from the first SELECT statement,
and not just the second result set stripped of columns?

And I really encourage you to post your actual code. That could reduce
the amount of guessing we have to do.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Hugo Kornelis
4/27/2007 10:52:45 PM
[quoted text, click to view]

Hi Derek,

Your post is confusing. The second code snippet you posted is not even
valid in eitehr .Net or in Access. It would be almost (but not
completely) valid in T-SQL.

I suggest that you post the actual .Net and/or Access code that you
execute. That might help us find the problem.

--
Hugo Kornelis, SQL Server MVP
AddThis Social Bookmark Button