[英]VBA, ADO.Connection and PostgreSQL how to pass query parameter into pg_typeof()
我一直在嘗試通過在以下代碼部分中將列名作為參數傳遞來獲取列的數據類型。 但是我的查詢返回的是“未知”值,而不是定義的數據類型(例如字符變化,整數,文本等)。如果我直接將參數值提供給SQL查詢而不是問號(?),它將返回我想查看的內容。 (“從t_haendler限制1中選擇SELECT pg_typeof(haendler_id);”)。 我該如何解決這個問題?
Sub PGTYPEOF()
Dim CN As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim CM As New ADODB.Command
Dim PM1 As New ADODB.Parameter
Dim strConn As String
Dim i, ItemVal As Variant
strConn = "Driver={PostgreSQL ODBC Driver(Unicode)};DSN=postgres;Server=localhost;
Port=5432;UID=postgres;PWD=###; Database=###;READONLY=0;PROTOCOL=6.4;FAKEOIDINDEX=0;
SHOWOIDCOLUMN=0; ROWVERSIONING=0;SHOWSYSTEMTABLES=1"
CN.Open strConn
i = "haendler_id"
With CM
.ActiveConnection = CN
.CommandText = "SELECT pg_typeof(?) from t_haendler limit 1;"
.CommandType = adCmdText
Set PM1 = .CreateParameter(, adVarChar, adParamInput, 30)
PM1.Value = i
.Parameters.Append PM1
Set RS = .Execute
End With
Do While Not RS.EOF
'Debug.Print RS.Fields(0).Value
ItemVal = RS.Fields(0).Value
RS.MoveNext
Loop
End Sub
我不知道為什么,但是pg_typeof()函數不起作用。 我解決了如下更改SQL查詢的問題:“從information_schema.columns中選擇data_type,其中table_name =?和column_name =?;”
With CM
.ActiveConnection = CN
.CommandText = "select data_type from information_schema.columns where
table_name = ? and column_name = ?;"
.CommandType = adCmdText
Set PM1 = .CreateParameter(, adVarChar, adParamInput, 30)
PM1.Value = i
.Parameters.Append PM1
Set PM2 = .CreateParameter(, adVarChar, adParamInput, 30)
PM2.Value = j
.Parameters.Append PM2
Set RS = .Execute
End With
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.