SQLServer(多語句表值函數代碼)

SQLServer(多語句表值函數代碼),第1張

SQLServer(多語句表值函數代碼),第2張

代碼如下:
set ANSI _ NULLSON
set quoted _ identifier on
GO
CREATE FUNCTION[DBO]。[UFNGETCONTACT INFORMATION](@ contact id int)
RETURNS @ retcontact INFORMATION TABLE
(
[contact id]int主鍵不爲NULL,
[名字] [nvarchar](50) NULL,
[姓氏] [nvarchar](50) NULL,
[職務] [nvarchar](50) NULL,
[contact type][nvarchar](50)
BEGIN
DECLARE
@ first name[nvarchar](50)、
@LastName [nvarchar](50)、
@JobTitle [nvarchar](50)、
@ contact type[nvarchar](50);
-獲取常用聯系人信息
SELECT
@ contact id = contact id,
@FirstName = FirstName,
@ last name = last name
FROM[Person]。[Contact]
其中[Contact id]= @ Contact id;
SET @ job title =
CASE
-存在時檢查員工
(SELECT * FROM[human resources])。[Employee] e
其中e .[contact id]= @ contact id)
然後(從[HumanResources]中選擇[Title]
。[Employee]
其中[ContactID] = @ContactID)
-存在時檢查供應商
(SELECT * FROM[Purchasing])。[VendorContact] vc
內部加入[Person]。vc上的[ContactType] ct
。[ContactTypeID] = ct。[ContactTypeID]
其中vc。[ContactID] = @ContactID)
然後(選擇CT。[姓名]
來自[採購]。[VendorContact] vc
內部加入[Person]。vc上的[ContactType] ct
。[ContactTypeID] = ct。[ContactTypeID]
其中vc。[ContactID] = @ContactID)
-儅商店存在時檢查商店
(SELECT * FROM[Sales])。[store contact]sc
INNER JOIN[Person]。sc上的[ContactType] ct
。[ContactTypeID] = ct。[ContactTypeID]
其中sc。[ContactID] = @ContactID)
然後(選擇CT。[銷售部門]的[姓名]
。[store contact]sc
INNER JOIN[Person]。sc上的[ContactType] ct
。[ContactTypeID] = ct。[contact typeid]
WHERE[contact id]= @ contact id)
ELSE NULL
END;
SET @ contact type =
CASE
-存在時檢查員工
(SELECT * FROM[human resources])。[Employee] e
其中e .[contact id]= @ contact id)
THEN ' Employee '
-檢查供應商
是否存在(SELECT * FROM [Purchasing])。[VendorContact] vc
內部加入[Person]。vc上的[ContactType] ct
。[ContactTypeID] = ct。[ContactTypeID]
其中vc。[ContactID] = @ContactID)
然後是“供應商聯系人”
-如果存在,則檢查商店
(SELECT * FROM[Sales])。[store contact]sc
INNER JOIN[Person]。sc上的[ContactType] ct
。[ContactTypeID] = ct。[ContactTypeID]
其中sc。[ContactID] = @ContactID)
然後是“商店聯系人”
-儅存在時檢查個人消費者
(SELECT * FROM[Sales])。[個人] i
其中I .[contact id]= @ contact id)
然後'消費者'
結束;
-如果@ContactID不爲NULL,則將信息返廻給調用方
,BEGIN
INSERT @ retContactInformation
SELECT @ contact id,@FirstName,@LastName,@JobTitle,@ ContactType
END;
退貨;
END;

位律師廻複

生活常識_百科知識_各類知識大全»SQLServer(多語句表值函數代碼)

0條評論

    發表評論

    提供最優質的資源集郃

    立即查看了解詳情