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

SQL Server 2000 Query Issue: Extra Spaces in First Parameter #59

Open ooodjc opened 3 weeks ago

ooodjc commented 3 weeks ago

When querying data from a SQL Server 2000 database, when querying through db.Query(sql, id, name), there will be 16 extra spaces in the first parameter, resulting in failure to query the specified data.

//This is shown in the SQL Server Profiler. declare @P1 int set @P1=1 exec sp_prepexec @P1 output, N'@P1 char(20),@P2 varchar(40)', N'SELECT item_no, item_name, item_clsno, price, sale_price, vip_price FROM t_bd_item_info WHERE item_no like @P1 or item_name like @P2', '%090870% ', '%090870%' select @P1

mattn commented 3 weeks ago

Could you please show me your code?

ooodjc commented 3 weeks ago

`package main

import ( "database/sql" "fmt" "log" "os"

_ "github.com/mattn/go-adodb"

)

func main() { url := "Provider=SQLOLEDB;Initial Catalog=issyytv3;Data Source=192.168.0.116,1433;user id=sa;password=" issyytv3, err := sql.Open("adodb", url)

if err != nil {
    log.Fatal(err)
    return
}
defer issyytv3.Close()

args := os.Args

value := "%" + args[1] + "%"

sql := `SELECT item_no, item_name, item_clsno, price, sale_price, vip_price FROM t_bd_item_info WHERE item_no like ? or item_name like ?`

rows, err := issyytv3.Query(sql, value, value)
if err != nil {
    fmt.Println(err)
    return
}
defer rows.Close()

for rows.Next() {
    var itemNo, itemName, itemClsno string
    var price, salePrice, vipPrice float64
    err := rows.Scan(&itemNo, &itemName, &itemClsno, &price, &salePrice, &vipPrice)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(itemNo, itemName, itemClsno, price, salePrice, vipPrice)
}

} `