Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a local Variable to return a Table Name

Status
Not open for further replies.

M8tt

Technical User
Feb 11, 2003
43
NL
I have a Data Transformation Task which is going to end up with 40-50 lines of code in it using table names which are quite lengthy. In order to make the code shorter and easier to read I am trying to substitute the table names with a variable but I am not having much joy and don't even know if this is possible.

As an example my code reads as follows: -

Declare @Trans nvarchar(80)
Set @Trans = '[R6_RELIAB_CURRENTDETAIL].[dbo].[TRANS_SERVINCIDENT]'

Select @Trans.[Transition_Field]
From @Trans

Rather than: -

Select [R6_RELIAB_CURRENTDETAIL].[dbo].[TRANS_SERVINCIDENT].[Transition_Field]
From [R6_RELIAB_CURRENTDETAIL].[dbo].[TRANS_SERVINCIDENT]

I have tried changing the variable type and length, dropping the ' marks and enclosing the @Trans in [].

Is what I am trying to do possible and if so how can I achieve it?

Many Thanks in advance.
 
You can do something like the following:
Code:
Declare @Trans nvarchar(80)
declare @sql nvarchar (200)
Set @Trans = 'MAT_ACCT_CUST'

SET @sql = 'select ACCT_SRC_REF from '+ @Trans
print @sql
EXEC sp_executesql @sql

[bandito] [blue]DBomrrsm[/blue] [bandito]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top