mattn / go-adodb

Microsoft ActiveX Object DataBase driver for go that using exp/sql
http://mattn.kaoriya.net/
MIT License
142 stars 36 forks source link

I cannot get any rows, when I query a proc #56

Open ymofen opened 2 years ago

ymofen commented 2 years ago

create proc xxx1 as insert table_1 select 1 select * from table_1

golang

rows, err := db.query('exec xxx1');

rows.Columns();  // empty
rows.Next();    //  false
mattn commented 2 years ago

What is your:

ymofen commented 2 years ago

sql.open("adodb", "Provider=sqloledb;Data Source=127.0.0.1;Initial Catalog=dss_test;User ID=sa;Password=xxxx")

mattn commented 2 years ago

What server? sqlserver?

ymofen commented 2 years ago

sqlserver is mssql2014 or mssql2018 r2 / sql proc / ALTER procedure [dbo].[dss_list_proc] @instr varchar(64)='001', @instr2 varchar(64)='002' as begin
/ unexception if object_id('tempdb..#temp') is not null drop table #temp select 1 as A into #temp select from #temp */

 /* will be ok */
 select 1 as A

end

golang code func TestADODB(t *testing.T) {

str := "Provider=sqloledb;Data Source=127.0.0.1;Initial Catalog=dss_test;User ID=sa;Password=sa@2014"
db, err := sql.Open("adodb", str)
if err != nil{
    fmt.Println(err)
    return;
}

vsql := "exec dss_list_proc '001', '002'";
rows, e1 := db.Query(vsql)
if e1 != nil{
    fmt.Println(e1);
    return;
}

if rows.Next(){
    fmt.Println("OK")
}

}

ymofen commented 2 years ago

I code direct with go-ole, got the same results....

func TestADORecordSet(t *testing.T) {
    ole.CoInitialize(0)
    defer ole.CoUninitialize()

    unknown, err := oleutil.CreateObject("ADODB.Connection")
    if err != nil {
        fmt.Println(err);
        return;
    }
    db, err := unknown.QueryInterface(ole.IID_IDispatch)
    unknown.Release()
    if err != nil {
        fmt.Println(err);
        return;
    }
    rc, err := oleutil.CallMethod(db, "Open", "Provider=sqloledb;Data Source=127.0.0.1;Initial Catalog=dss_test;User ID=sa;Password=sa@2014")
    if err != nil {
        fmt.Println(err);
        return;
    }
    rc.Clear()
    {
        unknown, err := oleutil.CreateObject("ADODB.recordset")
        if err != nil {
            fmt.Println(err);
            return;
        }
        rs, err := unknown.QueryInterface(ole.IID_IDispatch)
        lvsql := "exec dss_list_proc '001', '002'"

        _, err1 := oleutil.CallMethod(rs, "Open", lvsql, db);
        if err1 != nil {
            fmt.Println(err1);
            return;
        }else{
            unknown, err := oleutil.GetProperty(rs, "Fields")
            if err != nil {
                fmt.Println(err);
                return;
            }
            fields := unknown.ToIDispatch()
            defer fields.Release()
            rv, err := oleutil.GetProperty(fields, "Count")
            if err != nil {
                fmt.Println(err);
                return;
            }
            nc := int(rv.Val)
            rv.Clear()
            cols := make([]string, nc)
            for i := 0; i < nc; i++ {
                var varval ole.VARIANT
                varval.VT = ole.VT_I4
                varval.Val = int64(i)
                val, err := oleutil.CallMethod(fields, "Item", &varval)
                if err != nil {
                    fmt.Println(err);
                    return;
                }
                item := val.ToIDispatch()
                val.Clear()
                name, err := oleutil.GetProperty(item, "Name")
                if err != nil {
                    item.Release()
                    fmt.Println(err);
                    return;
                }
                cols[i] = name.ToString()
                name.Clear()
                item.Release()
            }
            fmt.Println(cols)
        }
    }

}