Thread: dynamic query
06-07-11, 01:29 #1Registered User
- Join Date
- Sep 2010
Unanswered: dynamic query
I have a doubt.
I have two database, office and factory. i have tableA in office and tableB in factory. I am moving data from table A of one datbase into table B of another database through a procedure.
QUERY - insert into [factory].[dbo].tableB (field1, field2) select field1, field2 from [office].[dbo].tableA.
Suppose, I took a backup of tableA and restored the bakup into NEW DATABASE, "new_office".
Requirement is same, moving data from tableA to tableB.
But database name is different.
I want to know, is there any dynamic query or any way so that when database name gets changed then the query inside the procedure should get changed?
the previous query should automatically gets changed into :-
insert into [factory].[dbo].tableB (field1, field2) select field1, field2 from [new_office].[dbo].tableA.
06-07-11, 07:18 #2Registered User
- Join Date
- Sep 2001
- Chicago, Illinois, USA
Yes, this can be done, although not ideal.
Google "dynamic T-SQL".
Also, look at @@ServerName.Ken
Maverick Software Design
(847) 864-3600 x2