Microsoft Access SQL嵌套的select语句

时间:2014-02-02 19:04:45

标签: sql ms-access

如何为访问sql中的select statments形成的表提供临时名称? 我有这个:

SELECT PUNONJES.[ID_P], PUNONJES.[Emer]
FROM PUNONJES, PUSHIM
WHERE PUNONJES.[ID_P] = PUSHIM.[#ID_P] 
  AND PUSHIM.[Kohezgjatja] > (SELECT AVG(PUSHIM.[Kohezgjatja]) FROM PUSHIM)

我想将上面的选择存储在临时表中,然后让我们说temp,然后在temp表上使用下面的另一个选择,依此类推。怎么做?

1 个答案:

答案 0 :(得分:0)

单用户情况的一般概要可能是 -

' determine if temp table exists
If DCount("*", "MSYSOBJECTS", "Type = 1 and Name = 'temp'") = 0 Then
    ' temp table does not exist, create it with columns [ID_P] and [Emer]
    ....
EndIf


' empty the table of any prior records
...
"DELETE * From temp;"



' now use your prior sql, wrapped with Insert
INSERT INTO temp
SELECT PUNONJES.[ID_P], PUNONJES.[Emer]
FROM PUNONJES, PUSHIM
WHERE PUNONJES.[ID_P] = PUSHIM.[#ID_P] 
  AND PUSHIM.[Kohezgjatja] > (SELECT AVG(PUSHIM.[Kohezgjatja]) FROM PUSHIM)

对于多用户情况,您需要在行前面加上UserID或一些uniquifier,然后更改Delete和Insert以使用它。