Database & SQL

Moderators: None (Apply to moderate this forum)
Number of threads: 1194
Number of posts: 2247

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Query a dynamic table...HELP! Posted by jshull on 1 May 2003 at 11:00 AM
Hello,

I am trying to write a query to pull data from a table that is system generated daily at midnight. I tried:

declare @tablename varchar(50) set @tablename = (select name from sysobjects where crdate = '05/01/2003') select * from @tablename

But I get an error on 'select * from @tablename' (must declare @tablename)

Any assistance is GREATLY APPRECIATED!!!!
Report
Re: Query a dynamic table...HELP! Posted by Daedalius on 1 May 2003 at 11:53 AM
I think what you want to do is create a string and then you want to execute the string:

DECLARE @SQL NVARCHAR(128);
SET @SQL = N'SELECT * FROM '+@tablename+';'
EXECUTE sp_executesql @SQL


: Hello,
:
: I am trying to write a query to pull data from a table that is system generated daily at midnight. I tried:
:
: declare @tablename varchar(50) set @tablename = (select name from sysobjects where crdate = '05/01/2003') select * from @tablename
:
: But I get an error on 'select * from @tablename' (must declare @tablename)
:
: Any assistance is GREATLY APPRECIATED!!!!
:

Report
Re: Query a dynamic table...HELP! Posted by jshull on 1 May 2003 at 12:05 PM
Worked like a charm. Thank you for the help!!!!


: I think what you want to do is create a string and then you want to execute the string:
:
: DECLARE @SQL NVARCHAR(128);
: SET @SQL = N'SELECT * FROM '+@tablename+';'
: EXECUTE sp_executesql @SQL
:
:
: : Hello,
: :
: : I am trying to write a query to pull data from a table that is system generated daily at midnight. I tried:
: :
: : declare @tablename varchar(50) set @tablename = (select name from sysobjects where crdate = '05/01/2003') select * from @tablename
: :
: : But I get an error on 'select * from @tablename' (must declare @tablename)
: :
: : Any assistance is GREATLY APPRECIATED!!!!
: :
:
:

Report
Re: Query a dynamic table...HELP! Posted by rpalser on 25 May 2007 at 2:58 PM
I am trying to declare a table as a variable use the code below, however in query analyzer I still get the error "Must Declare the Variable @tablename". How did you get it to "work like a charm?"

: Worked like a charm. Thank you for the help!!!!
:
:
: : I think what you want to do is create a string and then you want to execute the string:
: :
: : DECLARE @SQL NVARCHAR(128);
: : SET @SQL = N'SELECT * FROM '+@tablename+';'
: : EXECUTE sp_executesql @SQL
: :
: :
: : : Hello,
: : :
: : : I am trying to write a query to pull data from a table that is system generated daily at midnight. I tried:
: : :
: : : declare @tablename varchar(50) set @tablename = (select name from sysobjects where crdate = '05/01/2003') select * from @tablename
: : :
: : : But I get an error on 'select * from @tablename' (must declare @tablename)
: : :
: : : Any assistance is GREATLY APPRECIATED!!!!
: : :
: :
: :
:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.