使用ODBC驱动程序在Microsoft SQL Server上调用存储过程。

huangapple go评论96阅读模式
英文:

Call stored procedure on Microsoft SQL Server using ODBC driver

问题

我有一个存储过程,假设名称为"vijaystoredprocedure",如果是在MSSQL中的某个查询,我会在Go中这样查询:

l_query_str = fmt.Sprintf(`select * from Users where Fname='%s'`, l_firstanme)

row, err := DBC.Query(l_query_str)
	if err != nil {
		log.Fatal("Prepare failed:", err.Error())
	}

	_, rows, r_err := DBScan_fn(row)

	if r_err != nil {
		fmt.Println("no data found err")
		return
	}

现在,由于我需要从存储过程中获取值...有人能建议如何在Go中实现这个吗?

英文:

I have a stored procedure let the name be "vijaystoredprocedure" , if it is some query in mssql then i will query in Go like

l_query_str = fmt.Sprintf(`select * from Users where Fname='%s'`, l_firstanme)

row, err := DBC.Query(l_query_str)
	if err != nil {
		log.Fatal("Prepare failed:", err.Error())
	}

	_, rows, r_err := DBScan_fn(row)

	if r_err != nil {
		fmt.Println("no data found err")
		return
	}

now since i have to get values from a stored procedure...can some one suggest how to acheive this in go

答案1

得分: 1

我正在使用github.com/alexbrainman/odbc驱动程序,
执行存储过程的示例代码如下:

proc := "exec Dbo.vijaystoredprocedure ?, ?, ?, ?" //(参数的数量)
parms := []interface{}{"parm1","parm2","parm3","parm4"}// 如果需要的话,可以传入参数

if Stmt, err := DBC.Prepare(proc); err != nil {
    log.Fatal(err.Error())
} else {
    defer Stmt.Close()
    if result, err := Stmt.Exec(parms...); err != nil {
        log.Fatal(err.Error())
    }
}

执行存储函数的示例代码如下:

proc := "SELECT * From Dbo.[vijaystoredprocedure](?,?)" //(参数的数量)
parms := []interface{}{"parm1","parm2"}// 如果需要的话,可以传入参数
row, err := DBC.Query(proc, parms...)
if err != nil {
    log.Fatal("Prepare failed:", err.Error())
}

_, rows, r_err := DBScan_fn(row)

if r_err != nil {
    fmt.Println("no data found err")
    return
}

以上是示例代码,用于执行存储过程和存储函数。

英文:

I'm using github.com/alexbrainman/odbc driver,
Example of executing stored procedure:

	proc := "exec Dbo.vijaystoredprocedure ?, ?, ?, ?" //(Number of parameters)
	parms := []interface{}{"parm1","parm2","parm3","parm4"}// Parameters if needed
	
	if Stmt, err := DBC.Prepare(proc); err != nil {
		log.Fatal(err.Error())
	} else {
		defer Stmt.Close()
		if result, err := Stmt.Exec(parms...); err != nil {
			log.Fatal(err.Error())
		}
	}

Example of stored function:

proc := "SELECT * From Dbo.[vijaystoredprocedure](?,?)" //(Number of parameters)
parms := []interface{}{"parm1","parm2"}// Parameters if needed
row, err := DBC.Query(proc, parms...)
if err != nil {
    log.Fatal("Prepare failed:", err.Error())
}

_, rows, r_err := DBScan_fn(row)

if r_err != nil {
    fmt.Println("no data found err")
    return
}

huangapple
  • 本文由 发表于 2017年3月2日 20:30:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/42555612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定