Solved

where statement as variable

Posted on 2002-06-07
2
175 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linking a DMV to a database id/sql text in SQL server 2008 8 46
Help with Sorting Full Text results 2 14
SQL Server stored proc 2 13
Sql Query 4 16
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

17 Experts available now in Live!

Get 1:1 Help Now