sql stored procedure clear resultsets - sql-server

Can I clear the multiple result sets that a stored procedure has already gathered so that the next result set that is created becomes the very first result set returned by the procedure?

this would depend on the Database. In Sql Server, once the result set is sent, it is gone. The receiving application/code must deal with it. If you need to have logic like this, gather the results into a temp table and only return what is needed at the end of the procedure.

As KM said it depends a bit on the database. Can you explain how your stored procedure gather multiple result sets? Are you achieving this via multiple unions or by creating a dynamic sql statement ?

Related

How can a Data Factory see the result set of a SQL Server stored procedure

I've only been at Data Factory for a short while and so far, looks OK. But I could hardly believe it when I found (apparently) that I cannot get at the result set of a SQL Server stored procedure. Is this really true? It looks like I might have to execute that stored procedure and get it to put the output into a (permanent!) table which I can read. Crazy?
Yes, this is by design, you cannot view the actual output of the stored procedure results, instead you just see the activity execution results.
You can add your points to support this similar idea already logged here Logging support for Store Procedure Pipeline

Stored Procedure Results and Ad-Hoc Query Results in Single Request?

I'm using plain ADO.NET to returns results from my SQL Server database.
I have a control that requires two sets of results. To make the control efficient, my plan was to send two queries in a single request to avoid multiple trips to the database, and use SqlDataReader.NextResult() to access the second set of results.
However, looking at the code, I see the first query is actually calling a stored procedure.
Is there any way to use ADO.NET to request the results from both a stored procedure and an ad-hoc query in a single request?
Yes you can, and exactly in the way you suggest. Set CommandType to Text, and CommandText to:
exec YourProcedure;
select * from YourTable;
You can use NextResult two move to the next resultset.

How to reduce time for taken by stored Procedure for inserting a single record?

We wrote a stored procedure for selecting a single record from DB at a time. Using the same stored procedure to read 2000 records it takes 4 seconds. Is there any way to optimize it? (like single stored procedure for 2000 records)
Here is my Stored procedure:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[GetItemValue](#ItemName VARCHAR(200),#TimeStamp as DATETIME)
AS
select * from EIItemData
where ItemName=#ItemName AND TimeStamp=#TimeStamp
Can anyone tell me how can this stored procedure can be optimized to read 2000 records instead of calling once for each item.
SQL Query Analyzer
http://www.developer.com/db/article.php/3418031/Query-Analyzer-Tips-and-Tricks.htm
Stored Procedure Optimization
http://www.mssqlcity.com/Tips/stored_procedures_optimization.htm
How-to-Optimize-Queries
http://www.serverwatch.com/tutorials/article.php/2175621/How-to-Optimize-Queries-Theory-an-Practice.htm
last but not least Index, Index, Index...
Consider that SQL Server (and all RDBMS) work best with set-based operations.
Consider changing your calling code (the client) to expect a set of records to read.
Please post your stored procedure, either reading or inserting, so we can help find the best solution!
Looking at your SQL, I'd say, hit up the following points:
Do not use select * syntax, just bring back columns you need.
I am guessing you do not have an index on the columns in the where clause. Think about adding them.
If you need 2000 items, why not just bring them in one shot.

Returning a subset of a stored procedure

I have an application that (unfortunately) contains a lot of its business logic is stored procedures.
Some of these return masses of data. Occassionally the code will need a small amount of the data returned from the stored procedure. To get a single clients name, I need to call a stored procedure that returns 12 tables and 950 rows.
I am not able (due to project politics) to change the existing stored procedures or create a replacement stored procedure - the original massive procedure must be called as that contains the logic to find the correct client. I can create a new procedure as long as it uses the original massive procedure.
Is there anyway I can get SQL server to return only a subset, (a single table, or even better a single row of a single table) of a stored procedure?
I have to support sql server 2000 +
It is not possible to conditionally modify the query behaviour of a procedure whose source code you cannot change.
However, you can create a new procedure that calls the original then trims down the result. A SQL 2000 compatible way of doing this might be:
declare #OriginalResult table (
// manually declare every column that is returned in the original procedure's resultset, with the correct data types, in the correct order
)
insert into #OriginalResult execute OriginalProcedure // procedure parameters go here
select MyColumns from #OriginalResult // your joins, groups, filters etc go here
You could use a temporary table instead of a table variable. The principle is the same.
You will definitely pay a performance penalty for this. However, you will only pay the penalty inside the server, you will not have to send lots of unnecessary data over the network connection to the client.
EDIT - Other suggestions
Ask for permission to factor out the magic find client logic into a separate procedure. You can then write a replacement procedure that follows the "rules" instead of bypassing them.
Ask whether support for SQL 2000 can be dropped. If the answer is yes, then you can write a CLR procedure to consume all 12 resultsets, take only the one you want, and filter it.
Give up and call the original procedure from your client code, but find a way of measuring the performance drop, so that you can exert some influence on the decision-making backed up with hard data.
No, you can't. A stored procedure is a single executable entity.
You have to create a new stored proc (to return what you want) or modify the current one (to branch) if you want to do this: project politics can not change real life
Edit: I didn't tell you this...
For every bit of data you need from the database, call the stored procedure each time and use the bit you want.
Don't "re-use" a call to get more data and cache it. After all, this is surely the intention of your Frankenstein stored procedure to give a consistent contract between client and databases...?
You can try to make SQL CLR stored procedure for handle all tables returned by your stored procdure and
in C# code to find data you need and return what you need. But I think that is just is going to make things more complicated.
When you fill your dataset with sored procedure which return more results sets in data set you get for each
result set one DataTable.

grabbing first result set from a stored proc called from another stored proc

I have a SQL Server 2005 stored proc which returns two result sets which are different in schema.
Another stored proc executes it as an Insert-Exec. However I need to insert the first result set, not the last one. What's a way to do this?
I can create a new stored proc which is a copy of the first one which returns just the result set I want but I wanted to know if I can use the existing one which returns two.
Actually, INSERT..EXEC will try to insert BOTH datasets into the table. If the column counts match and the datatype can be implicitly converted, then you will actually get both.
Otherwise, it will always fail because there is no way to only get one of the resultsets.
The solution to this problem is to extract the functionality that you want from the called procedure and incorporate it into the (formerly) calling procedure. And remind yourself while doing it that "SQL is not like client code: redundant code is more acceptable than redundant data".
In case this was not clear above, let me delineate the facts and options available to anyone in this situation:
1) If the two result sets returned are compatible, then you can get both in the same table with the INSERT and try to remove the ones that you do not want.
2) If the two result sets are incompatible then INSERT..EXEC cannot be made to work.
3) You can copy the code out of the called procedure and re-use it in the caller, and deal with the cost of dual-editing maintenance.
4) You can change the called procedure to work more compatibly with your other procedures.
Thats it. Those are your choices in T-SQL for this situation. There are some additional tricks that you can play with SQLCLR or client code but they will involve going about this a little bit differently.
Is there a compelling reason why you can't just have that first sproc return only one result set? As a rule, you should probably avoid having one sproc do both an INSERT and a SELECT (the exception is if the SELECT is to get the newly created row's identity).
Oo to prevent code from getting out of synch between the two processes, why not write a proc that does what you want to for the insert, call that in your process and have the orginal proc call that to get the first recordset and then do whatever else it needs to do.
Depending on how you get to this select, it is possible it might be refactored to a table-valued function instead of a proc that both processes would call.

Resources