sqlx + Go:存储过程中的输出参数

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

sqlx + Go: Output parameter in stored procedure

问题

我有一个包含输出参数的 MSSQL 存储过程,可以像这样调用:

EXEC [SP_NAME]
    @Input = N'Some Text',
    @Result = @Result OUTPUT

你知道在使用 jmoiron/sqlx 调用存储过程后是否可以访问 @Result 参数吗?

英文:

I have mssql stored procedure that contains output parameter and can be called like this

EXEC [SP_NAME]
	@Input = N'Some Text',
	@Result = @Result OUTPUT

Do you know if it's possible to access this @Result in jmoiron/sqlx after sp call?

答案1

得分: 4

sqlx只是database/sql包的一个包装器。你尝试执行的查询是特定于驱动程序的。因此,它取决于你使用的连接到MSSQL服务器的驱动程序。如果你使用的是https://github.com/denisenkom/go-mssqldb,根据问题#78,你可以尝试以下代码:

rows, err := db.Queryx(`EXEC [SP_NAME] @Input, @Result OUTPUT; 
          SELECT Result = @Result;`, sql.Named("Input", "Some Text"))
//对rows进行处理...

这段代码使用了db.Queryx方法来执行查询,并传递了一个包含输入参数和输出参数的SQL语句。你可以根据自己的需求修改[SP_NAME]@Input@Result的值。

英文:

sqlx is just a wrapper around database/sql package. The query you're trying to perform is driver specific. Thus, it depends on the driver you're using to connect to the MSSQL server. If you're using https://github.com/denisenkom/go-mssqldb, based on issue #78, you can try the following:

rows, err := db.Queryx(`EXEC [SP_NAME] @Input, @Result OUTPUT; 
          SELECT Result = @Result;`, sql.Named("Input", "Some Text"))
//do something with rows...

huangapple
  • 本文由 发表于 2017年7月20日 15:52:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/45208654.html
匿名

发表评论

匿名网友

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

确定