Solved

I need to add a parameter to include or exclude a "WHERE" cluase

Posted on 2006-11-02
3
254 Views
Last Modified: 2012-05-05
The code below works fine, it select the complete table I want to add another parameter.
If @Lock = 'on' then add the where cluase (WHERE t9 = 1)
else
If @Lock = '' then don't add the where cluase

Please alter the code below:

ALTER PROCEDURE [dbo].[Events]
@Order varchar(50) = 't5',
@By varchar(10) = 'Asc'
AS  
Declare @sql varchar(500)

SET @sql='SELECT * FROM tbl1 ORDER BY '+@Order+' '+@By+''
exec(@sql)
0
Comment
Question by:quest_capital
  • 2
3 Comments
 
LVL 11

Expert Comment

by:rw3admin
ID: 17862706
ALTER PROCEDURE [dbo].[Events]
@Order varchar(50) = 't5',
@By varchar(10) = 'Asc'
@lock varchar(2)='on'
AS  
Declare @sql varchar(500),
@Where Varchar(500)
Select @Where=''

If @Lock = 'on' then begin Set @WHERE=' Where t9 = 1'


SET @sql='SELECT * FROM tbl1 '+@WHERE +'ORDER BY '+@Order+' '+@By+''
exec(@sql)
0
 

Author Comment

by:quest_capital
ID: 17862826
rw3admin

I get some error:
I found one: see below

ALTER PROCEDURE [dbo].[Events]
@Order varchar(50) = 't5',
@By varchar(10) = 'Asc'<<<<<<<<<< error (,)
@lock varchar(2)='on'
AS  
Declare @sql varchar(500),
@Where Varchar(500)
Select @Where=''

If @Lock = 'on' then begin Set @WHERE=' Where t9 = 1'


SET @sql='SELECT * FROM tbl1 '+@WHERE +'ORDER BY '+@Order+' '+@By+''
exec(@sql)

Error Message
Msg 156, Level 15, State 1, Procedure Events, Line 10
Incorrect syntax near the keyword 'then'.
Msg 102, Level 15, State 1, Procedure Events, Line 14
Incorrect syntax near ')'.

0
 
LVL 11

Accepted Solution

by:
rw3admin earned 500 total points
ID: 17862860
:)
you even pointed out one of the error, I was missing a comma
here is the correct one
ALTER PROCEDURE [dbo].[Events]
@Order varchar(50) = 't5',
@By varchar(10) = 'Asc',
@lock varchar(2)='on'
AS  
Declare @sql varchar(500),
@Where Varchar(500)
Select @Where=''

If @Lock = 'on' begin Set @WHERE=' Where t9 = 1' End


SET @sql='SELECT * FROM tbl1 '+@WHERE +'ORDER BY '+@Order+' '+@By+''
exec(@sql)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

860 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