Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Table as a variable

 
   Database Forums (Home) -> Client RSS
Next:  correct syntax?  
Author Message
Timothy Peer

External


Since: Jun 16, 2005
Posts: 2



(Msg. 1) Posted: Thu Jun 16, 2005 4:02 pm
Post subject: Table as a variable
Archived from groups: microsoft>public>sqlserver>clients (more info?)

I am reading a list of tables from a table and wish to execute a SQL
statement for each table from this list. I tried declaring @table_name,
depositing the table name from my table-list data. However, the "Select *
from @table_name" SQL fails. Any ideas?

 >> Stay informed about: Table as a variable 
Back to top
Login to vote
Andrew J. Kelly

External


Since: Sep 01, 2003
Posts: 975



(Msg. 2) Posted: Thu Jun 16, 2005 6:29 pm
Post subject: Re: Table as a variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Have a look at this:
<a rel="nofollow" style='text-decoration: none;' href="http://www.sommarskog.se/dynamic_sql.html" target="_blank">http://www.sommarskog.se/dynamic_sql.html</a>


--
Andrew J. Kelly SQL MVP


"Timothy Peer" wrote in message

 >I am reading a list of tables from a table and wish to execute a SQL

 > depositing the table name from my table-list data. However, the "Select *

 >> Stay informed about: Table as a variable 
Back to top
Login to vote
Asit Kaushik

External


Since: Dec 08, 2008
Posts: 1



(Msg. 3) Posted: Mon Dec 08, 2008 9:04 pm
Post subject: RE: Table as a variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi,
I think there is problrm with the query it should be

declare @query varchar(2000)

sert @query='select * from ' +@@table_name

exec (@query)

This should work



"Timothy Peer" wrote:

> I am reading a list of tables from a table and wish to execute a SQL
> statement for each table from this list. I tried declaring @table_name,
> depositing the table name from my table-list data. However, the "Select *
> from @table_name" SQL fails. Any ideas?
 >> Stay informed about: Table as a variable 
Back to top
Login to vote
Lawrence Garvin

External


Since: Dec 15, 2005
Posts: 14



(Msg. 4) Posted: Tue Dec 09, 2008 10:46 am
Post subject: Re: Table as a variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Asit Kaushik" wrote in message


> "Timothy Peer" wrote:
>
>> I am reading a list of tables from a table and wish to execute a SQL
>> statement for each table from this list. I tried declaring @table_name,
>> depositing the table name from my table-list data. However, the "Select *
>> from @table_name" SQL fails. Any ideas?

> Hi,
> I think there is problrm with the query it should be
>
> declare @query varchar(2000)
>
> sert @query='select * from ' +@@table_name
>
> exec (@query)
>
> This should work


This, also, will not work. You cannot use a variable in the FROM clause of a
query under any conditions.

The above concept is valid; however, the execution is incorrect.

Given a preexisting variable @table_name containing a valid two-part table
name.

DECLARE @query varchar(max)
SET @query = 'SELECT * FROM ' + @table_name
EXEC (@query)

You could also skip the @query variable and simply:

EXEC('SELECT * FROM ' + @table_name)


--
Lawrence Garvin, M.S., MCITP(x2), MCTS(x5), MCP(x7), MCBMSP
Principal/CTO, Onsite Technology Solutions, Houston, Texas
 >> Stay informed about: Table as a variable 
Back to top
Login to vote
smzrkh

External


Since: Dec 10, 2008
Posts: 2



(Msg. 5) Posted: Wed Dec 10, 2008 6:17 am
Post subject: Re: Table as a variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 9, 11:46 am, "Lawrence Garvin" wrote:
> "Asit Kaushik" wrote in messagenews:21E460DB-C0FF-4B33-8514-536487ABCCF2@microsoft.com...
>
>
>
>
>
> > "Timothy Peer" wrote:
>
> >> I am reading a list of tables from a table and wish to execute a SQL
> >> statement for each table from this list. I tried declaring @table_name,
> >> depositing the table name from my table-list data. However, the "Select *
> >> from @table_name" SQL fails. Any ideas?
> > Hi,
> > I think there is problrm with the query it should be
>
> > declare @query varchar(2000)
>
> > sert @query='select * from ' +@@table_name
>
> > exec (@query)
>
> > This should work
>
> This, also, will not work. You cannot use a variable in the FROM clause of a
> query under any conditions.
>
> The above concept is valid; however, the execution is incorrect.
>
> Given a preexisting variable @table_name containing a valid two-part table
> name.
>
> DECLARE @query varchar(max)
> SET @query = 'SELECT * FROM ' + @table_name
> EXEC (@query)
>
> You could also skip the @query variable and simply:
>
> EXEC('SELECT * FROM ' + @table_name)
>
> --
> Lawrence Garvin, M.S., MCITP(x2), MCTS(x5), MCP(x7), MCBMSP
> Principal/CTO, Onsite Technology Solutions, Houston, Texas- Hide quoted text -
>
> - Show quoted text -

what about a cursor?
 >> Stay informed about: Table as a variable 
Back to top
Login to vote
Lawrence Garvin

External


Since: Dec 15, 2005
Posts: 14



(Msg. 6) Posted: Wed Dec 10, 2008 9:09 am
Post subject: Re: Table as a variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

wrote in message


>> This, also, will not work. You cannot use a variable in the FROM clause
>> of a
>> query under any conditions.
>>
>> The above concept is valid; however, the execution is incorrect.
>>
>> Given a preexisting variable @table_name containing a valid two-part
>> table
>> name.
>>
>> DECLARE @query varchar(max)
>> SET @query = 'SELECT * FROM ' + @table_name
>> EXEC (@query)
>>
>> You could also skip the @query variable and simply:
>>
>> EXEC('SELECT * FROM ' + @table_name)


> what about a cursor?


What about it?


--
Lawrence Garvin, M.S., MCITP(x2), MCTS(x5), MCP(x7), MCBMSP
Principal/CTO, Onsite Technology Solutions, Houston, Texas
 >> Stay informed about: Table as a variable 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How do I "using bcp_columns with a variable number of bcp_.. - hello, I have MFC VC++7.1 program which has the following code segment: // Specify the colums and format to copy retcode = bcp_columns(hdbc1, 6 ); if ( (retcode != SUCCEED) ) { return; } retcode = bcp_colfmt(hdbc1, 1, SQLCHARACTER, 0, SQL_VARLEN_DATA,....

Why the table is locked ? - Dear all, I have Access Font End to SQL server Back end. In an Access form, I use combobox, based on a stored procedure, which based on 1 table (SELECT LocationID, LocationDescription FROM Locations). When I open the form and check in the SQL server....

Temp table. - Is there any one let me know, in the stored procedure I am creating temprary table and droping table at the end, when single user access it is working fine but when simultaneous user access of that stored procedure it giving error like #Temp table is....

updating an sql table using another sql table - I started with a CSV file which I imported into SQL to create a table. My initial table is employees (ID, LastName, FirstName, Department, Division, Title, OfficePhone, MobilePhone, Login). The new table is called employees_update ( LastName, FirstName,...

Duplicate table - Hi, In my program i need to duplicate a table in a current data base. I'm thinkin' of reading the data base columns and then rows and so i create another table Is there any other easy and fast method with SQL Server 2005, because my idea is so slow I'm..
   Database Forums (Home) -> Client All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]