Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

How to use a variable for a Server or Database name in FROM Statement

$
0
0

Hi, 

I was wondering if anything like the below is possible?

DECLARE

@DB1 CHAR(25),

@DB2 CHAR(25),

@SERVER CHAR(25)       

SELECT 

@DB1 = (SELECT PARAM_VALUE FROM UTIL_PARM WHERE PARAM_ID = 'DB1') --Returns DB1 name

@DB2 = (SELECT PARAM_VALUE FROM UTIL_PARM WHERE PARAM_ID = 'DB2') --Returns DB2 name

@SERVER = (SELECT PARAM_VALUE FROM UTIL_PARM WHERE PARAM_ID = 'SERVER') --Returns Server name

SELECT  *

FROM        @SERVER.@DB1.dbo.TABLE_NAME


Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>