简体   繁体   中英

VBA, ADO.Connection and PostgreSQL how to pass query parameter into pg_typeof()

I have been trying to get datatype of a column by passing the column name as parameter in following code part. But my query returns "unknown" value instead of defined datatypes like character varying, integer, text, etc. If I give paramater value directly into SQL query instead of question mark (?), it returns what I want to see. ("SELECT pg_typeof(haendler_id) from t_haendler limit 1;"). How can I handle this problem?

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

I do not know why but pg_typeof() function does not function. I solved my problem changing my sql query as follows "select data_type from information_schema.columns where table_name = ? and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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