Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling stored procedures with parameters in PetaPoco

Tags:

I want to be able to call a stored proc with named parameters in PetaPoco.

In order to call a stored proc that does a search/fetch:

Can I do something like this:

return db.Fetch<Customer>("EXEC SP_FindCust", new SqlParameter("@first_name", fName), new SqlParameter("@last_name", lName), new SqlParameter("@dob", dob)); 

Also, how can I call a stored proc that does an insert?

return db.Execute("EXEC InsertCust @CustID = 1, @CustName = AAA") 

Thanks, Nac

like image 375
Tech Xie Avatar asked Aug 05 '11 01:08

Tech Xie


People also ask

How do you call a stored procedure with parameters?

Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.

Can stored procedures accept parameters?

You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed.

How do you call in & out parameters for stored procedures?

The easy way is to right-click on the procedure in Sql Server Management Studio (SSMS), select 'Execute stored procedure..." and add values for the input parameters as prompted. SSMS will then generate the code to run the procedure in a new query window, and execute it for you.

How do you call stored procedure?

You can call an SQL stored procedure with the execute, open, or get statement; in each case, you use the #sql directive. A stored procedure is a set of instructions for a database, like a function in EGL.


1 Answers

Update:

I tried the following for fetch and insert and it worked perfectly:

var s = PetaPoco.Sql.Builder.Append("EXEC SP_FindCust @@last_name = @0", lname); s.Append(", @@first_name = @0", fName); s.Append(", @@last_name = @0", lName); s.Append(", @@dob = @0", dob); return db.Query<Cust>(s); 

This can be improved further to pass SQL parameters.

like image 192
Tech Xie Avatar answered Sep 24 '22 21:09

Tech Xie