Stored procedure has multiple SELECT statements - sql

This stored procedure has multiple SELECT statements - I need to write a condition based on one SELECT statement results among them without altering the stored procedure:
CREATE PROC Test
AS
BEGIN
SELECT 1 AS ID
FROM EMPLOYEE
SELECT NAME, ADDRESS, STATE
FROM EMPLOYEE --NEED TO TAKE ROW COUNT
END
After executing the stored procedure, I'll get 2 result sets. But I need to row count of 2nd select statement, without altering the stored procedure.
Based on that rowcount, I need to work further.

exec Test
select ##rowcount
In this case, ##rowcount will always return last result set row count.

Related

How to get row count of select statement inside stored procedure in SQL Server

I have requirement where I want get row count of select statement inside stored procedure and if row count is equal to 0 then only it will execute the next statement
select A, B, C
from AT
where B = 1
If the above statement returns any rows, then it will not execute further but if this statement do not have any row then it will execute next statement. I have tried it using in two ways
##rowcount - it's not working properly
Using temp table by inserting select statement into table getting row count of table but using temp table is not optimize way
Is there any solution?
You could use IF NOT EXISTS:
IF NOT EXISTS (select A,B,C from AT where B=1)
BEGIN
-- sth
END
is there any solution like getting into variable without hitting to database again and again
DECLARE #my_rowcount INT;
select A,B,C from AT where B=1;
SET #my_rowcount = ##ROWCOUNT; -- immediately after select get ##ROWCOUNT
...
IF #my_rowcount = 0
BEGIN
-- sth
END
EDIT:
##ROWCOUNT Globle variable for database so it may return wrong Value if any other select statement processed in other sp in same databe
Nope. ##ROWCOUNT:
Returns the number of rows affected by the last statement.
You could easily check it with your SSMS(open 2 tabs, select 1 and 2 rows on each of them and then get ##ROWCOUNT respectively).

Get count of records of a table which return from stored procedure

I have a stored procedure, which is returning a table. I just want the count of the records is it possible
My Procedure
create procedure Test
as begin
select * From Student
end
exec Test
will give the records out put
I want the count
NB: I need the sp to return the results of select statement.In another place I need the count of the records returned by sp and columns in the student table is dynamic.
I am expecting an answer without modifying stored procedure.
You can select the data into a temporary table like below. However, you have to use OPENQUERY to do so. You must also enable data access on your server first.
Exec sp_serveroption 'ServerName', 'data access', 'true'
SELECT * INTO #TempTable
FROM OPENQUERY("ServerName", 'EXEC Test')
SELECT COUNT(*) FROM #TempTable
NB: I need the sp to return the results of select statement.
Use your stored procedure as is. That is,
create procedure Test
as begin
select * From Student
end
In another place I need the count of the records returned by sp and
columns in the student table is dynamic.
If this other place is another SP, then use rowcount. You use it this way :
EXEC [sp_WhateverTheSPNameIs]
select ##rowcount
Read about ##RowCount. This is all you need.

Using the count as a parameter in stored procedure?

I'm looking to try creating a stored procedure that will search for duplicate rows based on a certain number (i.e., if I wanna see an instance of 2 identical rows, use "having count(*)>2)
select invoice.first_name, invoice.last_name, invoice.date_ordered,
sum(total_cost) AS Total_Spent
from invoice
group by invoice.last_name
having count(*)>2;
What I would like to create is the option for the person to specify the count whenever they call the procedure so they don't have to go into the procedure, edit the query, save, etc.
Is there a way to turn it into a parameter?
Try like this:
set nocount on;
create proc dbo.test
#nas int
as
begin
select invoice.first_name, invoice.last_name, invoice.date_ordered,
sum(total_cost) AS Total_Spent
from invoice
group by invoice.last_name
having count(*)>#nas;
end
exec dbo.test #nas=2
First of all, what do you mean by:
the option for the person to specify the count
If you mean that you want to specify the number of identical records returned, then you could:
ALTER PROCEDURE YourProcedureName (#Count INT) AS
BEGIN
select invoice.first_name, invoice.last_name, invoice.date_ordered,
sum(total_cost) AS Total_Spent
from invoice
group by invoice.last_name
having count(*)>#Count;
END
This will create an input parameter so that when the user runs the procedure they can specify that the number of identical rows will be greater than the #Count Parameter.
If you want the user to be able to count on a specific column(s), that becomes a bit more tricky.

How to select query result inside stored procedure

I'm writing a stored procedure in SQL Server 2008 which contains a SELECT DISTINCT statement and another simple Select statement which is based on the result of first statement.
How to use the table returned by the SELECT DISTINCT statement i.e the UnitNumber column value in second Select statement?
Stored procedure:
CREATE PROCEDURE ExtractPacket
AS
BEGIN
SET NOCOUNT ON;
-- Select statements to check the number of unit
SELECT DISTINCT UnitNumber from dbo.CP_TemplateHandler
END
GO
you can create a temp table and fill it by first SELECT DISTINCT and then in second Select use that.
Excuse me for answer in order comment(i can comment yet :( )
I suggest that use First Select Distinct as a sub query of second Select Distinct query.

How to get the stored procedure result set value

I create a stored proc A that calls another proc B, which returns a list of the result set.
How I can use these result set values in my proc A as I have to use this result set value one by one to pass to other part of the proc A.
You can perform an INSERT INTO to insert the results of the stored procedure into a (temporary) table. You can then use a select statement to process these results.
INSERT INTO SomeTableThatMatchesTheSproc
EXEC YourStoredProcedure;
SELECT * FROM YourTable;