在动态SQL select语句中使用db名称

时间:2018-05-24 19:50:31

标签: sql sql-server ssms

道歉,我对动态SQL相对较新,我可以帮忙吗?我正在努力在数据库之间进行查询循环。

我可以让查询运行并返回结果,但它不在数据库之间移动

--
DECLARE @HUB_Instance VARCHAR(25);
DECLARE cur_collectHubData CURSOR FAST_FORWARD READ_ONLY FOR
SELECT name
FROM sys.databases
WHERE name LIKE '%HUB%'
      AND name NOT IN ( 'HUB_Training', 'HUB_Training_TNHG' );

OPEN cur_collectHubData;

FETCH NEXT FROM cur_collectHubData
INTO @HUB_Instance;

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @sql1 NVARCHAR(2000);
    SET @sql1
       = 'SELECT t1.Company,
       t1.StaffNumber,
       t1.FullName,
       COUNT(t1.FullName) AS NumberOfCases
FROM
(       SELECT tbl_Company.Name AS Company,
           IdCase,
           FullName AS CaseStatus,
           IdUser_Core_Negotiator,
           StaffNumber,
           FullName
    FROM dbo.tbl_PreCallCaseDetails
        LEFT JOIN dbo.tbl_Case
            ON tbl_Case.Id = tbl_PreCallCaseDetails.IdCase
        JOIN dbo.tbl_CaseStatus
            ON tbl_CaseStatus.Id = tbl_Case.IdCaseStatus
        JOIN Core..tbl_User
            ON tbl_User.Id = dbo.tbl_PreCallCaseDetails.IdUser_Core_Negotiator
        JOIN core..tbl_Company 
            ON  tbl_Company.Id = Core..tbl_User.IdCompany
    WHERE IdUser_Core_Negotiator IS NOT NULL
) t1
GROUP BY t1.Company,
         t1.FullName,
         t1.StaffNumber;';

    EXEC sys.sp_executesql @sql1;

    FETCH NEXT FROM cur_collectHubData
    INTO @HUB_Instance;
END;

CLOSE cur_collectHubData;
DEALLOCATE cur_collectHubData;



-- Doesn't loop between databases
DECLARE @HUB_Instance VARCHAR(25);
DECLARE cur_collectHubData CURSOR FAST_FORWARD READ_ONLY FOR
SELECT name
FROM sys.databases
WHERE name LIKE '%HUB%'
      AND name NOT IN ( 'HUB_Training', 'HUB_Training_TNHG' );

OPEN cur_collectHubData;

FETCH NEXT FROM cur_collectHubData
INTO @HUB_Instance;

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @sql1 NVARCHAR(2000);
    SET @sql1
       = N'SELECT t1.Company,
       t1.StaffNumber,
       t1.FullName,
       COUNT(t1.FullName) AS NumberOfCases
FROM
(       SELECT tbl_Company.Name AS Company,
           IdCase,
           FullName AS CaseStatus,
           IdUser_Core_Negotiator,
           StaffNumber,
           FullName
    FROM' + @HUB_Instance + N'.[dbo].tbl_PreCallCaseDetails
        LEFT JOIN' + @HUB_Instance + N'.[dbo].tbl_Case
            ON tbl_Case.Id = tbl_PreCallCaseDetails.IdCase
        JOIN' + @HUB_Instance + N'.tbl_CaseStatus
            ON tbl_CaseStatus.Id = tbl_Case.IdCaseStatus
        JOIN Core..tbl_User
            ON tbl_User.Id = dbo.tbl_PreCallCaseDetails.IdUser_Core_Negotiator
        JOIN core..tbl_Company 
            ON  tbl_Company.Id = Core..tbl_User.IdCompany
    WHERE IdUser_Core_Negotiator IS NOT NULL
) t1
GROUP BY t1.Company,
         t1.FullName,
         t1.StaffNumber;';

    EXEC sys.sp_executesql @sql1;

1 个答案:

答案 0 :(得分:2)

您的第二个光标没有FETCH NEXTEND。你也没有CLOSEDEALLOCATE光标。正如Sean指出的那样,在动态sql 中每个@HB_Instance实例之前也缺少一个空格

DECLARE @HUB_Instance VARCHAR(25);
DECLARE cur_collectHubData CURSOR FAST_FORWARD READ_ONLY FOR
SELECT name
FROM sys.databases
WHERE name LIKE '%HUB%'
      AND name NOT IN ( 'HUB_Training', 'HUB_Training_TNHG' );

OPEN cur_collectHubData;

FETCH NEXT FROM cur_collectHubData
INTO @HUB_Instance;

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @sql1 NVARCHAR(2000);
    SET @sql1
       = N'SELECT t1.Company,
       t1.StaffNumber,
       t1.FullName,
       COUNT(t1.FullName) AS NumberOfCases
FROM
(       SELECT tbl_Company.Name AS Company,
           IdCase,
           FullName AS CaseStatus,
           IdUser_Core_Negotiator,
           StaffNumber,
           FullName
    FROM ' + @HUB_Instance + N'.[dbo].tbl_PreCallCaseDetails
        LEFT JOIN ' + @HUB_Instance + N'.[dbo].tbl_Case
            ON tbl_Case.Id = tbl_PreCallCaseDetails.IdCase
        JOIN ' + @HUB_Instance + N'.tbl_CaseStatus
            ON tbl_CaseStatus.Id = tbl_Case.IdCaseStatus
        JOIN Core..tbl_User
            ON tbl_User.Id = dbo.tbl_PreCallCaseDetails.IdUser_Core_Negotiator
        JOIN core..tbl_Company 
            ON  tbl_Company.Id = Core..tbl_User.IdCompany
    WHERE IdUser_Core_Negotiator IS NOT NULL
) t1
GROUP BY t1.Company,
         t1.FullName,
         t1.StaffNumber;';

    EXEC sys.sp_executesql @sql1;
    --added everything below this line
    FETCH NEXT FROM cur_collectHubData
    INTO @HUB_Instance;
END;

CLOSE cur_collectHubData;
DEALLOCATE cur_collectHubData;

但是,我建议在这些表变量周围使用QUOTENAME(),以便进行衡量。