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!!!!
Comments
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!!!!
:
: 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!!!!
: :
:
:
: 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!!!!
: : :
: :
: :
:
: