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
  #41  
Old   
Lawrence Garvin
 
Posts: n/a

Default Re: Table as a variable - 12-10-2008 , 09:09 AM






<smzrkh (AT) gmail (DOT) com> wrote


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

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



Reply With Quote
  #42  
Old   
Lawrence Garvin
 
Posts: n/a

Default Re: Table as a variable - 12-10-2008 , 09:09 AM






<smzrkh (AT) gmail (DOT) com> wrote


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

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



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.