Posts

Showing posts from 2015

Calling SQL Server stored procedures from Entity Framework

Using the methods below, you can obtain the stored procedure return value , along with your data, which I've not seen in other blogs. A stored proc should return 0 for success, and any other value for a failure. Here is an example table, filled with data: CREATE TABLE DemoData ( id INT NOT NULL PRIMARY key, someValue DECIMAL(4,4) NOT NULL ) GO INSERT INTO DemoData(id, someValue) VALUES (1, 1.23), (2, 2.34), (3, 3.45), (4, 4.56) Here are our example stored procedures: CREATE PROCEDURE GetDemoData(@maxId INT) AS BEGIN SET NOCOUNT ON; SELECT id, someValue FROM DemoData WHERE id <= @maxId END GO CREATE PROCEDURE AddTwoValues(@a INT, @b INT) AS BEGIN SET NOCOUNT ON; RETURN @a + @b -- Don't do this. Stored procs should return -- 0 for success, and any other value for failure. END GO CREATE PROCEDURE AddTwoValuesWithResult(@a INT, @b INT, @result INT OUTPUT, @result2 INT OUTPUT) AS BEGIN SET NOCOUNT ON; SET @result = @a +...