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

最新文章

  1. android常犯错误记录
  2. iOS开发之Objective-C与JavaScript的交互(转载)
  3. kFreeBSD 7.0于2013/05/04发布 桌面环境 GNOME 3....
  4. 2016年12月11日 星期日 --出埃及记 Exodus 21:6
  5. Linux中安装Cisco Packet Tracer
  6. 在ASP.NET中实现OAuth2.0(一)之了解OAuth
  7. webkit,HTML5头部标签
  8. S3C2440的定时器详解
  9. BZOJ 1033: [ZJOI2008]杀蚂蚁antbuster(模拟)
  10. Gradle 1.12用户指南翻译——第三十一章. FindBugs 插件
  11. Linux 网络管理、软件包安装
  12. xcode 报错 malloc: *** error for object 0x6c3c5a4: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug------d
  13. Flask web开发之路六
  14. CMSampleBufferRef转换
  15. 杂项:TMT(数字媒体产业)
  16. java 定时执行
  17. win10 磁盘占用高--- 禁用用户改善反馈 CompatTelRunner.exe
  18. keepalived vrrp_script脚本不执行解决办法
  19. IG—金字塔
  20. Android isUserAMonkey()

热门文章

  1. TortoiseSVN客户端重新设置用户名和密码
  2. eclipse gradle插件(buildship)的安装和使用
  3. 框架整合----------Hibernate、spring整合
  4. nginx缓冲区优化
  5. dispatch a action with a timeout
  6. [Unity] Android插件
  7. MySQL 5.7 解压版安装配置
  8. 承接unity外包:2016年VR产业八大发展趋势
  9. iOS tabBar双击事件
  10. 严重:Error listenerStart