-2

我有个问题:

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

即在表单中我有 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
4

2 回答 2

3
SELECT ThisValue FROM thatTable1 WHERE ...
UNION ALL
SELECT ThisValue FROM thatTable2 WHERE ...
UNION ALL
SELECT ThisValue FROM thatTable10 WHERE ...
于 2012-05-21T10:02:49.620 回答
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
于 2012-05-21T10:03:33.403 回答