briangriffin
Programmer
Server 1: OK-DR01, contains two databases (testNdb and TestFdb) that I need to combine query results.
Server 2: OSUMC_BI, OK-DR01 is a linked server.
This query runs fine and returns the data I need when running from OK-DR01:
So I wanted to create this as a stored procedure on OSUMC_BI, but can't figure out how to fully qualify it - adding server name to use statement doesn't seem to work.
Second choice was to compile it on the OK-DR01 server, but then I realized that I can't use a use statement in a stored procedure.
How can I modify this to be able to compile as a SP on either server? TIA
Server 2: OSUMC_BI, OK-DR01 is a linked server.
This query runs fine and returns the data I need when running from OK-DR01:
Code:
use Testndb
DECLARE @TableRowCounts TABLE ([TableName] VARCHAR(128), [RowTotal] INT) ;
INSERT INTO @TableRowCounts ([TableName], [RowTotal])
EXEC sp_MSforeachtable 'SELECT ''?'' [TableName], COUNT(*) [RowCount] FROM ?' ;
declare @OSUTableStatus table(DatabaseName varchar(12),TableName varchar(128), RowTotal int)
insert into @OSUTableStatus (DatabaseName, TableName, RowTotal)
select 'TestNdb', substring(TableName,8,LEN(TableName) - 8), RowTotal from @TableRowCounts
delete from @TableRowCounts
use Testfdb
INSERT INTO @TableRowCounts ([TableName], [RowTotal])
EXEC sp_MSforeachtable 'SELECT ''?'' [TableName], COUNT(*) [RowCount] FROM ?' ;
insert into @OSUTableStatus (DatabaseName, TableName, RowTotal)
select 'TestFdb', substring(TableName,8,LEN(TableName) - 8), RowTotal from @TableRowCounts
use Testndb
declare @TableUpdateTime table (TableName varchar(128), LastUpdate smalldatetime)
insert into @TableUpdateTime
SELECT OBJECT_NAME(OBJECT_ID) AS TableName, last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'Testndb') order by 1
use Testfdb
insert into @TableUpdateTime
SELECT OBJECT_NAME(OBJECT_ID) AS TableName, last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'Testfdb')
select a.*, b.LastUpdate from @OSUTableStatus a left outer join @TableUpdateTime b
on a.TableName = b.TableName
order by 2
So I wanted to create this as a stored procedure on OSUMC_BI, but can't figure out how to fully qualify it - adding server name to use statement doesn't seem to work.
Second choice was to compile it on the OK-DR01 server, but then I realized that I can't use a use statement in a stored procedure.
How can I modify this to be able to compile as a SP on either server? TIA