-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
query := fmt.Sprintf( DECLARE @OutputResultCount INT; EXEC %s @YearAndMonth = :YearAndMonth, @City = :City, @Template = :Template, @saveFilePath = :FilePath, @OutputInsertedRowCount = @OutputResultCount OUTPUT;SELECT @OutputResultCount AS OutputResultCount
, storedProcedureName)
// 对于 SQL Server,通常推荐使用参数化的 EXEC 调用,避免字符串拼接带来的潜在问题
// query = fmt.Sprintf("Exec %s @FolderName=?,@City=?,@Filepath=?", storedProcedureName)
// 但你原始代码使用了具名参数,所以保持这种写法,它通常是安全的。
// 具名参数列表 (保持不变,因为参数名是固定的)
args := map[string]any{
"YearAndMonth": p.FolderName,
"City": p.City,
"Template": p.Template,
"FilePath": p.FilePath,
}
// 使用 goroutine 执行数据库导入操作,并利用 context 的取消机制
dbDone := make(chan error, 1)
var resultCount int64
go func() {
// WithTranContext 会将传入的 ctx 传递给事务操作,以便响应外部超时或取消
_, err := repo.db.WithTranContext(ctx, func(tx TxWrapper) (any, error) {
// 在事务内部执行存储过程
Count, err := tx.QueryInt(query, args)//This is the encapsulation of the GET method of sqlx
if err != nil {
// 返回一个更具体的错误信息,包含存储过程名称和原始错误
return nil, fmt.Errorf("执行存储过程 %s 失败: %w", storedProcedureName, err)
}
resultCount = Count
return nil, nil
})
dbDone <- err
}()
When I execute the stored procedure, a primary key conflict occurs, but sqlx does not catch the exception and the exception will be overwritten by the subsequent query statement. Can this problem be solved?If you know how to solve it, please contact us by email at [email protected]
Metadata
Metadata
Assignees
Labels
No labels