In SQL Server 2008, is there any way to get the total numbers of Result sets (tables) been populated after an execution of stored procedure.
Lets say I have one stored procedures which internally calls another stored procedure. I want to know that how many result sets it returns that internally called stored procedure.
Can anybody can assist me on this.
e.g.
CREATE PROCEDURE sp_GetReports
(
#reportName AS VARCHAR(50)
)
AS
BEGIN
DECLARE #reportProcName AS VARCHAR(50)
SELECT #reportProcName = ReportProcName
FROM ReportMaster
WHERE ReportName = #reportName;
EXEC (#reportProcName)
/*
* Need to get here, total numbers of Result Sets (tables) retrived.
*/
END
GO
Thanks in advance.
You might be able to:
Get the row count in the stored procedure and retrieve the result as an OUTPUT parameter
Utilize some type of external table (would have to design this)
This gives you the number of rows returned from the stored procedure
USE Northwind
GO
CREATE OR ALTER PROCEDURE dbo.Test (
#rowCnt int OUTPUT
)
AS
SELECT * FROM dbo.Customers
SET #rowCnt = ##ROWCOUNT
GO
DECLARE
#rowCount int
EXEC dbo.Test #rowCount OUTPUT
SELECT #rowCount
What about a workaround?
Create some counter variable in each report.
Every time before SELECT statement, increment it.
Then, return it as the last output in sproc.
Each report should follow this convention.
You can return an output parameter with ##ROWCOUNT from inner procedure and use it in sp_GetReports.
Related
I'm working on a stored procedure to retrieve the average of a column and back it to another stored procedure. Now I don't know how to set the value of output to the column value.
Thank you in advance :)
CREATE PROCEDURE AverageGrade
-- Add the parameters for the stored procedure here
#course_grade INT OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
PersonID, AVG(Grade) AS averagegrade
FROM
Enrollment
GROUP BY
PersonID;
SELECT #course_grade = averagegrade;
END
GO
Read what you wrote. "I don't know how to set the value ...".
Notice the key word - set. Now go look up the syntax for tsql set since that is how you should assign a value to a scalar variable.
To assign the value of your query to your variable, you use the form:
set #blah = (select ...);
Example F in the documentation provides a complete example. Go read it and learn it.
I have a transaction that calls a stored procedure which creates a temp table. I need to be able to access this temp table outside of the stored procedure after it has been ran. Note: for what I am trying to do, I cannot use global temp tables.
Example:
Here is an example of the stored procedure:
CREATE PROCEDURE [dbo].[GetChangeID]()
AS
BEGIN
IF OBJECT_ID('tempdb..#CurrentChangeID') IS NOT NULL
DROP TABLE #CurrentChangeID
SELECT '00000000-0000-0000-0000-000000000000' AS ChangeID INTO #CurrentChangeID
END
GO
Here is an example of the transaction:
BEGIN TRANSACTION
DECLARE #changeID uniqueidentifier
EXEC dbo.GetChangeID
DECLARE #test uniqueidentifier
SET #test = (SELECT ChangeID FROM #CurrentChangeID)
COMMIT TRANSACTION
GO
The issue is that it cannot find a table named #CurrentChangeID.
How can I make it to where it can see this table without declaring it as a global temp table such as ##CurrentChangeID?
------UPDATE------
So let me give more context to my question because that was just a simplified example. So what I am ultimately trying to do is this: 1. Begin Transaction 2. Call stored procedure that generates the GUID 3. Then update row in a given view that has a trigger. 4. Within that trigger get the GUID that was generated within the sp. 5. Commit.
First of all you can't get access to local temp table defined in SP outside stored procedure. It will always be out of scope.
Second you probalbly don't even need temp table. In your example:
SET #test = (SELECT ChangeID FROM #CurrentChangeID)
it looks like you want only one value.
I propose to use output parameter.
CREATE PROCEDURE [dbo].[GetChangeID](
#test UNIQUEIDENTIFIER OUTPUT
)
AS
BEGIN
-- ...
SET #test = '00000000-0000-0000-0000-000000000000';
END;
And call:
DECLARE #changeID uniqueidentifier
EXEC dbo.GetChangeID #chaneId OUTPUT;
SELECT #changeId;
Thank you lad2025 and Dan Guzman for your input. The way I was originally trying to do this was definitely incorrect.
I did, however, figure out a way to accomplish this task.
Modified Stored Procedure:
CREATE PROCEDURE [dbo].[GetChangeID]()
AS
BEGIN
DECLARE #ChangeID uniqueidentifier
...
Code that generates the uniqueidentifier, #ChangeID.
...
--This can be seen within the context of this batch.
SET CONTEXT_INFO #ChangeID
END
GO
Then anywhere within this transaction that you would like to access the changeID, you just have to use the following query:
SELECT CONTEXT_INFO as changeID
FROM sys.dm_exec_requests
WHERE session_id = ##SPID AND request_id = CURRENT_REQUEST_ID()
In TSQLT, I'm trying to return a result from a stored procedure and add it to a variable so that I can assert if it matches my expected result.
I've seen loads of examples of returning results from functions but none where a stored procedure is called.
Does anybody have examples that they could share?
Thanks in advance
If you want to get a variable back from a stored procedure one way to do this is to use an out parameter
CREATE PROC MyTest
(#myVar int output)
AS
BEGIN
SET #myVar = 10
END
GO
DECLARE #x int
EXEC MyTest #myVar=#x output
SELECT #x
If you are getting a result set back from the stored procedure, here is an example from a tSQLt test that I wrote. I haven't bothered with the whole test because this should give you what you need.
CREATE TABLE #Actual (SortOrder int identity(1,1),LastName varchar(100), FirstName varchar(100), OrderDate datetime, TotalQuantity int)
-- Act
INSERT #Actual (LastName, FirstName, OrderDate, TotalQuantity)
EXEC Report_BulkBuyers #CurrentDate=#CurrentDate
The trick here is that you have to create the #actual table first. It should contain the same columns as what is returned from the stored procedure.
Just as an aside, you may have noticed I have a SortOrder column in the #actual table. This is because I was interested in testing the order of the data returned for this specific report. EXEC tSQLt.AssertEqualsTable will match rows like for like, but does not match the order in which the rows appear in the expected and actual so the way to ensure the order is to add a SortOrder column (which is an identity column) to both the #expected and #actual
Have a look here:
http://technet.microsoft.com/en-us/library/ms188655.aspx
Lots of examples about returning values from a stored procedure. At the bottom of the page there is also an example about evaluating a return code.
its actually really simple.
declare #variable int
exec #variable = _Stored_Procedure
I have a stored procedure I don't want to modify. It's rather large and complex, and I don't want to add any more confusion to it.
So what I would like to do is have another store procedure that calls on the big one, and uses the result set to perform further selects / joins etc.
You can insert procedure's result set into table. Like this:
create procedure test
as
begin
select 1
end
go
declare #t table
(
id int
)
insert into #t
exec test
select * from #t -- returns one row
You can use a user-defined function instead:
create function table_func
()
returns table
as
return
(
select top 10 *
from master..msreplication_options
)
Then, to get your result set
select * from table_func()
If you still need to call this as a stored proc in other places, create a stored proc that wraps the user-defined function:
create procedure test_proc
as
select * from test_func();
You can create a user defined function which call the stored procedure you have and use it in other queries.
Is it possible to do this? I have some filters set in my source Stored Procedure and I really don't want to have to duplicate it in another just to get the rowcount.
The only way I know how to do this is to insert into a temp table from the stored procedure and then select the count. Unfortunately, there's no pretty way to perform a "select" on a stored procedure.
CREATE TABLE #stuff (id int, status char(6))
INSERT #stuff (id, status)
EXEC dbo.sp_get_stuff
SELECT count(*) FROM #stuff
DROP TABLE #stuff
Edit
The above method will allow you to select from a stored procedure, but as Greg pointed out, a rowcount can be simplified to:
EXEC dbo.sp_get_stuff
SELECT ##Rowcount
This also works:
create proc pTest1
as
select * from comp
go
create proc pTest2
as
exec pTest1
select ##rowcount
GO
If you are really trying to fine tune as much as possible, then you will have to change the source stored procedure. If you are looking at performance, then returning the rowset just to get the count is not something to even consider.