Solved

exec sp_executesql syntax error

Posted on 2009-05-06
4
514 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 250 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

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

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

18 Experts available now in Live!

Get 1:1 Help Now