简体   繁体   English

Access前端女士,PostgreSQL后端高效INSERT INTO…RETURNING…语句

[英]Ms Access frontend, PostgreSQL backend efficient INSERT INTO … RETURNING … statement

HI, 嗨,

I'm using MS Access as a frontend to PostgreSQL database. 我使用MS Access作为PostgreSQL数据库的前端。 And I have run into dead end: 我陷入了死胡同:

How would I get return value of passthrough SQL INSERT statement? 如何获得直通SQL INSERT语句的返回值?

I tried something like this: 我尝试过这样的事情:

strSQL = "INSERT INTO someTable (col1, col2) VALUES ('val1', 'val2') RETURNING col0"
'Create passtrhough query dynamically (another function)
CreateSPT ("some_temp_query", strSQL)
idvalue = DLookup("col0", "some_temp_query")

Apparenty this way INSERT statement will be called 2 times :) Does anybody know what is the correct way? 显然,这种方式INSERT语句将被调用2次:)有人知道正确的方法吗? I have run out of ideas 我的想法用光了

Edit: I'm using latest psqlODBC drivers 编辑:我正在使用最新的psqlODBC驱动程序

Assuming you have a saved pass-though query, then you can modify the sql on the fly, and return values that way. 假设您有一个保存的传递查询,则可以即时修改sql并以这种方式返回值。 The following code runs without you even having to declare any variables. 下面的代码运行,甚至无需声明任何变量。

With CurrentDb.QueryDefs("qryPassReturn")
   .SQL = "INSERT INTO someTable (col1, col2) VALUES ('val1', 'val2') RETURNING col0"
   Debug.Print .OpenRecordset()(0)
End With

I suppose you could declare a reocrdset to receive the return value, and go: 我想您可以声明一个reocrdset来接收返回值,然后执行以下操作:

Dim rstReturn  As DAO.Recordset

With CurrentDb.QueryDefs("qryPassReturn")
   .SQL = "INSERT INTO someTable (col1, col2) VALUES ('val1', 'val2') RETURNING col0"
   Set rstReturn = .OpenRecordset
End With

Debug.Print rstReturn(0)

So, you can just pass the sql, and the values returned will appear as a standard reocrdset. 因此,您只需传递sql,返回的值就会显示为标准reocrdset。 So, the first example should run fine and note how I not even declared one varialbe to make this work. 因此,第一个示例应该运行良好,并注意我什至没有声明一个变量来完成这项工作。 You as mentioned simply need a saved pass-though query. 如上所述,您只需要保存一个通过查询即可。

Seems that function below does the trick... Not sure about performance though :) 似乎下面的功能可以解决问题...尽管不确定性能:)

'Insert-SQL passthroug
'Pass here all the insert statements
Function ISPT(SQLString)
Dim strODBCString As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim result As Long

    strODBCString = _
      "DRIVER={PostgreSQL Unicode};DATABASE=" & settings_getDBName() & _
      ";SERVER=" & settings_getServerAddress & _
      ";PORT=" & settings_getServerPort & _
      settings_getConnStrParams

      cn.Open strODBCString

      rs.Open SQLString, cn


      'return value
      result = rs.Fields(0).Value



    'Cleanup
    If rs.state <> adStateClosed Then rs.Close
    Set rs = Nothing
    If cn.state <> adStateClosed Then cn.Close
    Set cn = Nothing

    ISPT = result

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM