USE [NC]
GO
/****** Object: UserDefinedFunction [dbo].[dict_url_channel] Script Date: 2019/5/25 16:40:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[dict_url_channel]
(
@url varchar(MAX) --传入参数
)
returns varchar(50) --返回参数类型
as
begin
declare @channel nvarchar(50) --定义变量
declare @temp varchar(250)
declare @charindex int
declare key_url_cursor cursor local for select key_url from Information.dbo.url_to_渠道 order by seq
--定义一个叫key_url_cursor 的游标,按seq顺序存放for select 后的数据 
open key_url_cursor --打开游标
fetch next from key_url_cursor into @temp ----获取key_cursor的下一条数据,其中为字段赋值@temp
while @@FETCH_STATUS = 0
begin
select @charindex = CHARINDEX(@temp,@url) --判断@temp是否在@url中
if @charindex > 0
begin
select @channel = value_渠道 from Information .dbo.url_to_渠道 where key_url = @temp;
break;
end
fetch next from key_url_cursor into @temp
end
close key_url_cursor --关闭游标
deallocate key_url_cursor --释放游标
if @channel is null
begin
select @channel=''
end
return @channel
end
05-25 22:13