사용자 정의 함수 만들기, 변경 및 제거

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse Analytics

이 개체는 UserDefinedFunction 사용자가 Microsoft SQL Server에서 사용자 정의 함수를 프로그래밍 방식으로 관리할 수 있는 기능을 제공합니다. 사용자 정의 함수는 입력 및 출력 매개 변수를 지원하며 테이블 열에 대한 직접 참조도 지원합니다.

SQL Server에서는 저장 프로시저, 사용자 정의 함수, 트리거 및 사용자 정의 데이터 형식 내에서 어셈블리를 사용하려면 데이터베이스 내에 어셈블리를 등록해야 합니다. SMO는 개체를 사용하여 이 기능을 SqlAssembly 지원합니다.

개체는 UserDefinedFunction . ClassNameMethodName 속성을 사용하여 .NET 어셈블리를 AssemblyName참조합니다.

개체가 UserDefinedFunction .NET 어셈블리를 참조하는 경우 개체를 SqlAssembly 만들고 개체에 속하는 개체에 SqlAssemblyCollection 추가하여 어셈블리를 Database 등록해야 합니다.

예시

제공된 코드 예제를 사용하려면 프로그래밍 환경, 프로그래밍 템플릿 및 애플리케이션을 만들 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기를 참조하세요.

Visual Basic에서 스칼라 사용자 정의 함수 만들기

이 코드 예제에서는 Visual Basic에서 입력 DateTime 개체 매개 변수와 정수 반환 형식이 있는 스칼라 사용자 정의 함수를 만들고 제거하는 방법을 보여줍니다. 사용자 정의 함수는 AdventureWorks2022 데이터베이스에 만들어집니다. 이 예제에서는 날짜 인수를 사용하고 ISO 주 번호를 계산하는 사용자 정의 함수 ISOweek을 만듭니다. 이 함수를 올바르게 계산하려면 함수가 호출되기 전에 데이터베이스 DATEFIRST 옵션을 1로 설정해야 합니다.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2022 database.
Dim db As Database
db = srv.Databases("AdventureWorks2022")
'Define a UserDefinedFunction object variable by supplying the parent database and the name arguments in the constructor.
Dim udf As UserDefinedFunction
udf = New UserDefinedFunction(db, "IsOWeek")
'Set the TextMode property to false and then set the other properties.
udf.TextMode = False
udf.DataType = DataType.Int
udf.ExecutionContext = ExecutionContext.Caller
udf.FunctionType = UserDefinedFunctionType.Scalar
udf.ImplementationType = ImplementationType.TransactSql
'Add a parameter.
Dim par As UserDefinedFunctionParameter
par = New UserDefinedFunctionParameter(udf, "@DATE", DataType.DateTime)
udf.Parameters.Add(par)
'Set the TextBody property to define the user defined function.
udf.TextBody = "BEGIN  DECLARE @ISOweek int SET @ISOweek= DATEPART(wk,@DATE)+1 -DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104') IF (@ISOweek=0) SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1 AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1 IF ((DATEPART(mm,@DATE)=12) AND ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28)) SET @ISOweek=1 RETURN(@ISOweek) END;"
'Create the user defined function on the instance of SQL Server.
udf.Create()
'Remove the user defined function.
udf.Drop()

Visual C에서 스칼라 사용자 정의 함수 만들기#

이 코드 예제에서는 C#에서 입력 DateTime 개체 매개 변수와 정수 반환 형식이 있는 스칼라 사용자 정의 함수를 만들고 제거하는 방법을 보여줍니다. 사용자 정의 함수는 AdventureWorks2022 데이터베이스에 만들어집니다. 이 예제에서는 사용자 정의 함수를 만듭니다. ISOweek. 이 함수는 날짜 인수를 사용하여 ISO 주 번호를 계산합니다. 이 함수가 올바르게 계산되려면 함수가 호출되기 전에 데이터베이스 DATEFIRST 옵션을 설정 1 해야 합니다.

{  
            //Connect to the local, default instance of SQL Server.   
           Server srv = new Server();  
            //Reference the AdventureWorks2022 database.   
           Database db = srv.Databases["AdventureWorks2022"];  
  
            //Define a UserDefinedFunction object variable by supplying the parent database and the name arguments in the constructor.   
            UserDefinedFunction udf = new UserDefinedFunction(db, "IsOWeek");  
  
            //Set the TextMode property to false and then set the other properties.   
            udf.TextMode = false;  
            udf.DataType = DataType.Int;  
            udf.ExecutionContext = ExecutionContext.Caller;  
            udf.FunctionType = UserDefinedFunctionType.Scalar;  
            udf.ImplementationType = ImplementationType.TransactSql;  
  
            //Add a parameter.   
  
     UserDefinedFunctionParameter par = new UserDefinedFunctionParameter(udf, "@DATE", DataType.DateTime);  
            udf.Parameters.Add(par);  
  
            //Set the TextBody property to define the user-defined function.   
            udf.TextBody = "BEGIN DECLARE @ISOweek int SET @ISOweek= DATEPART(wk,@DATE)+1 -DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104') IF (@ISOweek=0) SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1 AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1 IF ((DATEPART(mm,@DATE)=12) AND ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28)) SET @ISOweek=1 RETURN(@ISOweek) END;";  
  
            //Create the user-defined function on the instance of SQL Server.   
            udf.Create();  
  
            //Remove the user-defined function.   
            udf.Drop();  
        }  

PowerShell에서 스칼라 사용자 정의 함수 만들기

이 코드 예제에서는 C#에서 입력 DateTime 개체 매개 변수와 정수 반환 형식이 있는 스칼라 사용자 정의 함수를 만들고 제거하는 방법을 보여줍니다. 사용자 정의 함수는 AdventureWorks2022 데이터베이스에 만들어집니다. 이 예제에서는 사용자 정의 함수를 만듭니다. ISOweek. 이 함수는 날짜 인수를 사용하여 ISO 주 번호를 계산합니다. 이 함수가 올바르게 계산되려면 함수가 호출되기 전에 데이터베이스 DATEFIRST 옵션을 설정 1 해야 합니다.

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2022  
CD \sql\localhost\default\databases  
$db = get-item AdventureWorks2022  
  
# Define a user defined function object variable by supplying the parent database and name arguments in the constructor.   
$udf  = New-Object -TypeName Microsoft.SqlServer.Management.SMO.UserDefinedFunction `  
-argumentlist $db, "IsOWeek"  
  
# Set the TextMode property to false and then set the other properties.   
$udf.TextMode = $false  
$udf.DataType = [Microsoft.SqlServer.Management.SMO.DataType]::Int   
$udf.ExecutionContext = [Microsoft.SqlServer.Management.SMO.ExecutionContext]::Caller  
$udf.FunctionType = [Microsoft.SqlServer.Management.SMO.UserDefinedFunctionType]::Scalar  
$udf.ImplementationType = [Microsoft.SqlServer.Management.SMO.ImplementationType]::TransactSql  
  
# Define a Parameter object variable by supplying the parent function, name and type arguments in the constructor.  
$type = [Microsoft.SqlServer.Management.SMO.DataType]::DateTime  
$par  = New-Object -TypeName Microsoft.SqlServer.Management.SMO.UserDefinedFunctionParameter `  
-argumentlist $udf, "@DATE",$type  
  
# Add the parameter to the function  
$udf.Parameters.Add($par)  
  
#Set the TextBody property to define the user-defined function.   
$udf.TextBody = "BEGIN DECLARE @ISOweek int SET @ISOweek= DATEPART(wk,@DATE)+1 -DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104') IF (@ISOweek=0) SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1 AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1 IF ((DATEPART(mm,@DATE)=12) AND ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28)) SET @ISOweek=1 RETURN(@ISOweek) END;"  
  
# Create the user-defined function on the instance of SQL Server.   
$udf.Create()  
  
# Remove the user-defined function.   
$udf.Drop()  

참고 항목

UserDefinedFunction