如何从VBA代码执行PostgreSQL中存储的名为Test1的函数?
例如,函数定义如下:

CREATE OR REPLACE FUNCTION "public"."Test1" (
)
RETURNS bit AS
$body$
BEGIN
    INSERT INTO test ("name") VALUES ('1');
RETURN 1;

END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 100;

现在我正试图以这种方式执行此函数:
Function TestCall()
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset

Dim strSQl As String

strSQl = "SELECT * FROM Test1();"

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset(strSQl, dbOpenDynaset, dbSeeChanges)
'this doesnt work as well: syntax error'
dbs.Execute strSQl


If Not (rst.BOF And rst.EOF) Then
    do some work here
End If

End Function

但我现在有点。我不知道怎么执行,怎么能做到?

最佳答案

它失败是因为您已将dbs设置为当前数据库。执行此语句时,access将查找表“Test1()”,并在找不到表时抛出hissy-fit。
我不确定如何使用postgre,但这就是我如何使用SQL server做类似的事情,所以我假设它是相同的,但是使用不同的连接字符串

Dim dbCon as new ADODB.Connection
Dim rst as new ADODB.Recordset

Dbcon.connectionstring=”Your connection string goes here!”
Dbcon.open

Rst.open strsql

等等

10-07 15:27