Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected @@rowcount behavior inside an UDF in MS SQL 2019

here's my sample code

drop function rowcount_test
go
CREATE FUNCTION dbo.rowcount_test () RETURNS INT AS
BEGIN
    DECLARE @v INT
    SELECT @v = 1
    return @@ROWCOUNT
END
GO
grant exec on dbo.rowcount_test to public
go
SELECT dbo.rowcount_test()

It gives 1 when executed by mssql 2017 (and earlier)

It gives 0 when executed by mssql 2019

It gives 1 when executed by mssql 2019 (Standard edition) with a db put to the 2017 compatibility mode

It's never been a problem before... Is it a kind of setting affecting the code or a kind of bug in MSSQL 2019?

like image 920
pelod Avatar asked Nov 20 '19 11:11

pelod


People also ask

What is the use of @@ rowcount?

Data manipulation language (DML) statements set the @@ROWCOUNT value to the number of rows affected by the query and return that value to the client. The DML statements may not send any rows to the client. DECLARE CURSOR and FETCH set the @@ROWCOUNT value to 1.

What is select @@ rowcount?

Usage. SQL Server @@ROWCOUNT is a system variable that is used to return the number of rows that are affected by the last executed statement in the batch.

How do you give Rowcount in SQL?

The ROWCOUNT Set Function causes the server to stop the query processing after the specified number of records is returned. One may limit the number of records returned by all subsequent SELECT statements within the session by using the keyword SET ROWCOUNT.

What is UDF in MS SQL?

In the simplest terms, a user-defined function (UDF) in SQL Server is a programming construct that accepts parameters, does work that typically makes use of the accepted parameters, and returns a type of result.


1 Answers

Scalar udf inlining yet again, rather buggy

SELECT dbo.rowcount_test()

OPTION (USE HINT('DISABLE_TSQL_SCALAR_UDF_INLINING'));
like image 168
lptr Avatar answered Sep 28 '22 01:09

lptr