SET ROWCOUNT
will affect the rows returned by both the outer SELECT
statement and the outer level of any SELECT
statements contained in the scalar UDF.
So this isn't possible through use of SET ROWCOUNT
alone unless you can change the definition of the Scalar UDF.
I assume that you are using the undocumented and un guaranteed variable assignment approach. If you change to the XML PATH
approach then as the top most SELECT
only returns 1 row anyway it will remain unaffected by the SET ROWCOUNT 1
command.
Example Code
USE tempdb;
GO
CREATE FUNCTION dbo.Concat1(@number INT = 0)
RETURNS NVARCHAR(max)
AS
BEGIN
DECLARE @Result NVARCHAR(max)
SELECT @Result = COALESCE(@Result + ',','') + name
FROM master..spt_values
WHERE number = @number
RETURN @Result
END
GO
CREATE FUNCTION dbo.Concat2(@number INT = 0)
RETURNS NVARCHAR(max)
AS
BEGIN
RETURN
(SELECT STUFF((SELECT ',' + name AS [text()]
FROM master..spt_values
WHERE number = @number
FOR XML PATH('')), 1, 1, ''))
END
GO
SET ROWCOUNT 1;
SELECT dbo.Concat1(number) AS Concat1,
dbo.Concat2(number) AS Concat2
FROM master..spt_values
WHERE number = 1
DROP FUNCTION dbo.Concat1, dbo.Concat2
Results
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Concat1 | Concat2 |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| rpc | rpc,yes,autoclose,published,WINDOWS/NT,trusted,ignore duplicate keys,binary,varbinary,primary,NULL,Xact,NUL,GRANT,system table,disable_def_cnst_check,default disk |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+