Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_dropdatatype

  No additional text.


Syntax


-- Name:
--		sp_dropdatatype

-- Description:
--		Drop DBMS data type

-- Returns:
--		0 if successful
--		1 if failed

-- Security:
--		public

-- Notes:
--		Implicitly will cascade delete any related mappings


CREATE PROCEDURE sys.sp_dropdatatype
(
	@dbms		sysname,
	@version	sysname = NULL,
	@type		sysname = '%'
)
AS
BEGIN
	DECLARE @dbms_id		int
	DECLARE @datatype_id	int
	DECLARE @retcode		int

    SET NOCOUNT ON

	
    -- Security Check: require sysadmin
	
    IF (ISNULL(IS_SRVROLEMEMBER('sysadmin'),0) = 0)
    BEGIN
        RAISERROR(21089,16,-1)
        RETURN (1)
    END

	-- Prepare dbms for case insensitive searches
	SET @dbms = UPPER(@dbms)

	BEGIN TRAN
	SAVE TRAN dropdatatype
	
	-- Get DBMS id
	SELECT	@dbms_id		= dbms.dbms_id
	FROM	msdb.dbo.MSdbms dbms
	WHERE	dbms.dbms		= @dbms
	  AND	dbms.version	= @version

	IF @dbms_id IS NULL OR @@ERROR <> 0
	BEGIN
		RAISERROR (21653, 16, -1, @dbms, @version)
		
		IF @@TRANCOUNT > 0
		BEGIN
			ROLLBACK TRAN dropdatatype
			COMMIT TRAN
		END
		RETURN (1)
	END

	IF @type != '%'
	BEGIN
		-- Get Datatype id
		SELECT	@datatype_id = datatype_id
		FROM	msdb.dbo.MSdbms_datatype
		WHERE	dbms_id	= @dbms_id
		  AND	UPPER(type COLLATE DATABASE_DEFAULT) =
			UPPER(@type) COLLATE DATABASE_DEFAULT

		IF @datatype_id IS NULL OR @@ERROR <> 0
		BEGIN
			RAISERROR (21654, 16, -1, @type)
			
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRAN dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END
		
		-- Clear destination default data type mappings
		UPDATE	msdb.dbo.MSdbms_map
		SET		default_datatype_mapping_id = NULL
		FROM	msdb.dbo.MSdbms_map map,
				msdb.dbo.MSdbms_datatype_mapping dm
		WHERE	map.map_id			= dm.map_id
		  AND	dm.dest_datatype_id	= @datatype_id

		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END

		-- Drop destination data type mappings
		DELETE FROM msdb.dbo.MSdbms_datatype_mapping
		WHERE dest_datatype_id = @datatype_id
		
		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END
		
		-- Clear source default data type mappings
		UPDATE	msdb.dbo.MSdbms_map
		SET		default_datatype_mapping_id = NULL
		WHERE	src_datatype_id = @datatype_id

		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END

		-- Drop source maps
		DELETE	msdb.dbo.MSdbms_datatype_mapping
		FROM	msdb.dbo.MSdbms_map map,
				msdb.dbo.MSdbms_datatype_mapping dm
		WHERE	map.map_id			= dm.map_id
		  AND	map.src_datatype_id	= @datatype_id
		
		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END
		
		DELETE FROM msdb.dbo.MSdbms_map
		WHERE src_datatype_id = @datatype_id

		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END
		
		-- Drop DBMS data type
		DELETE FROM msdb.dbo.MSdbms_datatype
		WHERE datatype_id = @datatype_id	

		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END
	END
	ELSE
	BEGIN
		-- Clear default data type mappings
		UPDATE	msdb.dbo.MSdbms_map
		SET		default_datatype_mapping_id = NULL
		WHERE	src_dbms_id = @dbms_id

		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END

		-- Drop all data type mappings
		DELETE	msdb.dbo.MSdbms_datatype_mapping
		FROM	msdb.dbo.MSdbms_map map,
				msdb.dbo.MSdbms_datatype_mapping dm
		WHERE	map.map_id = dm.map_id
		  AND	(
					map.src_dbms_id = @dbms_id OR
					map.dest_dbms_id = @dbms_id
				)
		
		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END

		-- Drop all maps
		DELETE FROM msdb.dbo.MSdbms_map
		WHERE	src_dbms_id  = @dbms_id
		   OR	dest_dbms_id = @dbms_id
		
		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END

		-- Drop DBMS data type
		DELETE FROM msdb.dbo.MSdbms_datatype
		WHERE dbms_id = @dbms_id

		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END
	END
	
	-- Fixup defaults for any dropped default mappings
	exec @retcode = sys.sp_MSrepl_fixupdefaultmappings
	IF @retcode <> 0 OR @@ERROR <> 0
	BEGIN
		IF @@TRANCOUNT > 0
		BEGIN
			ROLLBACK TRANSACTION dropdatatype
			COMMIT TRAN
		END
		RETURN (1)
	END
	
	-- Verify that there are still any remaining data types for the DBMS
	IF NOT EXISTS
	(
		SELECT	datatype_id
		FROM	msdb.dbo.MSdbms_datatype
		WHERE	dbms_id = @dbms_id
	)
	BEGIN
		-- Drop DBMS since there is nothing left
		DELETE FROM msdb.dbo.MSdbms
		WHERE dbms_id = @dbms_id
		
		IF @@ERROR <> 0
		BEGIN
			IF @@TRANCOUNT > 0
			BEGIN
				ROLLBACK TRANSACTION dropdatatype
				COMMIT TRAN
			END
			RETURN (1)
		END
	END
	
	COMMIT TRAN
	RETURN (0)		
END

 
Last revision 2008RTM
See also

  sp_dropdatatypemapping (Procedure)
sp_MSdrop_replcom (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash