IF EXISTS (SELECT name FROM sys.assemblies WHERE name = N'dws.Regression')
BEGIN
	DECLARE @error int
	SET @error = 0

	DECLARE @moduleId sysname
	DECLARE @moduleName sysname
	DECLARE @moduleType char(2)
	DECLARE @moduleClass tinyint
	DECLARE assemblyModules CURSOR FAST_FORWARD FOR
		SELECT t.object_id, t.name, t.type, t.parent_class as class
			FROM sys.triggers t
			INNER JOIN sys.assembly_modules m ON t.object_id = m.object_id
			INNER JOIN sys.assemblies a ON m.assembly_id = a.assembly_id
			WHERE a.name = N'dws.Regression'
		UNION
		SELECT o.object_id, o.name, o.type, NULL as class
			FROM sys.objects o
			INNER JOIN sys.assembly_modules m ON o.object_id = m.object_id
			INNER JOIN sys.assemblies a ON m.assembly_id = a.assembly_id
			WHERE a.name = N'dws.Regression'
	OPEN assemblyModules
	FETCH NEXT FROM assemblyModules INTO @moduleId, @moduleName, @moduleType, @moduleClass
	WHILE (@error = 0 AND @@FETCH_STATUS = 0)
	BEGIN
		DECLARE @dropModuleString nvarchar(256)
		IF (@moduleType = 'AF') SET @dropModuleString = N'AGGREGATE'
		IF (@moduleType = 'TA') SET @dropModuleString = N'TRIGGER'
		IF (@moduleType = 'FT' OR @moduleType = 'FS') SET @dropModuleString = N'FUNCTION'
		IF (@moduleType = 'PC') SET @dropModuleString = N'PROCEDURE'
		SET @dropModuleString = N'DROP ' + @dropModuleString + ' [' + REPLACE(@moduleName, ']', ']]') + ']'
		IF (@moduleType = 'TA' AND @moduleClass = 0)
		BEGIN
			SET @dropModuleString = @dropModuleString + N' ON DATABASE'
		END

		EXEC sp_executesql @dropModuleString
		FETCH NEXT FROM assemblyModules INTO @moduleId, @moduleName, @moduleType, @moduleClass
	END
	CLOSE assemblyModules
	DEALLOCATE assemblyModules
	
	DECLARE @typeId int
	DECLARE @typeName sysname
	DECLARE assemblyTypes CURSOR FAST_FORWARD
		FOR SELECT t.user_type_id, t.name
			FROM sys.assembly_types t
			INNER JOIN sys.assemblies a ON t.assembly_id = a.assembly_id
			WHERE a.name = N'dws.Regression'
	OPEN assemblyTypes
	FETCH NEXT FROM assemblyTypes INTO @typeId, @typeName
	WHILE (@error = 0 AND @@FETCH_STATUS = 0)
	BEGIN
		DECLARE @dropTypeString nvarchar(256)
		SET @dropTypeString = N'DROP TYPE [' + REPLACE(@typeName, ']', ']]') + ']'

		EXEC sp_executesql @dropTypeString
		FETCH NEXT FROM assemblyTypes INTO @typeId, @typeName
	END
	CLOSE assemblyTypes
	DEALLOCATE assemblyTypes

	IF (@error = 0)
		DROP ASSEMBLY [dws.Regression] WITH NO DEPENDENTS
END

DECLARE @cmd NVARCHAR(MAX)

SELECT	@cmd = 'DROP FUNCTION [' + SPECIFIC_SCHEMA +'].[' + SPECIFIC_NAME + ']'
FROM	INFORMATION_SCHEMA.PARAMETERS
WHERE	SPECIFIC_NAME = 'dwsCoordinate'

IF @@ROWCOUNT = 1
	EXEC(@cmd)

SELECT	@cmd = 'DROP FUNCTION [' + SPECIFIC_SCHEMA +'].[' + SPECIFIC_NAME + ']'
FROM	INFORMATION_SCHEMA.PARAMETERS
WHERE	SPECIFIC_NAME = 'dwsCoordinate2'

IF @@ROWCOUNT = 1
EXEC(@cmd)