all groups > sql server programming > december 2005 >
You're in the

sql server programming

group:

Passing entire SELECT and WHERE clause to a stored procedure


Passing entire SELECT and WHERE clause to a stored procedure Ray
12/8/2005 9:38:02 PM
sql server programming: I have an application that allows the user to select various fields to
display and/or use as the search criteria to locate a record and I then build
the entire SQL clause in code and execute the SQL clause from code. The
records returned are actually a UNION of 4 different VIEWS.

Is it possible to pass in the SELECT and WHERE clauses as the parameters to
the stored procedure like;

SELECT @Select FROM View1 WHERE @Where
UNION
SELECT @Select FROM View2 WHERE @Where
....

I am using SQL Server 2000.

Re: Passing entire SELECT and WHERE clause to a stored procedure davidw
12/8/2005 11:49:00 PM
Pass in sql query string, and run it in the SP with

execute sp_executesql @sql


[quoted text, click to view]

Re: Passing entire SELECT and WHERE clause to a stored procedure Raymond D'Anjou
12/9/2005 8:39:29 AM
Before you implement this... read on.
http://www.sommarskog.se/
especially the chapters "The curse and blessings of dynamic SQL" and
"Dynamic search conditions".

[quoted text, click to view]

Re: Passing entire SELECT and WHERE clause to a stored procedure Ray
12/14/2005 10:05:01 PM
Thanks Raymond, the article was very helpful.

[quoted text, click to view]
Re: Passing entire SELECT and WHERE clause to a stored procedure Ray
12/14/2005 10:05:02 PM
Thanks David, this solved my problem.

[quoted text, click to view]
Re: Passing entire SELECT and WHERE clause to a stored procedure --CELKO--
12/15/2005 3:44:11 PM
Yes, you can do kludges to fake it. But you should not. You do not
know what you want to get back and you don't how you are going to get
it. Think about what you would call this thing. That means a simple
<verb><object> kind of name. Have you ever had a basic Software
Engineering course? Coupling? Cohesion?
Re: Passing entire SELECT and WHERE clause to a stored procedure Tony Rogerson
12/16/2005 9:06:30 AM
Have you ever done any comerrcial out of the lab programming?

Think - dynamic front end, not a report writer but the user having the
ability to select the columns they want and add their own filtering.

Would you refer them to using SQL or would you create a nice UI so they can
drag/drop, let me answer that one as you quite obviously haven't the
experience to understand that, but its the latter.

In order to do this in a stored procedure you would need hundreds of IF ELSE
statements and code for each possible occurrance - that would not scale, it
would also make coding more complex and less maintainable.

Get back to basics - go and get some programming experience, its not enough
just to read a book or do a course, you need experience too; and that
doesn't mean playing with the language.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


[quoted text, click to view]

AddThis Social Bookmark Button