Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_IHscriptindexes

  No additional text.


Syntax


-- Name:
--          sp_IHscriptindexes

-- Description:
--          Script indexes

-- Inputs:
--			@article_id		== article id
--			@useAlterTable	== 0 - Use constraint syntax (create table) - UNIQUE indexes/constraints only
--							   1 - Use ALTER TABLE syntax
--			@useUnique		== 0 - Filter out UNIQUE constraints/indexes
--							   1 - Include UNIQUE constraints/indexes
-- Security:
--          Internal (used by script generation)

-- Returns:
--          Success or failure
--			Temp table (#proctext) with commands

-- Owner:
--          

CREATE PROCEDURE sys.sp_IHscriptindexes
(
	@article_id		int,
	@useAlterTable	bit = 1,
	@useUnique		bit = 0
)
AS
BEGIN
	declare @retcode				int
	declare @cmd          			nvarchar(4000)
	declare @dest_owner				nvarchar(255)
	declare @dest_tabname			sysname
	declare @src_tabname			sysname
	declare @constraint_name		sysname
	declare @unq_constraint_name	sysname
	declare @idx_type				sysname
	declare @spacer       			nvarchar(1)
	declare @colname      			sysname
	declare @unique       			nvarchar(10)
	declare @cmd_sep      			nvarchar(10)
	declare @idx_count				int

	SET @retcode = 0
	
	-- Check options
	-- If scripting as a CONSTRAINT, UNIQUE must be
	-- allowed or no work to do
	IF (@useAlterTable = 0 AND @useUnique = 0)
	BEGIN
		SET @retcode = 1
		GOTO FINISHIDX
	END
	
	-- Get article info
	select	@dest_owner		= iha.dest_owner,
			@dest_tabname	= iha.dest_table,
			@src_tabname	= ihpt.name
	from	IHarticles iha,
			IHpublishertables ihpt
	where	article_id = @article_id
	
	if @dest_owner is not null
	begin
	    select @dest_owner = QUOTENAME(@dest_owner) + N'.'
	end
	else
	begin
	    select @dest_owner = N''
	end
	
	-- Pre-load the index counter based on primary key
	SELECT	@idx_count = COUNT(*)
	FROM	IHpublisherconstraints ihpc,
			IHarticles iha
	WHERE	ihpc.publisher_id	= iha.publisher_id
	  AND	ihpc.table_id		= iha.table_id
	  AND	iha.article_id		= @article_id
	
	-- Get indexes
	DECLARE hIdx CURSOR LOCAL FAST_FORWARD FOR
	select distinct
			ihpi.name,
			ihpi.name,
			ihpi.type
	from	IHpublisherindexes ihpi, IHarticles iha
	where	ihpi.publisher_id = iha.publisher_id
	  and	ihpi.table_id = iha.table_id
	  and	iha.article_id = @article_id

	OPEN	hIdx
	FETCH	hIdx
	INTO	@constraint_name,
			@unq_constraint_name,
			@idx_type

	WHILE (@@fetch_status <> -1)
	BEGIN
		-- Increment index counter
		set @idx_count = @idx_count + 1

		-- Check if index limit exceeded
		-- It doesn't matter if the indexes are UNIQUE keys
		-- or straight indexes - this should catch them either
		-- way.  It uses all the visited indexes in the counter.
		-- If the sum of unique keys + indexes > 256, must bail.
		-- It will also exit in the same place each time.
		IF @idx_count > 256
		BEGIN
			SET @retcode = 1
			RAISERROR(21675, 10, 1, 256, @idx_count)
			GOTO FINISHIDX
		END

		IF @useAlterTable = 1
		BEGIN
			-- Put command separator (if any) into buffer	
			select @cmd_sep = N'GO'
		END

		-- Determine index uniqueness
		if @idx_type = 'UNIQUE'
		begin
			select @unique = N'UNIQUE'
		end
		else
		begin
			select @unique = N''
		end
			
		-- Check if UNIQUE indexes should be included
		IF ((@useUnique = 0 AND @idx_type = 'UNIQUE')
			OR (@useAlterTable = 0 AND @idx_type != 'UNIQUE'))
		BEGIN
			-- Go to next index
			FETCH	hIdx
			INTO	@constraint_name,
					@unq_constraint_name,
					@idx_type

			CONTINUE
		END
			
		-- Verify index can be scripted
		exec @retcode = sys.sp_IHVerifyIndex
						@article_id		= @article_id,
						@index_name		= @constraint_name

		IF @retcode = 0 AND @@ERROR = 0
		BEGIN
			-- Generate unique constraint name
			SELECT @unq_constraint_name = sys.fn_IHGenerateUniqueName(@article_id, N'IDX', @idx_count)
		
			-- Create the index creation command
			IF @useAlterTable = 1
			BEGIN
				SELECT	@cmd =	N'CREATE ' + @unique
								+ N' INDEX '
								+ QUOTENAME(@unq_constraint_name)
								+ N' ON '
								+ @dest_owner
								+ QUOTENAME(@dest_tabname)
								+ N'('
			END
			ELSE
			BEGIN
				-- Use constraint syntax for UNIQUE indexes only!
				SELECT	@cmd =	N'CONSTRAINT '
								+ QUOTENAME(@unq_constraint_name) + ' '
								+ @unique + N' ('
			END

			insert into #proctext(procedure_text)
			values (@cmd)

			-- Get index column list
			DECLARE hArtCol CURSOR LOCAL FAST_FORWARD FOR
			SELECT	ihc.name
			FROM	IHcolumns ihc,
					IHpublishercolumns ihpc,
					IHpublishercolumnindexes ihpci,
					IHpublisherindexes ihpi,
					IHarticles iha
			WHERE	ihpi.publisher_id		= iha.publisher_id
			  AND	ihpi.table_id			= iha.table_id
			  AND	ihc.publishercolumn_id	= ihpc.publishercolumn_id
			  AND	ihpc.publishercolumn_id = ihpci.publishercolumn_id
			  AND	ihpi.publisherindex_id	= ihpci.publisherindex_id
			  AND	iha.article_id			= ihc.article_id
			  AND	iha.article_id			= @article_id
			  AND	ihpi.name				= @constraint_name
			ORDER BY ihpci.indid asc
		
			OPEN	hArtCol
			FETCH	hArtCol
			INTO 	@colname
		
			-- Create column list	
			select	@spacer = N' ',
					@cmd = N''
			
			WHILE (@@fetch_status <> -1)
			begin
				select @cmd = @cmd + @spacer + QUOTENAME(@colname)
				select @spacer = N','
			
				if len(@cmd) > 3000
				begin
					insert into #proctext(procedure_text) values(@cmd)
					select @cmd = N''
				end
		
				FETCH	hArtCol
				INTO 	@colname
			end
			
			CLOSE hArtCol
			DEALLOCATE hArtCol

			insert into #proctext(procedure_text) values( @cmd )
			insert into #proctext(procedure_text) values( N')' )
			
			IF @useAlterTable = 1
			BEGIN
				-- Write command separator
				insert into #proctext(procedure_text) values (@cmd_sep)
			END
			ELSE
			BEGIN
				-- Write ',' to separate commands
				insert into #proctext(procedure_text) values (',')
			END
		END
	
		FETCH	hIdx
		INTO	@constraint_name,
				@unq_constraint_name,
				@idx_type
	END

FINISHIDX:	
	CLOSE hIdx
	DEALLOCATE hIdx

	IF @useAlterTable = 0
	BEGIN
		DECLARE @proctext nvarchar(4000)
		
		-- Replace trailing ',' with ')'
		SELECT	@proctext = procedure_text
		FROM	#proctext
		WHERE	seq = IDENT_CURRENT('#proctext')
		
		IF RIGHT(@proctext, 1) = ','
		BEGIN
			UPDATE	#proctext
			SET		procedure_text = LEFT(procedure_text, LEN(procedure_text) - 1) + N')'
			WHERE 	seq = IDENT_CURRENT('#proctext')
		END
	END
	
	RETURN (@retcode)
END

 
Last revision 2008RTM
See also

  sp_IHScriptIdxFile (Procedure)
sp_IHScriptSchFile (Procedure)
sp_MSdrop_repltran (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