Waterstone
asked on
Select Into with variable for table_name
Hi,
I need to use a variable as the table name for a select into statement.
DECLARE
@WEDate NVarChar(8),
@PayPrefix NVarChar(10),
@PayTable NVarChar(20)
set @WEDate = '20071201'
set @PayPrefix = 'XXX'
set @PayTable = 'Pay_'+@PayPrefix+@WEDate
/* @PayTable now = Pay_XXX20071201 */
Select *
into @PayTable
from InputTable
I think I need to use exec sp_executesql
but need some help on the correct format, unless there is a better way.
Thanks
I need to use a variable as the table name for a select into statement.
DECLARE
@WEDate NVarChar(8),
@PayPrefix NVarChar(10),
@PayTable NVarChar(20)
set @WEDate = '20071201'
set @PayPrefix = 'XXX'
set @PayTable = 'Pay_'+@PayPrefix+@WEDate
/* @PayTable now = Pay_XXX20071201 */
Select *
into @PayTable
from InputTable
I think I need to use exec sp_executesql
but need some help on the correct format, unless there is a better way.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oops typo:
EXEC 'Select * into ' + @PayTable = 'from InputTable'
but I guess angels got it
EXEC 'Select * into ' + @PayTable = 'from InputTable'
but I guess angels got it
ASKER
Thanks! Exactly what I needed
DECLARE
@WEDate NVarChar(8),
@PayPrefix NVarChar(10),
@PayTable Sysname
set @WEDate = '20071201'
set @PayPrefix = 'XXX'
set @PayTable = 'Pay_'+@PayPrefix+@WEDate
EXEC 'Select * into @PayTable from InputTable'