473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Table and Column request as variables error.

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

Jul 20 '05 #1
2 1680

"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
Jul 20 '05 #2
Chris Auer (ch********@gmail.com) writes:
I am trying to run a query where my Table and my request are variables
(Will be used in Stored Procedure)


So what is the point with using a stored procedure then? If you are
passing tables as variables, you have a problem with your table design.

A longer discussion on this is on http://www.sommarskog.se/dynamic_sql.html.
There you can also learn to use sp_executesql, so you can avoid the
syntax-error problem you ran into.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
3
by: Rebecca Lovelace | last post by:
For some reason in Enterprise Manager for SQL Server 2000, I cannot put the following line into a trigger: select * into #deleted from deleted When I hit the Apply button I get the following...
1
by: Timothy Perrigo | last post by:
(PostgreSQL 8.0 beta 4 on Mac OS X 10.3.6) I'm working on a function which creates and populates a temporary table, then returns the number of records it has inserted. I'm getting an error,...
4
by: Mike | last post by:
We are using .NET 2.0 and intermittently egt the following errors on almost all our web pages. The error is not repoducable and cycling the worker process seems to temporarily fix the problems. ...
4
by: hmiller | last post by:
Hey there folks, I was wondering if there was a way to store a list of variables in a table and then call them one at a time in some loop method. Here's what I've got: A table "Tab Names"...
28
by: jverri01 | last post by:
First, I am relatively new to working with variables. Most of my experience has been with interface design. i am using ACCESS ver. 2003, running in Windows XP. Second, I spent an hour searching...
4
by: prasath03 | last post by:
Dear All, Can anybody help me about my coding, i'm inserting the image file to database using jsp. When i execute the code, the following error is occurred: java.sql.SQLException: Insert...
1
Merlin1857
by: Merlin1857 | last post by:
How to search multiple fields using ASP A major issue for me when I first started writing in VB Script was constructing the ability to search a table using multiple field input from a form and...
2
by: jarea | last post by:
I have read quite a bit about this error but I have yet to find the solution to my problem. I am trying to execute the following mysql statement: alter table line_items add...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.