I have two databases. They are on different servers. One is named REPORTDB and the other is named TRANSDB. TRANSDB is referenced as a linked server on REPORTDB named TRANS.
I execute a query like this from REPORTDB using Four Part Naming and Distributed Transactions:
SET @val = ''
SET @DBName = 'TRANSDB'
SELECT @val = @val + 'CREATE SYNONYM ' + [name] + ' for ' + @DBName + '.dbo.'
+ [name] + CHAR(10) + CHAR(13)
FROM TRANS.db.sys.tables;
I expect the result of this query to generate a CREATE SYNONYM statement for each table in TRANSDB inside my REPORTDB (Table1, Table2, Table3). However, it only creates one statement and that is the last table that is returned in the result set from TRANSDB (Table3).
This correctly returns three CREATE SYNONYM statements for Table1, Table2, and Table3 using OPENQUERY:.
SET @val = ''
SET @DBName = 'TRANSDB'
SELECT @val = @val + 'CREATE SYNONYM + [name] + ' for ' + @DBName + '.dbo.'
+ [name] + CHAR(10) + CHAR(13)
FROM OPENQUERY( TRANS, ' select [name] FROM db.sys.tables')
Since I do not wish to use openquery, how can I get the DISTRIBUTED TRANSACTION using four part naming to return results correctly?
