简体   繁体   中英

SQL Server database blocking issue with @@SERVERNAME

We have a locking issue on one of our database servers. After investigating in detail what is causing the blocking, we found out that this is one of our functions. What was really surprising is that the function does nothing - just returns formatted server name.

CREATE FUNCTION [dbo].[GetReleaseName]()
RETURNS NVARCHAR(50)
AS
BEGIN   
    DECLARE @serverName NVARCHAR(20)
    DECLARE @ReleaseVersion NVARCHAR(20)

    SET @ReleaseVersion = '20170807'
    RETURN @@SERVERNAME + '_UAT_' + @ReleaseVersion
END

How can it be that @@SERVERNAME is causing any blocking?

Scalar functions have poor performance.

Try replacing your function with an inline table valued function.

Here's an example:

CREATE FUNCTION [dbo].[GetReleaseName_ITVF]()
RETURNS TABLE
AS
RETURN(
    SELECT @@SERVERNAME + '_UAT_20170807' as FullServerName
)

Usage:

SELECT * FROM [table] CROSS APPLY GetReleaseName_ITVF()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM