我在弄清楚为什么我不能使用go-mssqldb驱动程序中定义的参数执行存储过程时遇到了麻烦。如果有人会指出我在定义作为参数传递的值时出错了,这将导致从Microsoft sql Server数据库接收错误“ mssql:将十进制数据类型转换为nvarchar时出错”。在Go中使用go-mssqldb会非常感激!
我在下面的存储过程中包括了我正在使用的代码,甚至包括参数定义。
执行代码:
this.physics.add.sprite(...)
sql存储过程参数:
package main
import (
"context"
"database/sql"
"flag"
"fmt"
_ "github.com/denisenkom/go-mssqldb"
"github.com/shopspring/decimal"
"log"
)
var (
debug = flag.Bool("debug", true, "enable debugging")
userid = flag.String("U", "user", "the database user")
password = flag.String("P", "password", "the database password")
server = flag.String("S", "address", "the database server")
port *int = flag.Int("port", 1433, "the database port")
database = flag.String("d", "database", "the database")
encrypt = flag.String("e", "disable", "the encryption flag")
)
func main() {
flag.Parse()
if *debug {
fmt.Printf(" port:%d\n", *port)
fmt.Printf(" server:%s\n", *server)
fmt.Printf(" user:%s\n", *userid)
fmt.Printf(" database:%s\n", *database)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
connString := fmt.Sprintf("server=%s;port=%d;user id=%s;password=%s;database=%s;encrypt=%s;", *server, *port, *userid, *password, *database, *encrypt)
if *debug {
fmt.Printf(" connString:%s\n", connString)
}
db, err := sql.Open("sqlserver", connString)
if err != nil {
log.Fatal("Open connection failed:", err.Error())
}
defer db.Close()
// bigint
WarehouseID := 1
// nvarchar(7)
TaxCode := "Vertex"
// nvarchar(max)
LineItemTotals := "16.35"
// decimal(19,5)
TotalTax, _ := decimal.NewFromString("00.00")
// nvarchar(50)
ShipToAddress1 := "Address1"
// nvarchar(50)
ShipToAddress2 := "Address2"
// nvarchar(40)
ShipToCity := "Seattle"
// nvarchar(5)
ShipToState := "WA"
// nvarchar(10)
ShipToZip := "zip"
// nchar(3)
ISOCountryCode := "USA"
// decimal(19,5)
FreightCharge, _ := decimal.NewFromString("12.10")
// bigInt
CustomerID := 456118
// bigint
Timeout := 5000
rows, err := db.QueryContext(ctx, "EAPI_CalculateTax",
sql.Named("WarehouseID", WarehouseID),
sql.Named("TaxCode", TaxCode),
sql.Named("LineItemTotals", LineItemTotals),
sql.Named("ShipToAddress1", ShipToAddress1),
sql.Named("ShipToAddress2", ShipToAddress2),
sql.Named("ShipToCity", ShipToCity),
sql.Named("ShipToState", ShipToState),
sql.Named("ShipToZip", ShipToZip),
sql.Named("ISOCountryCode", ISOCountryCode),
sql.Named("FreightCharge", FreightCharge),
sql.Named("CustomerID", CustomerID),
sql.Named("Timeout", Timeout),
sql.Named("TotalTax", sql.Out{Dest: &TotalTax}),
)
if err != nil {
log.Fatal("Prepare failed: ", err.Error())
}
defer rows.Close()
var strrow string
for rows.Next() {
err = rows.Scan(&strrow)
}
fmt.Printf("TotalTax is %d", TotalTax)
}
编辑:
我在errorx库中添加了一个更好的堆栈跟踪。堆栈跟踪如下:
@WarehouseID bigint,
@TaxCode nvarchar(7),
@LineItemTotals nvarchar(max),
@TotalTax decimal(19,5) OUTPUT,
@ShipToAddress1 nvarchar(50) = '',
@ShipToAddress2 nvarchar(50) = '',
@ShipToCity nvarchar(40) = '',
@ShipToState nvarchar(5) = '',
@ShipToZip nvarchar(10) = '',
@ISOCountryCode nchar(3) = 'USA',
@FreightCharge decimal(19,5) = 0,
@CustomerID bigInt = 0,
@Timeout bigint = 0
答案 0 :(得分:1)
检查这是否是订单问题。
含义:尝试使用db.QueryContext(ctx,
参数调用sql.Named()
,这些参数的定义顺序与您的sql存储过程参数的定义顺序相同。