dbTalk Databases Forums  

Table as a variable

microsoft.public.sqlserver.clients microsoft.public.sqlserver.clients


Discuss Table as a variable in the microsoft.public.sqlserver.clients forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Timothy Peer
 
Posts: n/a

Default Table as a variable - 06-16-2005 , 06:02 PM






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?

Reply With Quote
  #2  
Old   
Andrew J. Kelly
 
Posts: n/a

Default Re: Table as a variable - 06-16-2005 , 08:29 PM






Have a look at this:
http://www.sommarskog.se/dynamic_sql.html


--
Andrew J. Kelly SQL MVP


"Timothy Peer" <TimothyPeer (AT) discussions (DOT) microsoft.com> wrote

Quote:
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?



Reply With Quote
  #3  
Old   
Asit Kaushik
 
Posts: n/a

Default RE: Table as a variable - 12-08-2008 , 11:04 PM




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:

Quote:
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?

Reply With Quote
  #4  
Old   
Asit Kaushik
 
Posts: n/a

Default RE: Table as a variable - 12-08-2008 , 11:04 PM




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:

Quote:
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?

Reply With Quote
  #5  
Old   
Asit Kaushik
 
Posts: n/a

Default RE: Table as a variable - 12-08-2008 , 11:04 PM




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:

Quote:
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?

Reply With Quote
  #6  
Old   
Asit Kaushik
 
Posts: n/a

Default RE: Table as a variable - 12-08-2008 , 11:04 PM




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:

Quote:
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?

Reply With Quote
  #7  
Old   
Asit Kaushik
 
Posts: n/a

Default RE: Table as a variable - 12-08-2008 , 11:04 PM




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:

Quote:
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?

Reply With Quote
  #8  
Old   
Asit Kaushik
 
Posts: n/a

Default RE: Table as a variable - 12-08-2008 , 11:04 PM




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:

Quote:
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?

Reply With Quote
  #9  
Old   
Asit Kaushik
 
Posts: n/a

Default RE: Table as a variable - 12-08-2008 , 11:04 PM




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:

Quote:
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?

Reply With Quote
  #10  
Old   
Asit Kaushik
 
Posts: n/a

Default RE: Table as a variable - 12-08-2008 , 11:04 PM




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:

Quote:
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?

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.