Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added README comment about safe use of OUTPUT parameters #398

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ _, err := db.ExecContext(ctx, "sp_RunMe",
)
```

When running a stored procedure which returns rows, do not use variables assigned as OUTPUT parameters until all rows have been read.
This is necessary because SQL Server only sends OUTPUT parameters after all rows have been returned.

```go
var updated time.Time
rows, err := db.QueryContext(ctx, "sp_RunMeQuery",
sql.Named("ID", 123),
sql.Named("Updated", sql.Out{Dest: (*mssql.DateTime1)(&updated)}),
)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
// Iteratively read all the rows, calling rows.Scan() as appropriate.
}
if err = rows.Err(); err != nil {
return err
}
// It is now safe to use the variable assigned as the OUTPUT parameter
log.Println("Updated at:", updated)

```

## Statement Parameters

The `sqlserver` driver uses normal MS SQL Server syntax and expects parameters in
Expand Down