有没有办法可以在不创建表的情况下循环遍历 SQL Server 中的多个数据库?在运行下面的查询之前,我必须创建一个表。我想运行这个查询而不是每次都创建一个表并在一个网格中显示结果。我尝试用insert into WSA.dbo.TEST
go 替换,但错误出来了。是否有另一种循环方式不需要我创建一个表格并在一个网格上显示我的所有结果?
user2962271
问问题
292 次
2 回答
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;
此查询的输出(如此 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
UNION ALL
其中,当您填写 ... 部分时,应该会在所有 6 个数据库中生成有效查询。如果您没有在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;
于 2013-11-06T21:21:41.713 回答
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
于 2013-11-06T21:23:49.563 回答