Results 1 to 2 of 2

Thread: dynamic query

  1. #1
    Join Date
    Sep 2010

    Smile Unanswered: dynamic query

    Hi guys,

    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.

    Thank you

  2. #2
    Join Date
    Sep 2001
    Chicago, Illinois, USA
    Yes, this can be done, although not ideal.

    Google "dynamic T-SQL".

    Also, look at @@ServerName.

    Maverick Software Design

    (847) 864-3600 x2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts