Aracılığıyla paylaş


Oluşturma, değiştirme ve kullanıcı tanımlı işlevler kaldırma

UserDefinedFunctionNesnesi işlevselliği kullanıcıların programsal olarak yönetme kullanıcı tanımlı işlevler sağlayan sağlar Microsoft   SQL Server. Kullanıcı tanımlı işlevler girdi desteği ve çıkış parametreleri ve tablo sütunları doğrudan başvuruları da destekler.

SQL ServerBunlar önce bir veritabanı içinde kayıtlı olmak derlemeler-ebilmek var olmak kullanılmış içinde saklı yordamlar, kullanıcı tanımlı işlevler, Tetikleyiciler ve kullanıcı tanımlı veri türlerini gerektirir. smo destekler bu özelliği ile SqlAssemblynesnesini.

UserDefinedFunctionNesne başvurularını.net derleme ile AssemblyName, ClassName, ve MethodNameÖzellikler.

Ne zaman UserDefinedFunctionnesne başvurularını bir.net derleme, gereken kayıt Kurul oluşturarak bir SqlAssemblynesnesi ve ekleyerek SqlAssemblyCollectionait olduğu nesne, Databasenesne.

Örnek

Sunulan kod örneklerinden herhangi birini kullanmak için, programlama ortamını, programlama şablonunu ve uygulamanızı oluşturacağınız programlama dilini seçmeniz gerekecektir. Daha fazla bilgi için, bkz. Visual Studio'da Visual Basic smo proje oluşturun.NET veya Visual Studio'da Visual C# smo proje oluşturun.NET.

Visual Basic'te bir skalar kullanıcı tanımlı fonksiyon oluşturma

Bu kod örneği nasıl oluşturulacağı ve giriş olan skalar kullanıcı tanımlı fonksiyon kaldırma gösterir DateTimenesne parametresi ve bir tamsayı dönüş türleri Visual Basic. Kullanıcı tanımlı fonksiyonu oluşturulur AdventureWorks2012veritabanı. Örnek, kullanıcı tanımlı fonksiyon, ISOweek, bir tarih bağımsız değişken alır ve ISO hafta numarası hesaplar oluşturur. İşlev çağrılmadan önce bu işlevin doğru hesaplamak için veritabanı DATEFIRST seçeneği 1'e ayarlanmalıdır.

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks2012 2008R2 database.
Dim db As Database
db = srv.Databases("AdventureWorks2012")
'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# içinde bir skalar kullanıcı tanımlı fonksiyon oluşturma

Bu kod örneği nasıl oluşturulacağı ve giriş olan skalar kullanıcı tanımlı fonksiyon kaldırma gösterir DateTimenesne parametresi ve bir tamsayı dönüş türleri Visual C#. Kullanıcı tanımlı fonksiyonu oluşturulur AdventureWorks2012veritabanı. Bu örnek, kullanıcı tanımlı bir işlev oluşturur. ISOweek. Bu işlev, bir tarih bağımsız değişken alır ve ISO hafta numarası hesaplar. Veritabanı doğru olarak hesaplamak bu işlev DATEFIRSTseçeneğini ayarlamak 1işlevi çağrılmadan önce.

{
            //Connect to the local, default instance of SQL Server. 
           Server srv = new Server();
            //Reference the AdventureWorks2012 database. 
           Database db = srv.Databases["AdventureWorks2012"];

            //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();
        }

{
            //Connect to the local, default instance of SQL Server. 
           Server srv = new Server();
            //Reference the AdventureWorks2012 database. 
           Database db = srv.Databases["AdventureWorks2012"];

            //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 içinde bir skalar kullanıcı tanımlı fonksiyon oluşturma

Bu kod örneği nasıl oluşturulacağı ve giriş olan skalar kullanıcı tanımlı fonksiyon kaldırma gösterir DateTimenesne parametresi ve bir tamsayı dönüş türleri Visual C#. Kullanıcı tanımlı fonksiyonu oluşturulur AdventureWorks2012veritabanı. Bu örnek, kullanıcı tanımlı bir işlev oluşturur. ISOweek. Bu işlev, bir tarih bağımsız değişken alır ve ISO hafta numarası hesaplar. Veritabanı doğru olarak hesaplamak bu işlev DATEFIRSTseçeneğini ayarlamak 1işlevi çağrılmadan önce.

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2012
CD \sql\localhost\default\databases
$db = get-item Adventureworks2012

# 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()

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2012
CD \sql\localhost\default\databases
$db = get-item Adventureworks2012

# 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()

Ayrıca bkz.

Başvuru

UserDefinedFunction