Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I drop all functions from CLR Assembly before it's drop in SQL Server?

I am getting 'drop assembly failed because it is referenced by object' error. As I understand I need to drop all functions referenced to this assembly before it's drop. Is there any universal way how to do that?

like image 632
Dmitriy Grishin - dogrishin Avatar asked Feb 29 '16 06:02

Dmitriy Grishin - dogrishin


People also ask

How can check CLR assembly permission set in SQL Server?

To determine if CLR is enabled, execute the following commands: EXEC SP_CONFIGURE 'show advanced options', '1'; RECONFIGURE WITH OVERRIDE; EXEC SP_CONFIGURE 'clr enabled';

Where are CLR assemblies stored?

Deployed User CLR assemblies are stored in the database you deploy them to, not on the file system. the column called content contains binary data is the assembly.

What is CLR assembly in SQL Server?

NET Framework common language runtime (CLR), instead of in Transact-SQL. An assembly in SQL Server is an object that references a managed application module (. dll file) that was created in the . NET Framework common language runtime. An assembly contains class metadata and managed code.

What are CLR functions in SQL Server?

CLR functions can be used to access external resources such as files, network resources, Web Services, other databases (including remote instances of SQL Server). This can be achieved by using various classes in the . NET Framework, such as System.IO , System.


1 Answers

You'll need a little script like that:

DECLARE @sql NVARCHAR(MAX) = N'';
DECLARE @assembly_name NVARCHAR(MAX)='assembly'
SELECT @sql += '
DROP ' + 
CASE
 WHEN o.type='PC' THEN 'PROCEDURE ' 
 ELSE 'FUNCTION '
END
+ QUOTENAME(o.Name)
+ ';'
FROM Sys.Assemblies asm
INNER JOIN SYS.ASSEMBLY_MODULES m ON asm.assembly_id=m.assembly_id
INNER JOIN SYS.OBJECTS o ON m.object_id = o.object_id
WHERE asm.name=@assembly_name
SET @sql=@sql+'
DROP ASSEMBLY '+QUOTENAME(@assembly_name)

PRINT @sql;
EXEC sp_executesql @sql;

However, dropping all assembly's dependent objects is not safe, so take care what will be deleted.

like image 147
cyberj0g Avatar answered Oct 12 '22 23:10

cyberj0g