简体   繁体   English

如何使用 gorm 包从 golang 调用 postgres 存储过程

[英]How to call postgres stored procedure from golang using gorm package

I was working on a golang project, I've used a postgres database to store data with some Stored Procedure.我正在做一个golang项目,我使用postgres数据库来存储一些存储过程的数据

I used github.com/jinzhu/gorm for connecting to the database.我使用 github.com/jinzhu/gorm 连接到数据库。

I used below query to retrieve data.我使用以下查询来检索数据。 I know in postgres we are unable to use select, so I only tried insert code in SP.我知道在 postgres 中我们无法使用 select,所以我只尝试在 SP 中插入代码。

db.Database.Raw("CALL mydatabase.mystoredprocedure('" + param1 + "','" + param2 + "')")

db.Database.Raw("SELECT * FROM table1").Scan(&tableValue)

But here I'm only able to call a SELECT statement, not able to call the stored procedure.但是这里我只能调用SELECT语句,不能调用存储过程。

Please, can any one help me to solve this problem?请问有人能帮我解决这个问题吗?

Thanks in advance提前致谢

db.Database.Raw(...) alone does not do anything, it needs to be chained with Scan . db.Database.Raw(...)单独没有任何作用,它需要与Scan链接。 If you are not expecting any results, use Exec :如果您不期待任何结果,请使用Exec

db.Database.Exec("CALL mydatabase.mystoredprocedure($1, $2)", param1, param2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM