Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

914 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

22 Experts available now in Live!

Get 1:1 Help Now