{"id":677,"date":"2010-10-07T22:53:20","date_gmt":"2010-10-07T21:53:20","guid":{"rendered":"http:\/\/www.gennard.net\/blog\/?p=677"},"modified":"2010-10-07T22:53:20","modified_gmt":"2010-10-07T21:53:20","slug":"tsql-sqlclr-drop-assembly","status":"publish","type":"post","link":"http:\/\/www.gennard.net\/blog\/2010\/10\/tsql-sqlclr-drop-assembly\/","title":{"rendered":"SQLCLR &#8211; Drop Assembly + Dependancies"},"content":{"rendered":"<p>Recently I have had the pleasure of working on SQL Server 2008 R2 using the SQLCLR support.<\/p>\n<p>Which I must admit it is quite nice to use, though a couple of things have anoyed me while using&#8230; one of which is the user of &#8220;DROP ASSEMBLY&#8221; and its dependancies&#8230;.  normally this is not a huge problem but when you are write tests, it is good practice to remove\/drop everything before deploying.<\/p>\n<p>Being the sort of person that hates doing repetitively, I thought I would do a quick proc&#8230;. so let me introduce you to &#8216;MegaDrop&#8217;&#8230; yeah&#8230; its a rubbish name and the code is not elegant but it gets the job done..<\/p>\n<p>Enjoy&#8230;<\/p>\n<\/p>\n<p><code lang=\"tsql\" width=\"800\" lines=\"-1\" nowrap=\"0><br \/>\nCREATE PROCEDURE MegaDrop @assemblyName varchar(max)<br \/>\nAS<br \/>\n\tdeclare @name varchar(max)<br \/>\n\tdeclare @type_desc varchar(max)<\/p>\n<p>\tdeclare mf_getprocs_cursor cursor local for<br \/>\n\t SELECT so.name, so.type_desc from sys.objects  so with (nolock)<br \/>\n\t\tJOIN sys.assembly_modules sam with (nolock) on so.object_id = sam.object_id<br \/>\n\t\tJOIN sys.assemblies sa with (nolock) on sam.assembly_id = sa.assembly_id<br \/>\n\t\tWHERE ( sa.name = @assemblyName)<\/p>\n<p>\topen mf_getprocs_cursor<br \/>\n\tfetch next from mf_getprocs_cursor into @name, @type_desc<br \/>\n\twhile (@@fetch_status <> -1)<br \/>\n\tbegin<br \/>\n\t\tif @type_desc = 'CLR_STORED_PROCEDURE'<br \/>\n\t\tbegin<br \/>\n\t\t\tprint  'Executing DROP PROC '+@name + ' ('+@assemblyName+')'<br \/>\n\t\t\texecute ('drop proc '+@name)<br \/>\n\t\tend<\/p>\n<p>\t\tif @type_desc = 'AGGREGATE_FUNCTION'<br \/>\n\t\tbegin<br \/>\n\t\t\tprint  'Executing DROP AGGREGATE '+@name + ' ('+@assemblyName+')'<br \/>\n\t\t\texecute ('drop Aggregate '+@name)<br \/>\n\t\tend<\/p>\n<p>\t\tif @type_desc = 'CLR_TRIGGER'<br \/>\n\t\tbegin<br \/>\n\t\t\tprint  'Executing DROP TRIGGER '+@name + ' ('+@assemblyName+')'<br \/>\n\t\t\texecute ('drop trigger '+@name)<br \/>\n\t\tend<\/p>\n<p>\t\tif @type_desc = 'CLR_SCALAR_FUNCTION'<br \/>\n\t\tbegin<br \/>\n\t\t\tprint  'Executing DROP FUNCTION '+@name + ' ('+@assemblyName+')'<br \/>\n\t\t\texecute ('drop function '+@name)<br \/>\n\t\tend<br \/>\n\t fetch next from mf_getprocs_cursor into @name, @type_desc<br \/>\n\tend<br \/>\n\tclose mf_getprocs_cursor<br \/>\n\tDEALLOCATE mf_getprocs_cursor    <\/p>\n<p>\tdeclare mf_gettypes_cursor cursor local for<br \/>\n\t SELECT so.name from sys.assembly_types  so with (nolock)<br \/>\n\t\tJOIN sys.assemblies sa with (nolock) on so.assembly_id = sa.assembly_id<br \/>\n\t\tWHERE ( sa.name = @assemblyName)<\/p>\n<p>\topen mf_gettypes_cursor<br \/>\n\tfetch next from mf_gettypes_cursor into @name<br \/>\n\twhile (@@fetch_status <> -1)<br \/>\n\tbegin<br \/>\n\t\tprint 'Executing DROP TYPE '+@name+' ('+@assemblyName+')'<br \/>\n\t\texecute ('drop type '+@name)<br \/>\n\t fetch next from mf_gettypes_cursor into @name<br \/>\n\tend<br \/>\n\tclose mf_gettypes_cursor<br \/>\n\tDEALLOCATE mf_gettypes_cursor   <\/p>\n<p>\tif exists (select name from sys.assemblies where name=@assemblyName)<br \/>\n\tbegin<br \/>\n\t\tprint 'Executing DROP ASSEMBLY ('+@assemblyName+')'<br \/>\n\t\texec ('drop assembly '+@assemblyName)<br \/>\n\tend<\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I have had the pleasure of working on SQL Server 2008 R2 using the SQLCLR support. Which I must admit it is quite nice to use, though a couple of things have anoyed me while using&#8230; one of which &hellip; <a href=\"http:\/\/www.gennard.net\/blog\/2010\/10\/tsql-sqlclr-drop-assembly\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,30],"tags":[208,90,173],"_links":{"self":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/677"}],"collection":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/comments?post=677"}],"version-history":[{"count":0,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/677\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/media?parent=677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/categories?post=677"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/tags?post=677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}