How to use stored procedure in Linq-to-Entities - linq

I am working with Entity Framework
I want to know how I can use stored procedures in Linq-to-Entities. My stored procedure is called SelectEmployee and table name is Employee
For this I added code like this
databaseentity entities = new databaseentity();
var selectdata = entities.ExecuteStoreQuery<Employee>("SelectEmployee").ToList();
but it is not supported ExecuteStoreQuery
So please guide me how can I use stored procedures in Linq-to-Entities

You can check out the following two short articles
http://entityframeworktutorial.net/data-read-using-stored-procedure.aspx
http://entityframeworktutorial.net/EntityFramework4.3/execute-stored-procedure-using-dbcontext.aspx

In simple steps:
Add your stored procedure to the edmx.
You will find that sp in Model Browser
Right click on stored procedure and say Add Function Import
use it by entities.SelectEmployee()

Related

How to export data from multiple tables in a dynamic fashion in plsql

I have an plsql SP that creates multiple tables on a daily basis, however the number of tables are not always the same, however, the generated tables have a predefined pattern in its name.
I'm trying to create a plsql SP who exports those tables to a csv or excel file, based on a list input with the name of the generated tables.
Any ideas for achieving this rather than use PLSQL or is there any useful way of achieve this with it?
Thanks in advance
i think this question is very abstract and can have many solutions. Here is a solution that you can try if you want to create Excel from your data.
First you need a tool to create an excel file. Anton Scheffer has developed a package that you can use to create easily an Excel file from a cursor. Have a look: Create an Excel-file with PL/SQL.
Next, you can determine the created tables and create a query string that you can pass as parameter into query2sheet procedure of the Anton's package. All Tables you can find in user_tables View.
So your code could looks like:
for rec in (select * from user_tables where 1=1 /* or condition you need to filter the correct tables*/)
loop
-- as_xlsx - is a package created by Anton Scheffer
as_xlsx.query2sheet( 'select * from '||rec.table_name );
-- MY_DIR is a database Directory that you have to create
as_xlsx.save( 'MY_DIR', rec.table_name||'.xlsx' );
end loop;
Edit:
If you want to create a csv files, so you can use a package developed by William Robertson, Ref cursor to CSV converter.
The usage is quite similar to the Excel package.

User defined table types in Oracle

First of all usually I am working with MSSQL. But I have a stored procedure in MSSQL, which I need to use in Oracle now and since I am absolutely new to Oracle I have no idea at all how to do it correct.
I needed to use user defined table types in my MS SQL stored procedure because I am using "logical" tables in my stored procedure, which I also need to pass them to a dynamic sql statement within this procedure (using column names of "physical" tables as variables/parameters).
I've started to add the oracle function in a package I made before for another function. It looks like
TYPE resultRec IS RECORD
(
[result columns]
);
TYPE resultTable IS TABLE OF resultRec;
Function MyFunctionName([A LOT PARAMETERS]) RETURN resultTable PIPELINED;
I also described the layout of the tables (the user defined table types in MSSQL), which I want to use within this function in this package header.
So far so good, but now I don't really know where I have to declare my table variables or user defined table types. I also tried to put them in the package header, but if I am trying to use these tables in the package body, where I am describing my function, Oracle tells met, that the table or view does not exist.
I also tried it to describe the tables within the package body or in the block of my function, which looks like that:
FUNCTION MyFunctionName
(
[MyParameters]
)
RETURN resultTable PIPELINED is rec resultrec;
TYPE tableVariableA IS TABLE OF tableRecA;
TYPE tableVariableB IS TABLE OF tableRecB;
BEGIN
INSERT INTO tableVariableA
SELECT ColumnA, ColumnB FROM physicalTable WHERE[...];
[A LOT MORE TO DO...]
END;
But in this case Oracle also tells me, that it doesn't know the table or view.
I also tried a few more things, but at the end I wasn't able to tell Oracle what table it should use...
I would appreciate every hint, which helps me to understand how oracle works in this case. Thanks a lot!
You can't insert into a collection (e.g. PL/SQL table). You can use the bulk collect syntax to populate the collection:
SELECT ColumnA, ColumnB
BULK COLLECT INTO tableVariableA
FROM physicalTable
WHERE [...];
However, you might want to check this is an appropriate approach, since SQL Server and Oracle differ quite a bit. You can't use PL/SQL tables in plain SQL (at least prior to 12c), even inside your procedure, so you might need a schema-level type rather than a PL/SQL type, but it depends what you will do next. You might not really want a collection at all. Trying to convert T-SQL straight to PL/SQL without understanding the differences could lead you down a wrong path - make sure you understand the actual requirement and then find the best Oracle mechanism for that.

Passing parameters to stored procedure and then using it as data source in SSRS report

I want to do the following:
The user sets 3 parameters in MVC3 view: worker name, from date, to date.
This parameters are passed to a stored procedure, which return a table.
I want to use this table as data source for SSRS report and view the report in .aspx page.
Can you tell me how to do that?
I've already created the view and the procedure, but I don't know how to do the rest.
In your Report Builder/BIDS Environment create a new report.
Create a datasource which points to your database.
Create a new dataset which uses this datasource. In your dataset set the command type to a stored procedure and select your procedure from the drop down list. You can then add parameters to your stored procedure, i.e.: "#FromDate", "#ToDate".
You can also configure the report to prompt the user for these parameters by right clicking on the report and go to properties and adding parameters this way.
Then in ASP.NET you could direct the user to "http://yoursite.com/Reports/Report.aspx?FromDate=....&ToDate=....&WorkerName=Ivan"
I have included a few resources which may also help you achieve this.
Resources:
http://www.codeproject.com/Articles/20540/Using-StoredProcs-with-Parameters-in-SQL-Reporting
http://msdn.microsoft.com/en-us/magazine/cc188691.aspx

Using unnamed procedural code in JdbcTemplate

I have a table in an Oracle database where three columns are automatically populated by a trigger when a row in inserted. To retrieve those values when the insert happens, I am using some procedural code but it doesn't exist as a named stored procedure. The code looks something like this but with more outputs.
DECLARE widgetId NUMBER;
BEGIN INSERT INTO widget(foo) VALUES('bar')
RETURNING widget_id INTO widgetId;
END;
Does Spring JdbcTemplate provide any clean, convenient way of calling that code? I see where JdbcTemplate supports calling a stored procedure but that seems to require that the code live in the database and can be called by name. JdbcTemplate's KeyHolder seems promising but I'm not entirely sure if it can be used to retrieve an arbitrary number of values generated by a trigger. I also haven't had much luck finding anything but simple examples. Does anyone have any insight into a clean way of calling that code using JdbcTemplate?
To be called the code will have to be a stand-alone procedure or function, or will have to be an externally visible procedure or function in a package. As far as I'm aware there's no way to execute anonymous code.
Share and enjoy.

LINQ to SQL, Stored Procedures and the Methods Pane (more like Methods PAIN!)

I'm new to LINQ and am having a small issue. I created a DBML file and dragged all of my tables to the design surface. Now, when I try to drag a stored procedure to the methods pane, Visual Studio thinks a second and then doesn't do anything. My method does not show up in the methods pane.
Is some error occurring behind the scenes? If so, how can I troubleshoot it?
It's possible that the LINQ to SQL designer isn't able to figure out the schema on your stored procedure, especially if you use temporary tables. Try changing your stored procedure to just select from the table in question, map it into the designer (by dragging on top of the correct table), then change the procedure back to the original code.
I just tried this myself. I used VS2008 SP1, dragged every table from my DB into the class pane and they added. Then I dragged in a stored procedure. It did take longer to process the SP then the tables but the method showed up without an issue in my methods pane. Your stored procedure may not be able to use the metadata available to successfully create the method for it.
Did I do this right?
Jep, I had the same problem. My fault was that the stored procedure that I was trying to add to the pane had a return result with a geography datatype in it. This is not yet supported by the LinQ2SQL designer.
What you can do to test your returning datatypes (if you are using some exotic ones), is add them as parameters to your stored procedure. The designer will give an error.
For example try adding the following stored procedure to the pane will generate an error:
CREATE PROCEDURE test
(
#GeographicLocation geography
)
AS
BEGIN
END;

Resources