I am trying to execute a stored procedure with EF and a variable WHERE clause.
What I first thought was this :
ALTER PROCEDURE SP
#WHEREClause VARCHAR(250)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #SQL Varchar(8000)
SET #SQL ='Select ...' + #WHEREClause
EXEC(#SQL)
END
Problem here EF wont recognize the selected values from the stored procedure anymore.
So I think of something like that:
ALTER PROCEDURE SP
#WHEREClause VARCHAR(250)
AS
BEGIN
SET NOCOUNT ON;
Select ... FROM ... #WHEREClause
END
Anyone got an idea?
Thanks
Markus
First of all, creating a WHERE clause like this could leave you vulnerable to SQL injection. You might be able to do it safer by using LINQ to narrow down the data rather than a dynamically built WHERE.
Another way to handle it is to use the ExecuteStoreQuery method (this is Database.SqlQuery in EF5). This will allow you to load the results of the stored procedure into a POCO object. You can also parametrize the query which might make it safer, depending on how it's constructed.
Related
If no, please suggest efficient alternatives if possible, I'll edit my question and include the source code if requested
If you want to encapsulate a SQL statement into something "callable", then put it into a function
create function get_data(p_some_value int)
returns table (some_number int, some_date date, some_value text)
as
$$
select c1, c2, c3
from some_table
where x1 = p_some_value;
$$
language sql
stable;
A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again.
Stored Procedure Syntax:
CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
If you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it.
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.
I am novice user in oracle and well I am creating a stored procedure to display data from a table, because my teaching process requires it. At first I ran my query follows.
Create or replace procedure p_ mostrar
Is
Begin
Select ID_MODULO, NOMBRE, URL, ESTADO, ICONO FROM MODULO WHERE ESTADO=1 ;
Commit;
End p_mostrar;
And he throws me the following error:
The judgment was expected INTO" After some research changed the syntax and run it as follows:
Create or replace procedure p_ mostrar (C1 out sys_refcursor)
Is
Begin Open C1 for Select ID_MODULO, NOMBRE, URL, ESTADO, ICONO
FROM MODULO
WHERE ESTADO=1 ;
Commit;
End p_mostrar;
And I think runs correctly. But now it does not know how to run the procedure. I thank you in advance and expect a prompt response. Remember, I'm learning with Oracle SQL Developer.
When you are dealing with select statement inside a stored procedure, you need to include INTO clause to the select statement to store the values in a variable. Try this
CREATE OR REPLACE PROCEDURE p_mostrar
IS
v_id_modulo modulo.id_modulo%TYPE;
v_nombre modulo.nombre%TYPE;
v_url modulo.url%TYPE;
v_estado modulo.estado%TYPE;
v_icono modulo.icono%TYPE;
BEGIN
SELECT id_modulo, nombre, url, estado, icono
INTO v_id_modulo, v_nombre, v_url, v_estado, v_icono --needed to catch the values selected and store it to declared variables
FROM modulo
WHERE estado=1 ;
Commit; -- i dont think this is necessary, you use commit statement only when you use DML statements to manage the changes made
dbms_output.put_line(v_id_modulo||' '|| v_nombre||' '||v_url||' '||v_estado||' '||v_icono); --used to display the values stored in the variables
END;
This should work:
var result refcursor
execute p_ mostrar(:result)
I am using entity framework with a stored procedure, in which I am generating query dynamically and executing that query. The stored proc query looks like:
Begin
DECLARE #Query nvarchar(MAX)
SET #Query = 'SELECT e.id, e.name, e.add, e.phno from employee'
EXEC sp_executesql #Query
End
In above sql code you can see that i am executing '#Query' variable, and that variable value can be changed dynamically.
I am able to add my stored proc in my edmx file. and then I go to model browser and say Add function import and try to Get column information it does not show anything. but when I execute my stored proc at server it returns all columns with values. Why i am not getting column information at model browser?
The model browser isn't running the stored procedure to then gather the column information from its result - it's trying to grab the column information from the underlying procedure definition using the sys tables.
This procedure, because it's dynamic, will not have an underlying definition and therefore won't be importable into the EDMX like this.
Temporarily change your stored proc to
SELECT TOP 1 e.id, e.name, e.add, e.phno from employee /* ... rest of your proc commented out */
Then add it to the EF and create the function import (it will see the columns).
Then change the proc back to how you had it above.
Try adding SET NOCOUNT ON after BEGIN.... that supresses messages that might cause it to be "confused"
I am using SQL 2008 and EF
I have following stored proc for bulk insert
CREATE Type [dbo].[xxx] as Table (
[ErrorCode] [nvarchar](10),
[ErrorMessage] [nvarchar](300),
[FieldName] [nvarchar](50),
[FieldLable] [nvarchar](300),
)
CREATE procedure dbo.InsertAll(#Records xxx READONLY)
as
begin
insert into dbo.MyTable
select * from #Records;
end;
go
I am passing a Datatable as parameter (Type=structured) that has multiple records
This proc works when called using SQLCommand.ExecuteNonQuery, but does not do anything when called using contextObject.ExecuteStoreCommand. The return value = affected rows is always 0
Whats wrong? are such procedures not supported with EF? I am not even getting any exception :(
Update: After running SQL trace just realized the difference in the SQL statements being generated
When using contextObject.ExecuteStoreCommand
declare #p3 dbo.xxx
insert into #p3 values(N'M',N'ErrorMsg - 0',NULL,NULL)
insert into #p3 values(N'M',N'ErrorMsg - 1',NULL,NULL)
insert into #p3 values(N'M',N'ErrorMsg - 2',NULL,NULL)
exec sp_executesql N'InsertAll',N'#Records [xxx]
READONLY',#Records=#p3
When using SQLCommand.ExecuteNonQuery
declare #p1 dbo.xxx
insert into #p1 values(N'M',N'ErrorMsg - 0',NULL,NULL)
insert into #p1 values(N'M',N'ErrorMsg - 1',NULL,NULL)
insert into #p1 values(N'M',N'ErrorMsg - 2',NULL,NULL)
exec InsertAll #Records=#p1
How can I get contextObject.ExecuteStoreCommand to execute the same SQL stmt like SQLCommand.ExecuteNonQuery?
I have found it necessary to provide extension methods when the Entity Framework simply will not work for what I want it to do. The good part is adding an extension to the Context object will usually keep you from having to dig into the web.config or app.config for a connection string. Then parameters and return values can be generic. I have personally seen quite a few eloquent solutions using this strategy.
How can I call sp_executesql stored procedure in entity framework?
I need to dynamically execute formulas stored in SQL Server table column using Select Statement. I tried ENTITY SQL but it does not work.
Use ObjectContext.ExecuteStoreCommand (requires EF 4).
if i understood correctly try this
CREATE PROCEDURE sp_calculatesalary(#EmployeeId as int)
begin
declare #dynsql varchar(500)=' Salary,Username from employee where EmployeeId=#empID'
exec sp_executesql #dynsql,'#empID int',#empID=#EmployeeID
SELECT 1 as salary,2 as username
end
it solves stored procedures mapping problem