SELECT INTO只插入最后一行--VBA,Access 2000?

时间:2017-07-19 09:50:34

标签: vba ms-access

我有一个我在循环中创建的SQL字符串:

"SELECT '" & Trim(Str(j)) & "' AS cpa, Count(Val('" & rsCPANezbirni("tipprod") & "')) AS BrojProd, Sum(Val('" & rsCPANezbirni("povrsina") & "')) AS p, Sum(Val('" & rsCPANezbirni("pmagacin") & "')) AS pm INTO T14_KPD_CPA_samo_podatoci FROM CPA_nezbirni WHERE (t4k" & Trim(Str(j)) & "<>'' Or t4k" & Trim(Str(j)) & " Is Not Null);"

我使用的循环是:

Dim j As Integer
j = 1
Do While j <= 3
    cpaSelectSQL = "SELECT '" & Trim(Str(j)) & "' AS cpa, Count(Val('" & rsCPANezbirni("tipprod") & "')) AS BrojProd, Sum(Val('" & rsCPANezbirni("povrsina") & "')) AS p, Sum(Val('" & rsCPANezbirni("pmagacin") & "')) AS pm INTO T14_KPD_CPA_samo_podatoci FROM CPA_nezbirni WHERE (t4k" & Trim(Str(j)) & "<>'' Or t4k" & Trim(Str(j)) & " Is Not Null);"
    Debug.Print "j = " & Str(j) & ", cpa select SQL: " & cpaSelectSQL
    DoCmd.RunSQL cpaSelectSQL, True

    On Error GoTo ErrorHandler
    j = j + 1
Loop

我遇到的问题,只有最后生成的行被复制到T14_KPD_CPA_samo_podatoci,即cpa = 3

我想将每个复制为cpa 1到3的值。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

SELECT INTO是&#34;创建表&#34;查询,它会在每次运行时覆盖目标表。

表格存在后,您需要INSERT INTO

答案 1 :(得分:1)

好吧,我使用了这种SQL查询

INSERT INTO table1 (col1, col2, ..., coln) SELECT col1, col2,..., coln FROM table2 WHERE condition GROUP BY colx;