一个查询中的多个查询

时间:2012-05-21 10:00:35

标签: sql ms-access

我有一个问题:

是否可以在一个查询中收集多个查询?

即在表单中我有10个这样的查询:

1.  SELECT ThisValue FROM thatTable1 WHERE
2.  SELECT ThisValue FROM thatTable2 WHERE
                 .......
10.      SELECT ThisValue FROM thatTable10 WHERE

我应该在哪里写,即在Access中使用此代码。它可以在Access中使用,或者只能在MS SQL中执行此操作....

CREATE PROC dbo.proc_app_CollectControlData
AS
    SET NOCOUNT ON

    DECLARE @t TABLE
    (
      CONTROLNAME   nvarchar(74),
      CONTROLVALUE  nvarchar(255)
    )

    -- Now we collect the data for the 10 different controls
    INSERT INTO @t
    (CONTROLNAME, CONTROLVALUE)
    SELECT 'MyFirstControl',
            ThisValue
    FROM dbo.ThatTable
    WHERE Condition1

    ...

    INSERT INTO @t
    (CONTROLNAME, CONTROLVALUE)
    SELECT 'MyTenthControl',
            ThisValue
    FROM dbo.ThatTable
    WHERE Condition10

    -- And now we return all found data to the client
    SELECT * FROM @
    SET NOCOUNT OFF
GO

2 个答案:

答案 0 :(得分:3)

SELECT ThisValue FROM thatTable1 WHERE ...
UNION ALL
SELECT ThisValue FROM thatTable2 WHERE ...
UNION ALL
SELECT ThisValue FROM thatTable10 WHERE ...

答案 1 :(得分:3)

我认为您正在寻找UNION Operator

SELECT  'MyFirstControl' AS ControlName, ThisValue AS ControlValue
FROM    thatTable1 
WHERE   Condition1 
UNION ALL
SELECT  'MySecondControl' AS ControlName, ThisValue AS ControlValue
FROM    thatTable2
WHERE   Condition2
UNION ALL
SELECT  'MyThirdControl' AS ControlName, ThisValue AS ControlValue
FROM    thatTable3
WHERE   Condition3