use [dbname]
go

--1.产生crl程序集的sql

--定义表变量,临时存储中间结果集
declare @tb table
(
name nvarchar(100),
permission_set int,
content varbinary(max),
rownum int,
create_clr_sql nvarchar(max)
);

insert into @tb
select a.name,
a.permission_set,
af.content,
ROW_NUMBER() over(order by @@servername) as rownum,
null
from sys.assembly_files af
inner join sys.assemblies a
on af.assembly_id = a.assembly_id
where a.is_visible=1

/*2008  a.is_user_defined = 1*/


--select * from @tb

--从表变量中每次取出一条数据,通过内存循环把varbinary转化为varchar
--最后,拼接产生clr的sql语句,update到表变量中的create_clr_sql字段中
declare @outer_i int; --外层循环变量
declare @count int;

declare @bin varbinary(max)
declare @bin_convert_varchar varchar(max)
declare @inner_i int --内存循环变量

set @outer_i = 1;
set @count = (select COUNT(*) from @tb);

while @outer_i <= @count
begin
set @bin = (select content from @tb where rownum = @outer_i)

select @bin_convert_varchar = '',
@inner_i = datalength(@bin);

while @inner_i>0
begin
select @bin_convert_varchar=
substring('0123456789ABCDEF',substring(@bin,@inner_i,1)/16+1,1)+
substring('0123456789ABCDEF',substring(@bin,@inner_i,1)%16+1,1)+
@bin_convert_varchar,
@inner_i=@inner_i-1
end

update @tb
set create_clr_sql = N'create assembly ['+name+']' +
N' AUTHORIZATION [dbo] ' +
N'FROM 0x' + @bin_convert_varchar +
N' WITH PERMISSION_SET = ' +
case permission_set
when 1 then 'SAFE'
when 2 then 'EXTERNAL'
when 3 then 'UNSAFE'
end
where rownum = @outer_i

set @outer_i = @outer_i + 1
end


--创建crl程序集的sql
select create_clr_sql from @tb

 

--2.产生函数定义的sql

--创建基于clr程序集的用户自定义函数
select 'create function [dbo].[' + o.name + '](' +

stuff(
(select ',' + c.name +' ' + tp.name +
CASE WHEN tp.name in ('numeric','decimal')
THEN '(' + CAST(c.precision AS VARCHAR) +
',' + CAST(c.scale AS VARCHAR) +
')'

WHEN tp.name in ('varbinary','varchar')
THEN case when c.max_length <> -1
then '(' + CAST(c.max_length AS VARCHAR) + ')'
else '(max)'
end

WHEN tp.name = 'nvarchar'
then case when c.max_length <> -1
then '(' + CAST(c.max_length/2 AS VARCHAR) + ')'
else '(max)'
end

when tp.name = 'nchar'
then '(' + CAST(c.max_length/2 AS VARCHAR) + ')'

WHEN tp.name IN ('binary','bit','char')
THEN '(' + CAST(c.max_length AS VARCHAR) +
')'
ELSE ''
END
from sys.all_parameters c
inner join sys.types tp
on c.system_type_id = tp.system_type_id
and c.user_type_id = tp.user_type_id

where c.object_id = o.object_id
and c.is_output = 0
for xml path('')
),
1,1,''
) +

(select ') returns ' + c.name +' ' + tp.name +
CASE WHEN tp.name in ('numeric','decimal')
THEN '(' + CAST(c.precision AS VARCHAR) +
',' + CAST(c.scale AS VARCHAR) +
')'

WHEN tp.name in ('varbinary','varchar')
THEN case when c.max_length <> -1
then '(' + CAST(c.max_length AS VARCHAR) + ')'
else '(max)'
end

WHEN tp.name = 'nvarchar'
then case when c.max_length <> -1
then '(' + CAST(c.max_length/2 AS VARCHAR) + ')'
else '(max)'
end

when tp.name = 'nchar'
then '(' + CAST(c.max_length/2 AS VARCHAR) + ')'

WHEN tp.name IN ('binary','bit','char')
THEN '(' + CAST(c.max_length AS VARCHAR) +
')'
ELSE ''
END
from sys.all_parameters c
inner join sys.types tp
on c.system_type_id = tp.system_type_id
and c.user_type_id = tp.user_type_id

where c.object_id = o.object_id
and c.is_output = 1
) +

' WITH EXECUTE AS CALLER AS EXTERNAL name [' + a.name + N'].'+
'['+ am.assembly_class +'].[' +
am.assembly_method + '] ;' collate Chinese_PRC_CI_AS

from sys.assemblies a
inner join sys.assembly_modules am
on am.assembly_id = a.assembly_id
inner join sys.objects o
on am.object_id = o.object_id

posted on 2016-02-17 14:32  亚洲DotNet首席技师  阅读(190)  评论(0编辑  收藏  举报