使用表参数创建T-SQL函数

时间:2015-04-09 12:36:57

标签: sql sql-server sql-server-2008 tsql sql-server-2005

我需要用table参数编写函数。

一个例子:

CREATE FUNCTION getParentByBrandList
( @_BrandList TABLE(
                        BR_ID INT, BR_Name NVARCHAR(150), BR_ParentBrandID INT, BR_MasterBrandID INT, BR_Role INT,
                        BR_State INT, BR_OwnerID INT, BR_OwnerIP NVARCHAR(50), BR_CreateDate DATETIME, BR_UpdaterID INT,
                        BR_UpdaterIP NVARCHAR(50), BR_UpdateDate DATETIME
                       ) 
)

我该怎么办?

感谢名单

4 个答案:

答案 0 :(得分:3)

选中此tutorial

示例:将表值参数传递给函数

/* CREATE USER DEFINED TABLE TYPE */
CREATE TYPE StateMaster AS TABLE
(
 StateCode VARCHAR(2),
 StateDescp VARCHAR(250)
)
GO  
/*CREATE  FUNCTION WHICH TAKES TABLE AS A PARAMETER  */
CREATE FUNCTION TableValuedParameterExample(@TmpTable StateMaster READONLY)
RETURNS  VARCHAR(250)
AS
BEGIN
 DECLARE @StateDescp VARCHAR(250)
 SELECT @StateDescp = StateDescp FROM @TmpTable
 RETURN @StateDescp
END
GO

答案 1 :(得分:2)

试试这个......

CREATE FUNCTION getParentByBrandList ( )
RETURNS @_BrandList TABLE
    (
     BR_ID INT
    ,BR_Name NVARCHAR(150)
    ,BR_ParentBrandID INT
    ,BR_MasterBrandID INT
    ,BR_Role INT
    ,BR_State INT
    ,BR_OwnerID INT
    ,BR_OwnerIP NVARCHAR(50)
    ,BR_CreateDate DATETIME
    ,BR_UpdaterID INT
    ,BR_UpdaterIP NVARCHAR(50)
    ,BR_UpdateDate DATETIME
    )
AS 
    BEGIN
    --code to create/populate table
    END;

您似乎错过了returns,我添加了代码的占位符。此外,由于名称建议 Get By ,您可能会强调提供参数?如果是这样,你只需添加到括号......

getParentByBrandList ( param)

答案 2 :(得分:2)

SQL Server 2008开始,您可以使用表值参数:

CREATE TYPE [dbo].[TableType] AS TABLE(
[ID] [INT] NULL
)
GO

CREATE FUNCTION fnTest
    (
      @t [dbo].[TABLETYPE] READONLY
    )
RETURNS INT
AS
    BEGIN

        RETURN (SELECT TOP 1 ID FROM @t ORDER BY id DESC)

    END
GO

DECLARE @t [dbo].[TABLETYPE]
INSERT  INTO @t
VALUES  ( 1 ),
        ( 2 )

SELECT  dbo.fnTest(@t) AS ID

输出:

ID
2

答案 3 :(得分:0)

此功能来自Microsoft的AdevnrtureWorks2012数据库,它们用于学习目的。

ALTER FUNCTION [dbo].[ufnGetContactInformation](@PersonID int)
RETURNS @retContactInformation TABLE 
(
    -- Columns returned by the function
    [PersonID] int NOT NULL, 
    [FirstName] [nvarchar](50) NULL, 
    [LastName] [nvarchar](50) NULL, 
    [JobTitle] [nvarchar](50) NULL,
    [BusinessEntityType] [nvarchar](50) NULL
)
AS 
-- Returns the first name, last name, job title and business entity type for the specified contact.
-- Since a contact can serve multiple roles, more than one row may be returned.
BEGIN
    IF @PersonID IS NOT NULL 
        BEGIN
        IF EXISTS(SELECT * FROM [HumanResources].[Employee] e 
                    WHERE e.[BusinessEntityID] = @PersonID) 
            INSERT INTO @retContactInformation
                SELECT @PersonID, p.FirstName, p.LastName, e.[JobTitle], 'Employee'
                FROM [HumanResources].[Employee] AS e
                    INNER JOIN [Person].[Person] p
                    ON p.[BusinessEntityID] = e.[BusinessEntityID]
                WHERE e.[BusinessEntityID] = @PersonID;

        IF EXISTS(SELECT * FROM [Purchasing].[Vendor] AS v
                    INNER JOIN [Person].[BusinessEntityContact] bec 
                    ON bec.[BusinessEntityID] = v.[BusinessEntityID]
                    WHERE bec.[PersonID] = @PersonID)
            INSERT INTO @retContactInformation
                SELECT @PersonID, p.FirstName, p.LastName, ct.[Name], 'Vendor Contact' 
                FROM [Purchasing].[Vendor] AS v
                    INNER JOIN [Person].[BusinessEntityContact] bec 
                    ON bec.[BusinessEntityID] = v.[BusinessEntityID]
                    INNER JOIN [Person].ContactType ct
                    ON ct.[ContactTypeID] = bec.[ContactTypeID]
                    INNER JOIN [Person].[Person] p
                    ON p.[BusinessEntityID] = bec.[PersonID]
                WHERE bec.[PersonID] = @PersonID;

        IF EXISTS(SELECT * FROM [Sales].[Store] AS s
                    INNER JOIN [Person].[BusinessEntityContact] bec 
                    ON bec.[BusinessEntityID] = s.[BusinessEntityID]
                    WHERE bec.[PersonID] = @PersonID)
            INSERT INTO @retContactInformation
                SELECT @PersonID, p.FirstName, p.LastName, ct.[Name], 'Store Contact' 
                FROM [Sales].[Store] AS s
                    INNER JOIN [Person].[BusinessEntityContact] bec 
                    ON bec.[BusinessEntityID] = s.[BusinessEntityID]
                    INNER JOIN [Person].ContactType ct
                    ON ct.[ContactTypeID] = bec.[ContactTypeID]
                    INNER JOIN [Person].[Person] p
                    ON p.[BusinessEntityID] = bec.[PersonID]
                WHERE bec.[PersonID] = @PersonID;

        IF EXISTS(SELECT * FROM [Person].[Person] AS p
                    INNER JOIN [Sales].[Customer] AS c
                    ON c.[PersonID] = p.[BusinessEntityID]
                    WHERE p.[BusinessEntityID] = @PersonID AND c.[StoreID] IS NULL) 
            INSERT INTO @retContactInformation
                SELECT @PersonID, p.FirstName, p.LastName, NULL, 'Consumer' 
                FROM [Person].[Person] AS p
                    INNER JOIN [Sales].[Customer] AS c
                    ON c.[PersonID] = p.[BusinessEntityID]
                    WHERE p.[BusinessEntityID] = @PersonID AND c.[StoreID] IS NULL; 
        END

    RETURN;
END;