denisenkom / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
1.82k stars 495 forks source link

how to get the update result? #668

Closed hopestar closed 3 years ago

hopestar commented 3 years ago

hello ~ could you tell me how to get the update result? the query like "update table set xx=1 WHERE id=7" i using rows.Next() , but it always return false. how to get the result like rows.RowsAffected ? thanks.

kardianos commented 3 years ago

Learn T-SQL a bit better. Please review the docs for the update clause: https://docs.microsoft.com/en-us/sql/t-sql/queries/update-transact-sql?view=sql-server-ver15

What you want is:

update t set
   xx=1
output
  inserted.*
from
  table t
where
  t.id = 7
;
tc-hib commented 3 years ago

Hello,

I think what you are looking for is the Exec method. Use it instead of Query.

Please be aware that LastInsertId cannot function properly with SQL Server. You would need to use Query again, and an OUTPUT clause or a second statement:

INSERT ...
;SELECT SCOPE_IDENTITY()
hopestar commented 3 years ago

Learn T-SQL a bit better. Please review the docs for the update clause: https://docs.microsoft.com/en-us/sql/t-sql/queries/update-transact-sql?view=sql-server-ver15

What you want is:

update t set
   xx=1
output
  inserted.*
from
  table t
where
  t.id = 7
;

yes, thank you very much ~

dzsquared commented 3 years ago

@hopestar - if you're all set here, can you make the issue closed?