How to know total record return stored procedure in sql? - sql

I have a stored procedure called usp_getTotalOrder which looks like
Select * from Order where CompanyID = 1;
Now, I have a table which contains stored procedure names.
In my BLL I have a stored procedure name. I want to create function which contain one parameter StoredProcedureName and returns the count of total rows which is like
Declare #str varchar(50)
Set #str='GetOrders'; // Stored Procedure Name
Exec #str
But it does not return total row count that I want to get from the stored procedure with its name in the function.
Any IDEA???
Please help.....

Take a look at ##ROWCOUNT
http://technet.microsoft.com/en-us/library/ms187316.aspx
You might be able to use something like:
Declare #str varchar(50)
Set #str='GetOrders'; // Stored Procedure Name
Exec #str
SELECT ##ROWCOUNT

Do you want to count the number of rows in the result set from the sproc?
In that case you could do:
INSERT INTO #Orders
Exec #str
SELECT COUNT(*)
FROM #Orders
or you could use a Table variable instead of the temp table.

You can return the count of rows by output parameter or return value from your SP. Of course you have to add that output parameter to your SP and set it with COUNT(*) inside the SP.
DECLARE #o_count INT
EXEC GetOrder #o_count = #count OUT

Related

SQL return the output of SELECT and not the output of another stored procedure

How do I return the result of the SELECT as the output of the stored procedure? Sorry I'm new to stored procedures!
In this query below I'm calling stored procedure spCuExt_ExtractLog and assigning the result to variable #StartDate. I then use this variable within the main stored procedure, in a SELECT statement. I need to return only the result of the SELECT statement from the main stored procedure:
-- Main stored procedure
BEGIN
DECLARE #StartDate DATETIME
EXEC #StartDate = spCuExt_ExtractLog 'Customers'
SELECT Id, [Name], LogoPath, IsDeleted
FROM dbo.Customers
WHERE RecordCreatedDateUTC>= #StartDate
END
This returns the result of the call to spCuExt_ExtractLog as well as the result of the SELECT statement but I want to output the result of the SELECT only.
How do I do this?
Put the results into a table variable instead:
create procedure dbo.usp_Child
as
begin
select N'Hello world!' as [message];
end;
go
create procedure dbo.usp_Main
as
begin;
declare #results table ([message] nvarchar(max));
insert into #results
execute dbo.usp_Child;
select N'success';
end;
go
execute dbo.usp_Main;
Here's a link to a pretty good document explaining all the different ways to solve your problem (although a lot of them can't be used since you can't modify the existing stored procedure.)
http://www.sommarskog.se/share_data.html

SQL - Save results from stored procedure to a variable

I have a stored procedure that I cannot change. And it returns a select value (integer).
It insert some values and return the new id - the last line of the stored procedure:
SELECT #newID
I am using the new ID but I don't want to display that value when I run the stored procedure.
So I use the following:
DECLARE #test INT
EXEC #test = Pr_My_Procedure 'NewGroupName'
SELECT #test
This executes successfully, but my variable #test is always = 0 when the actual value created and return for the stored procedure is an actual id number.
Why is my variable is not being assigned?
I have a work around (which is not really what I wanted but it works:
1 DECLARE #NewGroupID INT
2 EXEC Pr_My_Procedure 'NewGroupName'
3 SET #NewGroupID = (SELECT GroupID FROM DB..Groups WHERE GroupName = 'NewGroupName')
This works, but when running the stored procedure, I still get the GroupID generated being return on the results (line 2). I don't want to see that.
Is it possible to NOT return that select result from the procedure?
Thanks all for any advise!
When you do
exec #p = StoredProc
... you are dealing with the return value of the proc, not the result set of the proc. The default return value of a procedure is zero so that if you do not do an explicit RETURN in your procedure, it returns 0.
You can change the procedure to RETURN your ID, or add an output parameter.
-- Procedure which RETURNS the ID
create procedure StoredProc_WithReturn
as
return 10;
go
-- Procedure which uses output parameter instead.
create procedure StoredProc_WithOutput
#p int output
as
select #p = 12;
go
-- Now test both ways of returning a value
declare #p int = 0,
#po int = 0;
exec #p = StoredProc_WithReturn;
select #p;
exec StoredProc_WithOutput #po output;
select #po;
Since you cannot change the procedure, you can insert the result set of the procedure into a table, temporary table or table variable like so
create procedure StoredProc_WithSelect
as
select 10;
go
declare #pt table (a int);
insert #pt
exec StoredProc_WithSelect;
select a from #pt;
If you don't see result, you can use PRINT instead of SELECT. Try this:
DECLARE #test INT
EXEC #test = Pr_My_Procedure 'NewGroupName'
PRINT #test

Get scalar value from SELECT statement in stored proc, from within a stored proc

I know the preferred method for returning scalar values from stored procs is either using RETURN or an OUTPUT parameter. But lets say that I have a stored proc that returns the value using a select statement:
CREATE PROC spReturnNumber AS
SELECT 1
Is it possible to get this value from within another stored proc?
CREATE PROC spCheckNumber AS
EXEC spReturnNumber -- <-- get the return value here?
Clarification: I need a solution that doesn't require using an OUTPUT parameter, or using RETURN to return the value.
Thanks in advance.
You could use insert-exec to store the result of a stored procedure in a table:
declare #t table (col1 int)
insert #t exec spReturnNumber
return (select col1 from #t)
The definition of the table has to match the result set of the stored procedure.
Use an OUTPUT parameter instead of (or in addition to, if this procedure is used by other applications) the SELECT.
ALTER PROCEDURE dbo.spReturnNumber
#Number INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SET #Number = 1;
SELECT #Number;
END
GO
CREATE PROCEDURE dbo.spCheckNumber
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Number INT;
EXEC dbo.spReturnNumber #Number = #Number;
SELECT #Number;
END
GO
If you can't change the original procedure, but you know its output will remain static, you could use a #temp table.
CREATE PROCEDURE dbo.spCheckNumber
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #n(i INT);
INSERT #n(i) EXEC dbo.spReturnNumber;
DECLARE #Number INT;
SELECT #Number = i FROM #n;
END
GO
You can't get the SELECT value from "parent" procedure but you can get the return value like this:
CREATE PROC A AS
BEGIN
DECLARE #ret int
EXEC #ret = spReturnNumber
RETURN #ret
END
If you are unable to change the proc being called .. place the result set in a temp table [or table variable]:
CREATE TABLE #results (val INT)
DECLARE #someval int
INSERT #results
EXEC dbo.spCheckNumber
SELECT #someval =val from #results

SQL Server Stored Procedure store return value

Helo,
My question is I have one Stored Procedure in SQL Server that returns counts of a field. I want to store the results of this Stored Procedure in a variable (scalar?) of a different stored procedure.
sp_My_Other_SP:
CREATE PROCEDURE [dbo].sp_My_Other_SP
#variable int OUTPUT -- The returned count
AS
BEGIN -- SP
SET NOCOUNT ON;
SET #SQL = "SELECT COUNT(*) FROM blah"
EXEC(#SQL)
END -- SP
I currently do it like:
DECLARE #count int
EXEC sp_My_Other_SP #count OUTPUT
Then I use it like
IF (#count > 0)
BEGIN
...
END
However its returning the other Stored Procedure results as well as the main Stored Procedure results which is a problem in my .NET application.
-----------
NoColName
-----------
14
-----------
MyCol
-----------
abc
cde
efg
(Above is an attempted representation of the results sets returned)
I would like to know if there is a way to store the results of a Stored Procedure into a variable that doesn't also output it.
Thanks for any help.
You can capture the results of the stored procedure into a temp table so it is not returned by the calling stored procedure.
create table #temp (id int, val varchar(100))
insert into #temp
exec sp_My_Other_SP #value, #value, #value, #count OUTPUT
Well, the easiest way to fix this is to recode the stored proc so that the select statement that returns the 'other' result set you don't want in this case is conditionally extecuted, only when you are NOT asking for the count
Add another parameter called #GetCount
#GetCount TinyInt Defualt = 0 // or
#GetCount Bit Default = 0
Then
instead of just
Select ...
write
If #GetCount = 1
Select ...
Have you tried changing
SET #SQL = "SELECT COUNT(*) FROM blah"
EXEC(#SQL)
to
SELECT #variable = COUNT(*) FROM blah"
-- don't do EXEC(#SQL)
?
THE FIRST PROCEDURE:
CREATE PROC DD43
#ID INT OUTPUT AS
(SELECT #ID=COUNT(*) FROM CS2)
SECOND PROCEDURE:
CREATE PROC DD45 AS
DECLARE #COUNT INT
DECLARE #COUN INT
EXEC DD43 #COUN OUT --CALLING THE FIRST PROCEDURE
SET #COUNT= (SELECT #COUN)
SELECT #COUNT
EXEC DD45

How to query from a stored procedure in SQL Server?

Let say I have a simple Stored Procedure:
ALTER PROCEDURE [dbo].[myProc]
AS
BEGIN
SELECT * FROM myTable
END
How can I do a WHERE statement in Microsoft SQL Server Management Studio to the stored procedure? Something like that:
SELECT * FROM myProc WHERE x = 'a'; -- But that doesn't work...
It sounds like you're trying to make a "dynamic" stored procedure.
Something you might want to do is:
1) Insert the contents of your stored procedure into a temporary table
2) Use dynamic sql to apply a where condition to that temporary table.
Something like:
declare #as_condition varchar(500); --Your condition
create table #a
(
id bigint
)
insert into #a
execute sproc
declare #ls_sql varchar(max);
set #ls_sql = "select * from #a where " + #as_condition;
execute (#ls_sql);
SQL Server allows you to use INSERT INTO to grab a stored procedure's output. For example, to grab all processes with SPID < 10, use:
create table #sp_who (
spid smallint,
ecid smallint,
status nchar(30),
loginame nchar(128),
hostname nchar(128),
blk char(5),
dbname nchar(128),
cmd nchar(16),
request int)
insert into #sp_who execute sp_who
select * from #sp_who where spid < 10
You can't add a WHERE clause to a stored procedure like this.
You should put the clause in the sproc, like this:
ALTER PROCEDURE [dbo].[myProc]
#X VARCHAR(10)
AS
BEGIN
SELECT * FROM myTable WHERE x=#X
END
GO
The syntax for calling a stored procedure is through the use of EXECUTE not SELECT(e.g.):
EXECUTE dbo.myProc 'a'
I think you can't do that.
The command to execute a stored procedure is EXECUTE.
See some more examples of the EXECUTE usage.
I think its better to use a view or a table valued function rather than the suggested approach. Both allow you to pass parameters to the function
If you want the WHERE clause to be something you can "turn off" you can do this, passing in a predetermined value (e.g. -1) if the WHERE limitation is to be bypassed:
ALTER PROCEDURE [dbo].[myProc]
#X VARCHAR(10)
AS
BEGIN
SELECT * FROM myTable WHERE x=#X or #X = -1
END
GO
You must declare a variable in the store procedure which will be necessary to pass to run the stored procedure. Here is an example. Keep this in mind: Before AS you can simply declare any variable by using the # character, but after the AS you must write Declare to declare any variable, e.g., Declare #name nvarchar (50).
ALTER PROCEDURE [dbo].[myProc]
#name varchar (50)
AS
BEGIN
SELECT * FROM myTable
where name= #name
END