SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Avoiding Dynamic Queries


Avoiding Dynamic Queries

Author
Message
Mohan Kumar VS
Mohan Kumar VS
Mr or Mrs. 500
Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)

Group: General Forum Members
Points: 502 Visits: 344
We have an Web application using sql server. In most of the sps, generally we have a parameter @strcriteria for the criteria part (where clause) of the query. The criteria will have value like 'where @clientId = 1 and ProgramId = 90 and projectid = 66;' or it can be empty also. Due to this criteria part most of the queries are made into dynamic queries. Is there any option/method to avoid dynamic query in this case?

Eg.
CREATE PROCEDURE dbo.usp_Rpt1 @strCriteria VARCHAR(500)
AS
BEGIN

SET NOCOUNT ON

DECLARE @strQuery VARCHAR(8000)

SET @strQuery = ' SELECT' +
' c_Client.ClientID,' +
' ClientName FROM Client '
ISNULL(@strCriteria,'')

EXEC (@strQuery)

SET NOCOUNT Off
END


Rgds
Mohan Kumar
Christopher Stobbs
Christopher Stobbs
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5276 Visits: 2233
Here is an article that Gail touches on about this topic.

http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley

w00t
Posting Best Practices
Numbers / Tally Tables

SQL-4-Life
Mohan Kumar VS
Mohan Kumar VS
Mr or Mrs. 500
Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)Mr or Mrs. 500 (502 reputation)

Group: General Forum Members
Points: 502 Visits: 344
Thanks.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search