用adodb驱动查询mssql数据。如果参数带有大括号。就会显示错误:
ServeSrs sql db.Prepare error发生意外。 (语法错误或违反访问规则)
代码如下:
db, err0 := sql.Open("adodb", cfg.Cfg["mssql"])
if err0 != nil {
log.Println("ServeSrs sql open error")
} else {
defer db.Close()
stmt1, err0 := db.Prepare(`
select top ? *
from (select a.[Id], a.[CreateDate],a.[PublisherId],a.[NickName],a.[FileSize],a.[Followed],
a.[Readed],a.[FilePath], b.[STitle],row_number() over(order by a.CreateDate desc) as rownumber
from [hds12204021_db].[dbo].[Web2019_historylist] a, [hds12204021_db].[dbo].[Web2019_roomlist] b
where a.roomid = ? and a.Deleted = 0 and a.roomid=b.id ) temp_row
where rownumber>((?-1)*?)
`)
if err0 != nil {
log.Println("ServeSrs sql db.Prepare error" + err0.Error())
} else {
defer stmt1.Close()
rows, err := stmt1.Query(pagesize, roomid, pageid, pagesize)
然后我们把Prepare格式化的字符串,换个方式搞下:
db, err0 := sql.Open("adodb", cfg.Cfg["mssql"])
if err0 != nil {
log.Println("ServeSrs sql open error")
} else {
defer db.Close()
sqlstr := `
select top ` + pagesize + ` *
from (select a.[Id], a.[CreateDate],a.[PublisherId],a.[NickName],a.[FileSize],a.[Followed],
a.[Readed],a.[FilePath], b.[STitle],row_number() over(order by a.CreateDate desc) as rownumber
from [hds12204021_db].[dbo].[Web2019_historylist] a, [hds12204021_db].[dbo].[Web2019_roomlist] b
where a.roomid = '` + roomid + `' and a.Deleted = 0 and a.roomid=b.id ) temp_row
where rownumber>((?-1)*?)
`
//log.Println(sqlstr)
stmt1, err0 := db.Prepare(sqlstr)
if err0 != nil {
log.Println("ServeSrs sql db.Prepare error" + err0.Error())
} else {
defer stmt1.Close()
rows, err := stmt1.Query(pageid, pagesize)
OK,先自己拼接字符串,再喂给mssql 的adodb驱动就行了。。这坑太奇葩。。
有疑问加站长微信联系(非本文作者)