Solved

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

Posted on 2006-11-02
3
223 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

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