有没有办法在不创建表的情况下在SQL Server中循环遍历多个数据库?我必须创建一个表才能运行下面的查询。我想在不创建表的情况下运行此查询,并在一个网格中显示结果。我尝试用{替换insert into WSA.dbo.TEST
但是错误。还有另一种循环方式,不需要我创建一个表格并在一个网格上显示我的所有结果吗?
答案 0 :(得分:5)
好吧,我没有看到您实际返回或显示任何结果的任何地方,但这是一种更简单的方法来完成您的代码当前正在做的事情(您可以填写...
详细信息) :
DECLARE @x TABLE(db SYSNAME);
INSERT @x VALUES('Hs'),('Ge'),('At'),('LW'),('SE'),('LL');
DECLARE @sql NVARCHAR(MAX) = N'';
SELECT @sql += N'INSERT WSA.dbo.TEST(column list please)
SELECT ptno, MAX ... FROM (SELECT d.*, row_number() ...
FROM ' + db + '_Active.dbo.DTLPAYMENTS AS d) sq
GROUP BY ptno, cd, rn;
' FROM @x;
PRINT @sql;
-- EXEC sp_executesql @sql;
然而,重新阅读后,你必须将最后的SELECT
从代码中删除 - 我认为重点是插入表中,而不是仅仅在单个网格中显示结果。如果你想避开桌子,那么你可以说:
DECLARE @x TABLE(db SYSNAME);
INSERT @x VALUES('Hs'),('Ge'),('At'),('LW'),('SE'),('LL');
DECLARE @sql NVARCHAR(MAX) = N'';
SELECT @sql += N'UNION ALL
SELECT ptno, MAX ... FROM (SELECT d.*, row_number() ...
FROM ' + db + '_Active.dbo.DTLPAYMENTS AS d) sq
GROUP BY ptno, cd, rn
' FROM @x;
SET @sql = STUFF(@sql, 1, 11, '');
PRINT @sql;
-- EXEC sp_executesql @sql;
此查询的输出(如this SQLfiddle中所示)如下:
SELECT ptno, MAX ... FROM (SELECT d.*, row_number() ...
FROM Hs_Active.dbo.DTLPAYMENTS AS d) sq
GROUP BY ptno, cd, rn
UNION ALL
SELECT ptno, MAX ... FROM (SELECT d.*, row_number() ...
FROM Ge_Active.dbo.DTLPAYMENTS AS d) sq
GROUP BY ptno, cd, rn
UNION ALL
SELECT ptno, MAX ... FROM (SELECT d.*, row_number() ...
FROM At_Active.dbo.DTLPAYMENTS AS d) sq
GROUP BY ptno, cd, rn
UNION ALL
SELECT ptno, MAX ... FROM (SELECT d.*, row_number() ...
FROM LW_Active.dbo.DTLPAYMENTS AS d) sq
GROUP BY ptno, cd, rn
UNION ALL
SELECT ptno, MAX ... FROM (SELECT d.*, row_number() ...
FROM SE_Active.dbo.DTLPAYMENTS AS d) sq
GROUP BY ptno, cd, rn
UNION ALL
SELECT ptno, MAX ... FROM (SELECT d.*, row_number() ...
FROM LL_Active.dbo.DTLPAYMENTS AS d) sq
GROUP BY ptno, cd, rn
当您填写...部分时,应在所有6个数据库中生成有效的UNION ALL
查询。如果你没有在PRINT
输出中得到它,那么我怀疑你将查询转录错误或者你将无效的东西放入......部分。
STUFF()
函数只是在执行之前删除第一个UNION ALL
子句。
当然,您不会在此结果中知道哪些行来自哪个数据库。如果需要,可以添加:
SELECT @sql += N'UNION ALL
SELECT ''' + db + ''', ptno, MAX ... FROM (SELECT d.*, row_number() ...
FROM ' + db + '_Active.dbo.DTLPAYMENTS AS d) sq
GROUP BY ptno, cd, rn;
' FROM @x;
答案 1 :(得分:3)
为了尽可能少地修改代码逻辑,我将采取以下措施来避免使用永久表...
USE WSA
Delete From TEST
GO
DECLARE @Database varchar(20),
@i int = 0,
@SQL Nvarchar(2000)
Declare @table Table
(ptno Int, --Or whatever datatype it should be
dr_cd Int, --Or whatever datatype it should be
dr_amt Int, --Or whatever datatype it should be
dr_desc Int, --Or whatever datatype it should be
cr_cd Int, --Or whatever datatype it should be
cr_amt Int, --Or whatever datatype it should be
cr_desc Int) --Or whatever datatype it should be
WHILE @i < 6
BEGIN
if @i = 0 SELECT @Database = 'Hs_Active'
if @i = 1 SELECT @Database = 'Ge_Active'
if @i = 2 SELECT @Database = 'At_Active'
if @i = 3 SELECT @Database = 'LW_Active'
if @i = 4 SELECT @Database = 'SE_Active'
if @i = 5 SELECT @Database = 'LL_Active'
Set @SQL = 'USE '+@Database +'
insert into WSA.dbo.TEST
Select ptno,
max(case sign(amt) when 1 then cd end) dr_cd,
max(case sign(amt) when 1 then amt end) dr_amt,
max(case sign(amt) when 1 then description end) dr_desc,
max(case sign(amt) when -1 then cd end) cr_cd,
max(case sign(amt) when -1 then amt end) cr_amt,
max(case sign(amt) when -1 then description end) cr_desc
from (select d.*,
row_number() over (partition by ptno, cd, sign(amt)
order by abs(amt)) rn
from DTLPAYMENTS d) sq
group by ptno, cd, rn'
Insert @table
Exec sp_executeSQL @SQL
SELECT @i=@i+1
END
-- Whatever you want to do with @table aka results