How can you find out if the Sql Server stored procedure returns records

Inside tsql, I call the tsql stored procedure, which returns a set of records. I want to know if the recordset is set empty or not empty.

For some reason, @@ rowcount always returns 1.

What is the best way to do this?

One more thing, I cannot edit this stored procedure.

+3
source share
4 answers

Use @@ rowcount in the internal stored procedure, pass back as the output parameter. Use @@ rowcount immediately after the SELECT in the internally stored proc. And called like this:

EXEC dbo.InnerProc @p1, ..., @rtncount OUTPUT

or...

Use RETURN @@ rowcount in the internal stored procedure immediately after SELECT. And called like this:

EXEC @rtncount = dbo.InnerProc @p1, ...

Edit:

proc, .

CREATE TABLE #foo (bar int...)

INSERT #foo
EXEC MyUntouchableProc @p1
SELECT @@ROWCOUNT

@@ROWCOUNT , , SELECT. RETURN, END ( ), SET ..

+3

Edit

@@rowcount SELECT. - , @@rowcount . evalute @@rowcount .

Edit

, SET NOCOUNT ON.

a select count(*) from... . .

+1

@@ROWCOUNT

msdn:

, . 2 , ROWCOUNT_BIG.

:

USE AdventureWorks2008R2;
GO
UPDATE HumanResources.Employee 
SET JobTitle = N'Executive'
WHERE NationalIDNumber = 123456789
IF @@ROWCOUNT = 0
PRINT 'Warning: No rows were updated';
ELSE
PRINT @@ROWCOUNT + ' records updated';
GO

.net SqlDataReader, .HasRows .count . , , sproc, out sproc. , .net, ( , sproc).

MSDN:

, @@ROWCOUNT 1. . : SET @local_variable, RETURN, READTEXT SELECT GETDATE() SELECT " ".

, SET NOCOUNT ON

+1

@@rowcount: .

+1

Source: https://habr.com/ru/post/1793689/


All Articles