简体   繁体   中英

RepoDB with SQL Server's OUTPUT clause

How can I access the contents of an OUTPUT clause with RepoDB, eg

INSERT INTO MyTable(Name)
OUTPUT INSERTED.ID
VALUES ('TheName')

An DML with an OUTPUT clause looks to the client like a SELECT . So it looks like ExecuteQuery would be the correct API.

Use the ExecuteScalar extension method like below (only if you are inserting single row).

var result = connection.ExecuteScalar("INSERT INTO MyTable(Name) OUTPUT INSERTED.ID VALUES (@Name);", new { Name = "TheName" });

Or using the typed-result.

var result = connection.ExecuteScalar<int>("INSERT INTO MyTable(Name) OUTPUT INSERTED.ID VALUES (@Name);", new { Name = "TheName" });

If you are inserting multiple rows, please use the ExecuteQuery method. The result would be a model (of type IEnumerable<T> ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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