Stored procedure mapped to Entity ok, but getting additional columns? - stored-procedures

I have a stored procedure mapped ok in the EDMX to retrieve a ObjectResult<> of entities.
However I'm trying to get an additional column from the stored procedure that is the total rows.
I'm using this http://forums.asp.net/post/3000198.aspx variantion to get the total rows, which is working nicely. So I don't particularly want to use output parameters.
Is there anyway of both mapping the entity and getting ahold of the additional columns?
I'm using EF 4.3.1 database first, so I'm taking a look at the generated context file.
I think the trouble is that ObjectContext.ExecuteFunction is strongly typed, so I'm unsure how to get the additional columns. Is there perhaps a way of executing the stored procedure directly and get the mapping?

Related

How to Store an Aggregated Value in a Stored Procedure?

I am using SQL Server 2012 and trying to write a stored procedure that will have two components:
1. Select statement that will return several rows
2. Another component will have an aggregated value based on some conditions.
So, for the second component, I am thinking to declare a variable in the stored procedure and store the aggregated data there.
Now, my question is can I populate my output parameter with the data stored in previously declared variable and access the parameter from SSRS 2012?
I would appreciate any suggestion(s) to get me started.
Thank you

db2 delete from result set in stored procedure

I have written a stored procedure that includes a SELECT on a number of tables that applies logic to calculate values and transforms some of the data.
I have been asked if I can exclude records from the resultset in the stored procedure and write the record to a separate log table. I was looking to loop through the result set from the SELECT statement and delete the record I want to exclude once I have written it to a table. At the moment I am struggling to find the syntax to delete from the result set of a SELECT statement in a stored procedure and can only find how to use the cursor reference to delete from the original database table.
I need to remove the records in the same stored procedure and I am looking to avoid duplicating the logic by using some of the logic to find the records to include and repeat some of the logic again to be able to find the records to exclude. The only other alternative I can think of is using a temporary table, but I think what I am trying to do should be possible.
Any help appreciated.
When you have an open cursor in a stored procedure (or in an application), you can perform positioned deletes. You can execute the statement,
DELETE WHERE CURRENT OF cursorname;
Please be aware that by default issuing a COMMIT statement will close any open cursors, so if you plan to have this delete operation spread over multiple transactions you will need to declare your cursors using WITH HOLD.

Avoiding round-trips when importing data from Excel

I'm using EF 4.1 (Code First). I need to add/update products in a database based on data from an Excel file. Discussing here, one way to achieve this is to use dbContext.Products.ToList() to force loading all products from the database then use db.Products.Local.FirstOrDefault(...) to check if product from Excel exists in database and proceed accordingly with an insert or add. This is only one round-trip.
Now, my problem is there are two many products in the database so it's not possible to load all products in memory. What's the way to achieve this without multiplying round-trips to the database. My understanding is that if I just do a search with db.Products.FirstOrDefault(...) for each excel product to process, this will perform a round-trip each time even if I issue the statement for the exact same product several times ! What's the purpose of the EF caching objects and returning the cached value if it goes to the database anyway !
There is actually no way to make this better. EF is not a good solution for this kind of tasks. You must know if product already exists in database to use correct operation so you always need to do additional query - you can group multiple products to single query using .Contains (like SQL IN) but that will solve only check problem. The worse problem is that each INSERT or UPDATE is executed in separate roundtrip as well and there is no way to solve this because EF doesn't support command batching.
Create stored procedure and pass information about product to that stored procedure. The stored procedure will perform insert or update based on the existence of the record in the database.
You can even use some more advanced features like table valued parameters to pass multiple records from excel into procedure with single call or import Excel to temporary table (for example with SSIS) and process them all directly on SQL server. As last you can use bulk insert to get all records to special import table and again process them with single stored procedures call.

Should parameters to SP's of SqlDataSources be ordered identically?

I'm writing some stored procedures to do CRUD operations against some tables in a SQL Server database, which will be used in a FormView on an ASP.NET 2.0 page. I've already written the hardest one, which is the insert SP. Now I'm going to work on the select, update and delete SP's. What I'd like to know is, do the parameters to the SP's that are used by the SqlDataSource have to be in exactly the same order? For example, the insert operation requires about a dozen parameters, all of which are stored into the 3 tables that the insert handles. However, to retrieve the same data, all I need is the primary keys, which are just 2 parameters. Do I need to provide all of the parameters, in the same order, as I've specified for the insertion stored procedure?
No. Each command will have separate parameter collections SqlDataSource.DeleteParameters
SSMS tools pack has a CRUD generator that might be useful too

MSSQL2000: Using a stored procedure results as a table in sql

Let's say I have 'myStoredProcedure' that takes in an Id as a parameter, and returns a table of information.
Is it possible to write a SQL statement similar to this?
SELECT
MyColumn
FROM
Table-ify('myStoredProcedure ' + #MyId) AS [MyTable]
I get the feeling that it's not, but it would be very beneficial in a scenario I have with legacy code & linked server tables
Thanks!
You can use a table value function in this way.
Here is a few tricks...
No it is not - at least not in any official or documented way - unless you change your stored procedure to a TVF.
But however there are ways (read) hacks to do it. All of them basically involved a linked server and using OpenQuery - for example seehere. Do however note that it is quite fragile as you need to hardcode the name of the server - so it can be problematic if you have multiple sql server instances with different name.
Here is a pretty good summary of the ways of sharing data between stored procedures http://www.sommarskog.se/share_data.html.
Basically it depends what you want to do. The most common ways are creating the temporary table prior to calling the stored procedure and having it fill it, or having one permanent table that the stored procedure dumps the data into which also contains the process id.
Table Valued functions have been mentioned, but there are a number of restrictions when you create a function as opposed to a stored procedure, so they may or may not be right for you. The link provides a good guide to what is available.
SQL Server 2005 and SQL Server 2008 change the options a bit. SQL Server 2005+ make working with XML much easier. So XML can be passed as an output variable and pretty easily "shredded" into a table using the XML functions nodes and value. I believe SQL 2008 allows table variables to be passed into stored procedures (although read only). Since you cited SQL 2000 the 2005+ enhancements don't apply to you, but I mentioned them for completeness.
Most likely you'll go with a table valued function, or creating the temporary table prior to calling the stored procedure and then having it populate that.
While working on the project, I used the following to insert the results of xp_readerrorlog (afaik, returns a table) into a temporary table created ahead of time.
INSERT INTO [tempdb].[dbo].[ErrorLogsTMP]
EXEC master.dbo.xp_readerrorlog
From the temporary table, select the columns you want.

Resources