Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

exec sp_executesql syntax error

Posted on 2009-05-06
4
Medium Priority
?
534 Views
Last Modified: 2012-05-06
I am attempting to retrieve records based on security I have set in a SQL table.
I have a field named ufilter that contains the filter to be applied to the data set.
In this case, the filter is: schoolnum = '032'
I keep receiving the error 'Incorrect syntax near 'schoolnum'.
I tested the SQL that is generated to the @cmd variable and the correct records are returned.

Any guidance would be most appreciated.


ALTER proc spPrincipalTest
(
@uid varchar(30)
)
as
 
DECLARE @ufilter nvarchar(200)
DECLARE @uexpdate datetime
DECLARE @CMD nvarchar(200)
 
select @ufilter=ufilter, @uexpdate=uexpdate from UFILTERS where uid=@uid
 
Print @ufilter
 
IF ISNULL(@ufilter, 'ZZTOP') = 'ZZTOP'
	BEGIN
		select * from dvASTUCurrentShort where 1 = 2
	END
 
IF @ufilter = 'NONE'
	BEGIN
		select * from dvASTUCurrentShort order by Lastname, Firstname
	END
ELSE
	BEGIN
		set @cmd = 'select * from dvASTUCurrentShort where ' + @ufilter
		print @cmd
		exec sp_executesql @cmd, @ufilter 
	END
 
Output:
schoolnum='032'
select * from dvASTUCurrentShort where schoolnum='032'
Incorrect syntax near 'schoolnum'.
No rows affected.
(0 row(s) returned)
@RETURN_VALUE = 0
Finished running [dbo].[spPrincipalTest].

Open in new window

0
Comment
Question by:JEClark
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 total points
ID: 24318456

replace exec sp_executesql @cmd, @ufilter with EXEC(@cmd)
0
 

Author Comment

by:JEClark
ID: 24318537
Thanks for the quick response!
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24318545
You don't need to pass @ufilter into the sp_executesql command.

changed:
            exec sp_executesql @cmd, @ufilter

to

            exec sp_executesql @cmd

ALTER proc spPrincipalTest
(
@uid varchar(30)
)
as
 
DECLARE @ufilter nvarchar(200)
DECLARE @uexpdate datetime
DECLARE @CMD nvarchar(200)
 
select @ufilter=ufilter, @uexpdate=uexpdate from UFILTERS where uid=@uid
 
Print @ufilter
 
IF ISNULL(@ufilter, 'ZZTOP') = 'ZZTOP'
	BEGIN
		select * from dvASTUCurrentShort where 1 = 2
	END
 
IF @ufilter = 'NONE'
	BEGIN
		select * from dvASTUCurrentShort order by Lastname, Firstname
	END
ELSE
	BEGIN
		set @cmd = 'select * from dvASTUCurrentShort where '   @ufilter
		print @cmd
		exec sp_executesql @cmd
	END

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24318557
That WILL work, but you should be using sp_executeSQL.  The problem was that you were passing @ufilter in and didn't need to.
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

971 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