473,786 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1692

"Chris Auer" <ch********@gma il.com> wrote in message
news:ci******** @odah37.prod.go ogle.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********@gma il.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****@sommarsk og.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
8750
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware tblSoftware ------------------- ------------------ ---------------------- PID PName PID* SID* SID SWName --- ----- --- --- --- ------ 1 Thomas 1 1 ...
3
13277
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 error: Cannot use text, ntext, or image columns in the 'inserted' or 'deleted' tables This seems like a weird error, since I am not actually doing anything to the inserted or deleted tables, I am just trying to make a temp copy.
1
6588
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, though, after successive invocations of the function (I can call it once successfully, but on the next call I get an error). I've been able to reproduce the error with the following sample function: create or replace function test() returns...
4
15293
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. Has anyone seen this or have any suggestions? Event code: 3005 Event message: An unhandled exception has occurred. Event time: 9/5/2006 4:23:47 PM Event time (UTC): 9/5/2006 11:23:47 PM Event ID: cb408727296440278fb486579b359b15
4
4652
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" with 50 values all running down the first column named "Tabs"
28
3272
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 through articles and posts to make sure the information I am seeking is not already posted on the site. I am beating my head against the wall trying to figure out something that seems like it should be very simple. The trouble is I do not fully...
4
16974
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 Error: Column name or number of supplied values does not match table definition. I'm using MS-SQL Server. My code is:
1
7552
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 having the sql statement dynamically built according to the input provided by the user. I have used the method described here hundreds of times it is quick and adaptive. I generally use a frames page for the search, in this way the search is maintained...
2
43962
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 constraint fk_line_item_products foreign key (product_id) references products(id) I have also tried the following statement with the same result:
0
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.