"Chris Auer" <ch********@gmail.com> wrote in message
news:ci********@odah37.prod.google.com...
I am trying to run a query where my Table and my request are variables
(Will be used in Stored Procedure)
Declare @Email VARCHAR(100)
Declare @Table VARCHAR(50)
Declare @Count VARCHAR(8)
DECLARE @cmd VARCHAR(500)
set @Table = 'tblManager'
set @Email = 'c****@tampabay.rr.com'
Set @cmd = 'Select count(*) as Count from ' + @Table + ' WHERE Email
= ' + @Email
exec(@cmd)
The error I get is
Server: Msg 207, Level 16, State 3, Line 7
Invalid column name 'c****@tampabay.rr.com'.
I have worked on this for a while, any help would be greatly
appreciated.
Chris Auer
You need to put quotes around the @Email value - without quotes it's treated
as a column name:
Set @cmd = 'Select count(*) as Count from ' + @Table + ' WHERE Email =
''' + @Email + ''''
select * from tblManager where Email =
ca***@tampabay.rr.com -- doesn't work
select * from tblManager where Email = 'c****@tampabay.rr.com' -- does work
If you use dynamic SQL, it's often useful to add a @Debug parameter to your
procedure which displays dynamic SQL strings before executing them. That
way, if something isn't working as expected, you can execute the procedure
with the @Debug parameter set and see what the complete SQL string looks
like after the variables have been substituted.
Simon