I have multiple databases and their names are in masterDBTable.
MasterDBTable
ID DBName
CustomerDB1
CustomerDB2
CustomerDB3
In each of these DB's, there's a table called clients
How would I loop through and insert all the clients from each DB into a temp table? When I try I get the following error:
"variable assignment is not allowed in a cursor declaration."
Here's an example:
DECLARE @DataBaseName VARCHAR
,@SQL NVARCHAR
CREATE TABLE #TempClient (ClientName VARCHAR(50))
DECLARE clients CURSOR FAST_FORWARD READ_ONLY
FOR
SELECT DISTINCT @DataBaseName = DataBaseName
FROM AIMS.DataBaseMaster
OPEN clients
FETCH NEXT
FROM clients
INTO @DataBaseName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'INSERT INTO #TempClient(ClientName)
SELECT ClientName FROM ' + @DataBaseName + '.[Example].[Clients]'
EXECUTE sp_executesql @SQL
END
CLOSE clients
DEALLOCATE clients
SELECT *
FROM #TempClient