Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql server 2000, dynamic sql, error message 1

Status
Not open for further replies.

duchovnick

Programmer
Jan 4, 2007
115
IL
hi,
i send 2 parameters to my procedure: one for identification (flag), the other is a file's name.
I want my procedure to operate a "select" querry including a "where" statement based on those parameters.
my code is:
Code:
/*exec dbo.Dynamic '1','march_3_07'*/ 
alter PROCEDURE dbo.Dynamic @myparam VARCHAR(20), 
   @tab_name VARCHAR(20)
AS
BEGIN TRANSACTION

DECLARE @dynSQL VARCHAR(2000)
SET @dynSQL = 
  '
  SELECT city           
  FROM
    ['+@tab_name+'] 
    WHERE
    CASE '+@myparam+'
      
          city=''aaa'' AND
          city<>''bbb''                      
  end
  '
exec (@dynSQL)
COMMIT
and the error message says:
Server: Msg 170, Level 15, State 1, Line 8
Line 8: Incorrect syntax near 'city'.
can anyone help me to correct the "where" statement ?
Thanks.
 
Hi SQLDenis
this is the print
Code:
SELECT city           
  FROM
    [march_3_07] 
    WHERE
    CASE 1
      
          city='aaa' AND
          city<>'bbb'                      
  end
i need to run this "select" query on various dates and kind of people and i thought it would go faster sending parameters to the procedure rather then change its code.
Hopefully the attached "print" will help you to save me.
And thanks!
 
Your WHERE clause is incorrect, WHERE CASE 1????? is not the where clause SQL Server expect. Check the Procedure to be:

Code:
/*exec dbo.Dynamic '1','march_3_07'*/
ALTER PROCEDURE dbo.Dynamic
   @myparam VARCHAR(20),
   @tab_name VARCHAR(20)
AS
BEGIN TRANSACTION

DECLARE @dynSQL VARCHAR(2000)
SELECT @dynSQL =  'SELECT city
                          FROM ['+@tab_name+']
                          WHERE '+
                  CASE WHEN @myparam = 1 THEN
                       ' city=''aaa'' AND
                         city<>''bbb'''
                       ELSE '1=1' END                      
print @dynSQL
exec (@dynSQL)
COMMIT

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
The second parameter: "@myparam" recieved that value.
I ran the procedure writing:
"exec dbo.Dynamic '1','march_3_07'". '1' is for an example. it could by 'vip' or "employees" or :"managers". It is a cretiria for an employee. Lets say i want to show all managers that live in "ny" so i send "ny" as a parmeter.
 
Thank you so much Borislav for another one of your magic codes that always
W O R K !!
I wish half of mine would...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top