經典的多語句錶值函式

2021-04-20 22:33:32 字數 3242 閱讀 2650

set ansi_nulls on

set quoted_identifier on

gocreate 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;

多語句錶值函式

多語句錶值函式 多語句錶值函式可以看做是標量函式和內聯錶值函式的結合體。語法 create function 函式名 引數列表 returns 表變數名 table 表變數的字段定義 as begin sql語句 return end 練習 根據性別返回所有學生的學號,姓名,籍貫,數學成績,如果是女...

TOP語句放到錶值函式外,效率異常低下

在 系統中,有乙個獲取客戶資料的sqlserver 錶值函式,如果使用管理員登入,這個函式會返回150w行記錄,大概需要30秒左右,但如果將top語句放到錶值函式外,效率異常低下,需要約3分鐘 select top20 from getframe customerserch admin 1 將get...

簡述錶值函式

錶值函式 建立示例表 create table t name varchar 20 go insert into t select a union select b go 內嵌錶值函式 語法 create function 函式名 引數列表 returns table as return t sql...