Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Report from multiple SQL Databases
Message
From
04/08/2017 09:33:15
 
General information
Forum:
Microsoft SQL Server
Category:
Database design
Miscellaneous
Thread ID:
01653049
Message ID:
01653076
Views:
51
>Hi,
>
>I think that theoretically it is possible to create a report (from VFP or maybe even other tools) that gets/pulls data from various SQL Server databases (even hosted by different SQL Servers).
>But I was wondering, has anybody done it and do you find it practical?
>
>I am "drafting" an approach of creating a report for a corporate that shows how the data is used in misc branches.
>
>TIA

Not long ago I did a query on all databases in one SQL Server instance, and it worked well:
SET NOCOUNT ON;

IF OBJECT_ID (N'tempdb.dbo.#temp') IS NOT NULL
   DROP TABLE #temp

CREATE TABLE #temp
(
       DB VARCHAR(50)
       , emNumber Character(10)
     , emName VARCHAR(50)
     , emFirst VARCHAR(50)
)

DECLARE @TableName NVARCHAR(50) 
SELECT @TableName = '[dbo].[Employees]'

DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = STUFF((
    SELECT CHAR(13) + 'SELECT ''' + name + ''', emNumber, emName, emFirst '
       + ' FROM [' + name + '].' + @TableName
       + ' WHERE delFlag = 0 AND emArchived = 0 '
    FROM sys.databases 
    WHERE OBJECT_ID(name + '.' + @TableName) IS NOT NULL
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')

INSERT INTO #temp (DB, emNumber, emName, emFirst)              
EXEC sys.sp_executesql @SQL

SELECT * 
FROM #temp t
Christian Isberner
Software Consultant
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform