Trying to create a large dynamic query keeps truncating
I have a SQL query on a SQL server where I am trying to create a large query using "union" on a large number of databases. However, the request continues to be truncated. According to my research, this shouldn't happen if everyone is varchar
selected for varchar(MAX)
. I tried to do this, however, it still gets truncated. The final request should be @finalQuery
varible. Can anyone help with the query below?
DECLARE @name VARCHAR(MAX) -- database name
DECLARE @path VARCHAR(MAX) -- path for backup files
DECLARE @fileName VARCHAR(MAX) -- filename for backup
DECLARE @fileDate VARCHAR(MAX) -- used for file name
DECLARE @executeQuery VARCHAR(MAX)
DECLARE @finalQuery VARCHAR(MAX)
SET @finalQuery = ''
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master..sysdatabases
WHERE name NOT IN (CAST('master' AS VARCHAR(MAX)),CAST('model' AS VARCHAR(MAX)),CAST('msdb' AS VARCHAR(MAX)),CAST('tempdb' AS VARCHAR(MAX)))
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @executeQuery=CAST(
'SELECT TOP 1000
[EmailAddress] as ''Email Address''
,[FirstName] as ''First Name''
,[LastName] as ''Last Name''
,[LastLogin] as ''Last Login'',
Name as ''User Role''
FROM '+@name+'.[dbo].[User] c
INNER JOIN
( SELECT * FROM '+@name+'.[dbo].[SecurityRole] as a
INNER JOIN '+@name+'.[dbo].[SecurityRoleToUser] as b
ON (a.ID=b.SecurityRoleID)
) d
ON (c.ID=d.UserID)
WHERE IsActive=1' AS VARCHAR(MAX))
--PRINT @executeQuery
--PRINT @name
--PRINT @executeQuery
SET @finalQuery = CAST(@executeQuery+' UNION ALL ' +@finalQuery AS VARCHAR(MAX))
--PRINT @executeQUery
--EXEC (@executeQuery)
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
PRINT @finalQuery
--EXEC(@finalQuery)
source to share
You are better off not doing it UNION
in many databases. And you don't need to. Also, database names etc. Anything sysname
that equates to NVARCHAR(128)
, so it's better to use NVARCHAR(MAX)
instead VARCHAR(MAX)
.
Step 1: Less Complex Query Generation
DECLARE @DatabaseName sysname;
DECLARE @Query NVARCHAR(MAX),
@Template NVARCHAR(MAX);
SET @Query = '';
SET @Template = N'USE [?];
SELECT TOP 1000
[EmailAddress] as [Email Address]
,[FirstName] as [First Name]
,[LastName] as [Last Name]
,[LastLogin] as [Last Login],
Name as [User Role]
FROM [dbo].[User] c
INNER JOIN
( SELECT * FROM [dbo].[SecurityRole] as a
INNER JOIN [dbo].[SecurityRoleToUser] as b
ON (a.ID=b.SecurityRoleID)
) d
ON (c.ID=d.UserID)
WHERE IsActive = 1;
';
SELECT @Query = (@Query + REPLACE(@Template, N'?', sd.[name]))
FROM sys.databases sd
WHERE sd.[name] NOT IN (N'master', N'model', N'msdb', N'tempdb')
AND HAS_DBACCESS(sd.[name]) = 1;
--EXEC(@Query); -- uncomment when not debugging
SELECT LEN(@Query); -- 9506 on my system -- comment out if debugging
print @query; -- truncates at 4000 chars for NVARCHAR -- comment out if debugging
Step 2: No UNION required
Instead of using UNION to keep everything in one result set, just insert multiple result sets into a local temporary table.
CREATE TABLE #tmp (DatabaseName sysname NOT NULL,
EmailAddress NVARCHAR(200), FirstName NVARCHAR(50),
LastName NVARCHAR(50), LastLogin DATETIME, UserRole VARCHAR(50);
DECLARE @Query NVARCHAR(MAX),
@Template NVARCHAR(MAX);
SET @Query = '';
SET @Template = N'USE [?];
SELECT TOP 1000
DB_NAME() AS [DatabaseName],
[EmailAddress] as [Email Address]
,[FirstName] as [First Name]
,[LastName] as [Last Name]
,[LastLogin] as [Last Login],
Name as [User Role]
FROM [dbo].[User] c
INNER JOIN
( SELECT UserID, Name--* -- see Step #3 below
FROM [dbo].[SecurityRole] sr
INNER JOIN [dbo].[SecurityRoleToUser] srtu
ON sr.ID = srtu.SecurityRoleID
) d
ON c.ID = d.UserID
WHERE IsActive = 1;
';
SELECT @Query = (@Query + REPLACE(@Template, N'?', sd.[name]))
FROM sys.databases sd
WHERE sd.[name] NOT IN (N'master', N'model', N'msdb', N'tempdb')
AND HAS_DBACCESS(sd.[name]) = 1;
INSERT INTO #tmp (DatabaseName, EmailAddress, FirstName, LastName, LastLogin, UserRole)
EXEC(@Query);
SELECT * FROM #tmp;
Step 3:
Probably best used SELECT *
in a subquery SELECT * FROM [dbo].[SecurityRole] as a
. Just pick the fields you want as they will use indexes more often. It looks like you just need two fields:UserID, Name
source to share
It's the PRINT
one that gets truncated, not your variable:
From docs on PRINT
:
The message string can be up to 8,000 characters if it is a non-Unicode string, and 4000 characters long if it is a Unicode string. Longer lines are truncated. Data types
varchar(max)
andnvarchar(max)
are truncated to data types that do not exceedvarchar(8000)
andnvarchar(4000)
.
source to share
why use a cursor?
SELECT
'SELECT TOP 1000
[EmailAddress] as ''Email Address''
,[FirstName] as ''First Name''
,[LastName] as ''Last Name''
,[LastLogin] as ''Last Login'',
Name as ''User Role''
FROM ' + name + '.[dbo].[User] c'
.....
FROM master..sysdatabases
WHERE name NOT IN (CAST('master' AS VARCHAR(MAX)),CAST('model' AS VARCHAR(MAX)),CAST('msdb' AS VARCHAR(MAX)),CAST('tempdb' AS VARCHAR(MAX)))
source to share