如何在mssql中同时执行多个查询

时间:2011-06-10 10:46:54

标签: sql sql-server-2005

我知道;允许多个查询在工作室管理中进行exicute,但是有人知道如何将这些查询连接在一起,以便它们作为一个一起运行。下面是一个基本的查询,但我想一起运行其中的大约5个,而不是为它们创建不同的报告。它们都有不同的where子句,每个子句的列数相同。有办法吗?

干杯!

SELECT     InspectionProcedureName AS Inspection, COUNT(*) AS Total
FROM         UnitData_Vehicle
WHERE     (DATEDIFF(day, InspectionDateTime, GETDATE()) = 1)
GROUP BY InspectionProcedureName

我想看看

Inspection  Total
qc             10
Inspection  Total
photo         20
Inspection  Total
insp         30

2 个答案:

答案 0 :(得分:0)

您可以使用union all从查询中获取一个结果集

SELECT     InspectionProcedureName AS Inspection, COUNT(*) AS Total
FROM         UnitData_Vehicle
WHERE     (DATEDIFF(day, InspectionDateTime, GETDATE()) = 1)
GROUP BY InspectionProcedureName
UNION ALL
SELECT     InspectionProcedureName AS Inspection, COUNT(*) AS Total
FROM         AnotherTable
WHERE     (DATEDIFF(day, InspectionDateTime, GETDATE()) = 1)
GROUP BY InspectionProcedureName
UNION ALL
-- Next query etc...

答案 1 :(得分:0)

您可以使用UNION操作。