经典的多语句表值函数

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/3092558 set ...
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/3092558

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


CREATE FUNCTION [dbo].[ufnGetContactInformation](@ContactID int)
RETURNS @retContactInformation TABLE
(
    -- Columns returned by the function
    [ContactID] int PRIMARY KEY NOT NULL,
    [FirstName] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,
    [JobTitle] [nvarchar](50) NULL,
    [ContactType] [nvarchar](50) NULL
)
AS
-- Returns the first name, last name, job title and contact type for the specified contact.
BEGIN
    DECLARE
        @FirstName [nvarchar](50),
        @LastName [nvarchar](50),
        @JobTitle [nvarchar](50),
        @ContactType [nvarchar](50);

    -- Get common contact information
    SELECT
        @ContactID = ContactID,
        @FirstName = FirstName,
        @LastName = LastName
    FROM [Person].[Contact]
    WHERE [ContactID] = @ContactID;

    SET @JobTitle =
        CASE
            -- Check for employee
            WHEN EXISTS(SELECT * FROM [HumanResources].[Employee] e
                WHERE e.[ContactID] = @ContactID)
                THEN (SELECT [Title]
                    FROM [HumanResources].[Employee]
                    WHERE [ContactID] = @ContactID)

            -- Check for vendor
            WHEN EXISTS(SELECT * FROM [Purchasing].[VendorContact] vc
                    INNER JOIN [Person].[ContactType] ct
                    ON vc.[ContactTypeID] = ct.[ContactTypeID]
                WHERE vc.[ContactID] = @ContactID)
                THEN (SELECT ct.[Name]
                    FROM [Purchasing].[VendorContact] vc
                        INNER JOIN [Person].[ContactType] ct
                        ON vc.[ContactTypeID] = ct.[ContactTypeID]
                    WHERE vc.[ContactID] = @ContactID)

            -- Check for store
            WHEN EXISTS(SELECT * FROM [Sales].[StoreContact] sc
                    INNER JOIN [Person].[ContactType] ct
                    ON sc.[ContactTypeID] = ct.[ContactTypeID]
                WHERE sc.[ContactID] = @ContactID)
                THEN (SELECT ct.[Name]
                    FROM [Sales].[StoreContact] sc
                        INNER JOIN [Person].[ContactType] ct
                        ON sc.[ContactTypeID] = ct.[ContactTypeID]
                    WHERE [ContactID] = @ContactID)

            ELSE NULL
        END;

    SET @ContactType =
        CASE
            -- Check for employee
            WHEN EXISTS(SELECT * FROM [HumanResources].[Employee] e
                WHERE e.[ContactID] = @ContactID)
                THEN 'Employee'

            -- Check for vendor
            WHEN EXISTS(SELECT * FROM [Purchasing].[VendorContact] vc
                    INNER JOIN [Person].[ContactType] ct
                    ON vc.[ContactTypeID] = ct.[ContactTypeID]
                WHERE vc.[ContactID] = @ContactID)
                THEN 'Vendor Contact'

            -- Check for store
            WHEN EXISTS(SELECT * FROM [Sales].[StoreContact] sc
                    INNER JOIN [Person].[ContactType] ct
                    ON sc.[ContactTypeID] = ct.[ContactTypeID]
                WHERE sc.[ContactID] = @ContactID)
                THEN 'Store Contact'

            -- Check for individual consumer
            WHEN EXISTS(SELECT * FROM [Sales].[Individual] i
                WHERE i.[ContactID] = @ContactID)
                THEN 'Consumer'
        END;

    -- Return the information to the caller
    IF @ContactID IS NOT NULL
    BEGIN
        INSERT @retContactInformation
        SELECT @ContactID, @FirstName, @LastName, @JobTitle, @ContactType;
    END;

    RETURN;
END;

 

相关文章
|
7月前
链式语句中join用法
链式语句中join用法
36 0
|
7月前
|
Go C语言
C语言之语句和语句的分类
C语言之语句和语句的分类
43 0
|
7月前
|
SQL 存储 定位技术
数据库基础(七):用户自定义数据类型与标量值、内嵌表值、多语句表值函数等用户定义函数的创建、使用、删除
数据库基础(七):用户自定义数据类型与标量值、内嵌表值、多语句表值函数等用户定义函数的创建、使用、删除
129 1
|
12月前
|
存储 SQL 关系型数据库
MySQL基础-变量/流程控制/游标/触发器
MySQL基础-变量/流程控制/游标/触发器
|
12月前
|
知识图谱
举例解释Lingo的条件执行@if语句
举例解释Lingo的条件执行@if语句
341 0
|
存储 关系型数据库 MySQL
MySQL基础篇——存储过程和函数中的变量
MySQL基础篇——存储过程和函数中的变量
121 0
|
SQL
函数的语法
函数的语法
110 1
|
存储 SQL 安全
SQL 存储过程和函数的对比、变量、条件和处理程序、游标、流程控制详解+代码示例
SQL 存储过程和函数的对比、变量、条件和处理程序、游标、流程控制详解+代码示例
|
开发者 Python
If 语句的嵌套 | 学习笔记
快速学习 If 语句的嵌套
|
测试技术 Shell
[20180625]函数与标量子查询13(补充)
[20180625]函数与标量子查询13(补充).txt --//最近一段时间一直在测试标量子查询视buckets的数量,我前面的测试方法纯粹蛮力测试. --//参考链接:http://blog.
1332 0