Solved

where statement as variable

Posted on 2002-06-07
2
166 Views
Last Modified: 2008-03-06
Dear all,
    If I now want to pass some condition from ASP to store just like:

in ASP:
  sqlquery="where clientid = 3 and status='R'"
  callStoreProcedure(SPname, sqlquery)
  pass in to Store procedure

in Store procedure:

Create  StoreProcedure1(@sqlquery varchar(30))
BEGIN
   Select *
   From Client
   @sqlquery
GO

How can I do that?
0
Comment
Question by:lemontree
2 Comments
 
LVL 39

Expert Comment

by:appari
ID: 7061347
Create  StoreProcedure1(@sqlquery varchar(30))
BEGIN
exec('Select *  From Client' +   @sqlquery)
GO
0
 
LVL 9

Accepted Solution

by:
miron earned 150 total points
ID: 7061405
another way, nice for both security and performance on database side, is to structure ASP and stored procedure this way

ASP
...
oCmd.Parameters.Append(oCmd.CreateParameter("@where_criteria", adVarChar, 8000, adParamInput));
oCmd.Parameters.Append(oCmd.CreateParameter("@WithWhere", adInteger, adParamInput));
oCmd( "@where_criteria" ) = "abcd"
oCmd( "@WithWhere" ) = 1
...


create procedure my_procedure
                       @where_criteria varchar(8000)
                      , @WithWhere int = 0 -- by default where clause is omitted
AS
BEGIN
...
IF( @WithWhere = 0 )
BEGIN
   select * from my_table
END

IF( @WithWhere = 1 )
BEGIN
   select * from myt_table where my_col = @where_criteria
END
...
END --END of procedure

GO

my two cents
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now