Labels

Apache Hadoop (3) ASP.NET (2) AWS S3 (2) Batch Script (3) BigQuery (21) BlobStorage (1) C# (3) Cloudera (1) Command (2) Data Model (3) Data Science (1) Django (1) Docker (1) ETL (7) Google Cloud (5) GPG (2) Hadoop (2) Hive (3) Luigi (1) MDX (21) Mongo (3) MYSQL (3) Pandas (1) Pentaho Data Integration (5) PentahoAdmin (13) Polybase (1) Postgres (1) PPS 2007 (2) Python (13) R Program (1) Redshift (3) SQL 2016 (2) SQL Error Fix (18) SQL Performance (1) SQL2012 (7) SQOOP (1) SSAS (20) SSH (1) SSIS (42) SSRS (17) T-SQL (75) Talend (3) Vagrant (1) Virtual Machine (2) WinSCP (1)

Thursday, July 28, 2011

SSIS Control Flow Items and Uses

Data Flow Task

  • Data Flow Task: This task extracts data from a source, allows for transformations of that data, and then the data is loaded into a target data destination.

Data Preparation Tasks

  • File System Task: This task allows the user to copy/move/delete files and directories on a file system.
  • FTP Task: This task allows the user to copy/move/delete files and directories over FTP.
  • Web Service Task: This task allows the user to execute a Web service method and store the results
  • XML Task: This task is used to work with XML data. XSLT can be used along with XPath to validate, compare and merge documents. The results of this can then be stored.
  • Data Profiling Task: This task can be used for checking and validating data quality. Profiles can be set up and checked for varius data quality issues such as, column length issues, column patterns, column statics, etc.

Workflow Tasks

  • Execute Package Task: This task will run other SQL Server Integration Services packages.
  • Execute Process Task: This task will execute an application or batch file.
  • Message Queue Task: This task allows you to send and receive messages between SSIS packages, or to send messages to an application via an application queue. This task uses Message Queuing (MSMQ)
  • Send Mail Task: This task allows for email messages to be created and sent using an SMTP server.
  • WMI Data Reader Task: This task allows a Package to query, using WQL, computer systems (local and remote) for information regarding that computer.
  • WMI Event Watcher Task: This task watches for WMI events that have occurred on a computer system, and allows the package to take an action if certain criteria are met.

Scripting Tasks

  • Script Task: This task can be used to program functions that are not available in the standard SSIS tasks or transformations. In SSIS 2005 this task can be programmed in VB .NET. In SSIS 2008 VB .NET and C# can be used to program a Script Task.

SQL Server Maintenance Tasks

  • Back Up Database Task: This task will allow you to backup a one or many SQL Server databases.
  • Check Database Integrity Task: This task will allow you to check the integrity of all the objects in one or many SQL Server databases.
  • Execute SQL Server Agent Job Task: This task allows for the execution of a SQL Server Agent job.
  • Execute T-SQL Statement Task: This task is similar to the Execute SQL Task, however it only supports Transact SQL Statements. It should be used for SQL Server specific SQL statements.
  • History Cleanup Task: This task allows for the cleanup of historical activity data. It will cleanup the history for database maintenance plans, backup activites, restore activities and SQL Server agent jobs.
  • Maintenance Cleanup Task: This task allows for the cleanup of backup files, and the reports of maintenance plans.
  • Notify Operator Task: This task allows SSIS to notify SQL Server Agent operators. They can be notifies by email, pager, or netsend.
  • Rebuild Index Task: This task will rebuild an index or indexes on one or many databases.
  • Reorganize Index Task: This task will reorganize an index or indexes on one or many databases.
  • Shrink Database Task: This task will shrink the size of the SQL Server database data and database log files.
  • Update Statistics Task: This task will update the statistics for one of many tables in one or many databases.

SQL Server Tasks

  • Bulk Insert Task: This task offers an efficient way to copy large volumes of data.
  • Execute SQL Task: This task allows the execution of a SQL statement. If the statement returns results, they can be stored in a variable.
  • Transfer Database Task: This task will copy or move a SQL Server database between two instances of SQL Server. It can even be used to make a copy of a database on the same server. Databases can be copied either online or offline.
  • Transfer Error Messages Task: This task will transfer a single or multiple SQL Server user defined error messages between SQL Server instances. It can be setup to transfer specific user messages or all error messages.
  • Transfer Jobs Task: This task will transfer a single or multiple SQL Server Agent jobs between SQL Server instances.
  • Transfer Logins Task: This task will transfer a single or multiple SQL Server logins between SQL Server instances.
  • Transfer Master Stored Procedures Task: This task will transfer a single or multiple SQL Server Master database stored procedures between SQL Server instances.
  • Transfer SQL Server Objects Task: This task will transfer a single or multiple SQL Server database objects between SQL Server instances. Most of SQL Servers DDL objects can be copied with this task.

Analysis Services Tasks

  • Analysis Services Execute DDL Task: This task will run data definition language statements on Analysis Services. This allows for the create, drop, alter of cubes, dimensions and mining models.
  • Analysis Services Processing Task: This task will process Analysis Services Cubes, Dimensions, and Mining Models.
  • Data Mining Query Task: This task will run a DMX (Data Mining Extensions) query that create a prediction based on new data that is run against a Analysis Services data mining model.

Friday, July 8, 2011

Fastest Performing Rowcount SQL Query

Try executing the below queries in a table containing millins of records. Both the queries yields row count of the table, but the performance of Query 1 is more.

Query 1:


SELECT SUM(row_count) FROM Sys.dm_db_partition_stats
WHERE OBJECT_ID = OBJECT_ID('AccountCommunications')
AND (index_id =0 or index_id =1);

Query 2:
Select COUNT(*) From dbo.AccountCommunications

Tuesday, July 5, 2011

Drop and Create Primary Key, Foreign Key, Indexes in SSIS dynamically

I managed to create SSIS POC  for Drop & Create constraints and indexes dynamically.

I have created the following tables and stored procedures to arrive at the solution:

Two tables to store Index and Constraints details:
    1.  dbo.TableConstraints: This table holds the detail of all constraints (PK's & FK's)  created in a database.
    2. dbo.TableIndex: This table contains the detail of all indexes created in a database.

Nine stored procedures to drop and create:
    1. spInsertConstraints : Execute this procedure to populate 'dbo.TableConstraints', when you make any changes to table or database structure this has to be executed.
    2. spInsertIndexes: Execute this procedure to populate 'dbo.TableIndex', when you make any changes to table or database structure this has to be executed.
    3. spDropRefFKs: Drops all reference relationship in a table.
    4. spDropPKFKConstarints: Drops all constraints in a table.
    5. spDropAllIndexes: Drops all indexes in a table.
    6. spBuildPKConstarints: Rebuilds PK in atable (gets details from 'dbo.TableConstarints').
    7. spBuildFKConstarints: Rebuilds all Foreign Keys in a table (gets details from 'dbo.TableConstarints').
    8. spBuildFKRefs: Rebuilds all reference relationship in a table (gets details from 'dbo.TableConstarints').
    9. spBuildAllIndexes: Rebuilds all indexes in a table (gets index details from 'dbo.TableIndex').

Initial steps to be followed:

    1. Execute stored procedure 'spInsertConstraints'. It is one time run until there is no changes in database. If you made any changes to database or tables, this has to be re-executed.
    2. Execute stored procedure 'spInsertIndexes'. It is one time run until there is no changes in database. If you made any changes to database or tables, this has to be re-executed.

Script for Stored Procedure

USE [ListSelect20_be]
GO
/****** Object:  Table [dbo].[TableIndex]    Script Date: 07/05/2011 12:32:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableIndex](
 [ObjectID] [bigint] NULL,
 [TableName] [varchar](100) NULL,
 [SchemaName] [varchar](50) NULL,
 [IndexName] [varchar](100) NULL,
 [IndexID] [int] NULL,
 [IndexType] [int] NULL,
 [IXDescription] [varchar](100) NULL,
 [ColumnName] [varchar](100) NULL,
 [IsUnique] [varchar](50) NULL,
 [IsUniqueConstraint] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[TableConstraints]    Script Date: 07/05/2011 12:32:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableConstraints](
 [ObjectID] [bigint] NULL,
 [Constraint_Catalog] [varchar](100) NULL,
 [Constraint_Schema] [varchar](100) NULL,
 [Constraint_Name] [varchar](100) NULL,
 [Table_Catalog] [varchar](100) NULL,
 [Table_Schema] [varchar](100) NULL,
 [Table_name] [varchar](100) NULL,
 [Constraint_Type] [varchar](100) NULL,
 [Is_Deferrable] [nchar](10) NULL,
 [Initially_Deferrable] [nchar](10) NULL,
 [Column_Name] [varchar](100) NULL,
 [Ordinal_Position] [int] NULL,
 [TypeDesc] [varchar](100) NULL,
 [Is_Unique] [varchar](50) NULL,
 [Is_Unique_Constraint] [varchar](50) NULL,
 [ParentObjectID] [bigint] NULL,
 [ParentObject] [varchar](100) NULL,
 [ReferenceObjectID] [bigint] NULL,
 [ReferenceObject] [varchar](100) NULL,
 [ReferenceColumnID] [bigint] NULL,
 [ReferenceColumn] [varchar](100) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  StoredProcedure [dbo].[spDropPKFKConstarints]    Script Date: 07/05/2011 12:32:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ========================================================
-- Author   : Mahadevan
-- Create date: June 23, 2011
-- Description: Remove all constraints PK and FK in a table
-- =========================================================
CREATE PROCEDURE [dbo].[spDropPKFKConstarints]
@Database VARCHAR(50),
@Schema VARCHAR(50),
@Tablename VARCHAR(50)
AS
BEGIN

 SET NOCOUNT ON;
    BEGIN
  DECLARE @SQL NVARCHAR(255)
  WHILE EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_CATALOG = @Database
      AND TABLE_NAME = @Tablename AND CONSTRAINT_SCHEMA = @Schema)
  BEGIN
   SELECT    @SQL = 'ALTER TABLE ' + @Tablename + ' DROP CONSTRAINT ' + CONSTRAINT_NAME
   FROM    INFORMATION_SCHEMA.TABLE_CONSTRAINTS
   WHERE   CONSTRAINT_CATALOG = @Database AND TABLE_NAME = @Tablename AND CONSTRAINT_SCHEMA = @Schema
   ORDER BY CONSTRAINT_TYPE DESC
   EXEC    sp_executesql @sql
  END
 END
END
GO
/****** Object:  StoredProcedure [dbo].[spDropAllIndexes]    Script Date: 07/05/2011 12:32:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =================================================
-- Author:  Mahadevan
-- Create date: 26/6/2011
-- Description: To drop all the indexes in a table
-- =================================================
CREATE PROCEDURE [dbo].[spDropAllIndexes]

 @Tablename VARCHAR(50)
AS
BEGIN

 SET NOCOUNT ON;

 DECLARE @IndexName VARCHAR(128)
 DECLARE @Table VARCHAR(128)
 DECLARE [GetIndexes] CURSOR FOR
  SELECT  I.[name] AS [Index],
     S.[name] AS [Table]
  FROM  [sysindexes] I
  INNER JOIN [sysobjects] S
  ON   I.[id] = S.[id]
  WHERE  I.[name] IS NOT NULL
  AND   S.[type] = 'U'
  AND   I.INDID > 0
  AND   I.INDID < 255
  AND   (I.STATUS & 64)=0
  AND S.[Name] = @Tablename
  ORDER BY [Table]
 OPEN [GetIndexes]
 FETCH NEXT FROM [GetIndexes] INTO @IndexName, @Table
 WHILE @@FETCH_STATUS = 0
 BEGIN
  DECLARE @SQL NVARCHAR(255)
  BEGIN
  SELECT @SQL =  'DROP INDEX [' + @IndexName + '] ON [' + @Table + '] '
        + 'WITH ( ONLINE = OFF );'
  END
  --PRINT @SQL
  EXEC    sp_executesql @SQL
  FETCH NEXT FROM [GETIndexes] INTO @IndexName, @Table
 END
 CLOSE  [GetIndexes]
 DEALLOCATE [GetIndexes]
  
END
GO
/****** Object:  StoredProcedure [dbo].[spBuildPKConstarints]    Script Date: 07/05/2011 12:32:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ========================================================
-- Author   : Mahadevan
-- Create date: June 24, 2011
-- Description: Create PK in a table
-- =========================================================
CREATE PROCEDURE [dbo].[spBuildPKConstarints]

@Tablename VARCHAR(50)
AS
BEGIN

 SET NOCOUNT ON;
    BEGIN
  DECLARE @Table VARCHAR(50)
  DECLARE @ConstraintName VARCHAR(50)
  DECLARE @ConstraintDesc VARCHAR(50)
  DECLARE @ConstraintColumn1 VARCHAR(50)
  DECLARE @ConstraintColumn2 VARCHAR(50)
  
  DECLARE [GetConstraints] CURSOR FOR
    SELECT  A.TABLE_NAME
      ,A.CONSTRAINT_NAME
      ,A.TYPEDESC
      ,A.COLUMN_NAME AS Column1
      ,B.Column_Name AS Column2
    FROM    TableConstraints A
    LEFT JOIN (SELECT OBJECTID, Column_Name FROM TableConstraints WHERE Ordinal_Position = 2) B ON B.OBJECTID = A.ObjectID
    WHERE  A.Ordinal_Position=1 AND  A.Table_name = @Tablename AND   A.Constraint_Type = 'PRIMARY KEY'
  OPEN [GetConstraints]
  FETCH NEXT FROM [GetConstraints] INTO @Table, @ConstraintName,  @ConstraintDesc, @ConstraintColumn1, @ConstraintColumn2
  WHILE @@FETCH_STATUS = 0
  BEGIN
   DECLARE @SQL NVARCHAR(1000)
   IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = @Table
    AND CONSTRAINT_NAME = @ConstraintName)
   BEGIN
   IF (@ConstraintColumn2 IS NULL)
   SELECT    @SQL = 'ALTER TABLE ' + @Table + ' ADD CONSTRAINT '+ @ConstraintName
         + ' PRIMARY KEY '  + @ConstraintDesc + '(' + @ConstraintColumn1 + ' ASC)'
         + ' WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF'
         + ',SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF'
         + ',ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY];'

   ELSE
   SELECT    @SQL = 'ALTER TABLE ' + @Table + ' ADD CONSTRAINT '+ @ConstraintName
         + ' PRIMARY KEY '  + @ConstraintDesc + '(' + @ConstraintColumn1 +','+ @ConstraintColumn2+')'
         + ' WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF'
         + ',SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF'
         + ',ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY];'
   END
   --Print @SQL
   EXEC sp_executesql @SQL
   FETCH NEXT FROM [GetConstraints] INTO @Table, @ConstraintName,  @ConstraintDesc, @ConstraintColumn1, @ConstraintColumn2
  END
  CLOSE  [GetConstraints]
  DEALLOCATE [GetConstraints]
 END
END
GO
/****** Object:  StoredProcedure [dbo].[spBuildFKRefs]    Script Date: 07/05/2011 12:32:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ======================================================
-- Author:  Mahadevan
-- Create date: 26/6/2011
-- Description: To rebuild all the FK's in a table
-- ======================================================
CREATE PROCEDURE [dbo].[spBuildFKRefs]

 @Tablename VARCHAR(50)
AS
BEGIN

 SET NOCOUNT ON;

 BEGIN
 DECLARE @Table  VARCHAR(128)
 DECLARE @ConstraintName VARCHAR(128)
 DECLARE @ColumnName VARCHAR(128)
 DECLARE @ReferenceObject VARCHAR(128)
 DECLARE @ReferenceColumn VARCHAR(128)
 DECLARE [GetConstraints] CURSOR FOR
  SELECT  Table_name
     ,Constraint_Name
     ,Column_Name
     ,ReferenceObject
     ,ReferenceColumn
  FROM TableConstraints
  WHERE ReferenceObject = @Tablename AND Constraint_Type = 'FOREIGN KEY'
 OPEN [GetConstraints]
 FETCH NEXT FROM [GetConstraints] INTO  @Table, @ConstraintName, @ColumnName, @ReferenceObject, @ReferenceColumn
 WHILE @@FETCH_STATUS = 0
 BEGIN
  DECLARE @SQL NVARCHAR(255)
  IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(@ConstraintName)
  AND parent_object_id = OBJECT_ID(@Table))
  BEGIN
  SELECT @SQL =  'ALTER TABLE ' + @Table + '  WITH  CHECK  ADD CONSTRAINT ' + @ConstraintName
      + ' FOREIGN KEY ('+ @ColumnName +') REFERENCES '+ @ReferenceObject
      + ' ('+ @ReferenceColumn +') ON UPDATE  NO ACTION  ON DELETE  NO ACTION'
  END
  --PRINT @SQL
  EXEC    sp_executesql @SQL
  FETCH NEXT FROM [GetConstraints] INTO  @Table, @ConstraintName, @ColumnName, @ReferenceObject, @ReferenceColumn
 END
 CLOSE  [GetConstraints]
 DEALLOCATE [GetConstraints]

END
  
END
GO
/****** Object:  StoredProcedure [dbo].[spBuildFKConstarints]    Script Date: 07/05/2011 12:32:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ======================================================
-- Author:  Mahadevan
-- Create date: 26/6/2011
-- Description: To rebuild all the FK's in a table
-- ======================================================
CREATE PROCEDURE [dbo].[spBuildFKConstarints]

 @Tablename VARCHAR(50)
AS
BEGIN

 SET NOCOUNT ON;

 BEGIN
 DECLARE @Table  VARCHAR(128)
 DECLARE @ConstraintName VARCHAR(128)
 DECLARE @ColumnName VARCHAR(128)
 DECLARE @ReferenceObject VARCHAR(128)
 DECLARE @ReferenceColumn VARCHAR(128)
 DECLARE [GetConstraints] CURSOR FOR
  SELECT  Table_name
     ,Constraint_Name
     ,Column_Name
     ,ReferenceObject
     ,ReferenceColumn
  FROM TableConstraints
  WHERE Table_Name = @Tablename AND Constraint_Type = 'FOREIGN KEY'
 OPEN [GetConstraints]
 FETCH NEXT FROM [GetConstraints] INTO  @Table, @ConstraintName, @ColumnName, @ReferenceObject, @ReferenceColumn
 WHILE @@FETCH_STATUS = 0
 BEGIN
  DECLARE @SQL NVARCHAR(255)
  IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(@ConstraintName)
  AND parent_object_id = OBJECT_ID(@Table))
  BEGIN
  SELECT @SQL =  'ALTER TABLE ' + @Table + '  WITH  CHECK  ADD CONSTRAINT ' + @ConstraintName
      + ' FOREIGN KEY ('+ @ColumnName +') REFERENCES '+ @ReferenceObject
      + ' ('+ @ReferenceColumn +') ON UPDATE  NO ACTION  ON DELETE  NO ACTION'
  END
  --PRINT @SQL
  EXEC    sp_executesql @SQL
  FETCH NEXT FROM [GetConstraints] INTO  @Table, @ConstraintName, @ColumnName, @ReferenceObject, @ReferenceColumn
 END
 CLOSE  [GetConstraints]
 DEALLOCATE [GetConstraints]

END
  
END
GO
/****** Object:  StoredProcedure [dbo].[spBuildAllIndexes]    Script Date: 07/05/2011 12:32:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =================================================
-- Author:  Mahadevan
-- Create date: 26/6/2011
-- Description: To create all the indexes in a table
-- =================================================
CREATE PROCEDURE [dbo].[spBuildAllIndexes]

 @Tablename VARCHAR(50)
AS
BEGIN

 SET NOCOUNT ON;

 DECLARE @Table VARCHAR(50)
 DECLARE @IndexName VARCHAR(50)
 DECLARE @Type VARCHAR(50)
 DECLARE @Column1 VARCHAR(50)
 DECLARE @Column2 VARCHAR(50)
 DECLARE @Column3 VARCHAR(50)
 DECLARE @Column4 VARCHAR(50)
 DECLARE @Unique VARCHAR(50)

 DECLARE [GetIndexes] CURSOR FOR
  SELECT T1.Tablename
    ,T1.IndexName
    ,T1.IXDescription
    ,T1.ColumnName AS Column1
    ,T2.ColumnName AS Column2
    ,T3.ColumnName AS Column3
    ,T4.ColumnName AS Column4
    ,T1.IsUnique
  FROM (SELECT RANK() OVER (Order BY ColumnName ASC) AS SNO
     ,ObjectID
     ,Tablename
     ,IndexName
     ,IXDescription
     ,ColumnName
     ,IsUnique
    FROM TableIndex
    WHERE TableName = @Tablename AND IXDescription = 'NONCLUSTERED') T1
  LEFT JOIN (SELECT RANK() OVER (ORDER BY ColumnName ASC) AS SNO, ObjectID, ColumnName FROM TableIndex
       WHERE TableName = @Table AND IXDescription = 'NONCLUSTERED') T2 ON T2.ObjectID = T1.ObjectID AND T2.SNO = T1.Sno +1
  LEFT JOIN (SELECT RANK() OVER (ORDER BY ColumnName ASC) AS SNO, ObjectID, ColumnName FROM TableIndex
       WHERE TableName = @Table AND IXDescription = 'NONCLUSTERED') T3 ON T3.ObjectID = T2.ObjectID AND T3.SNO = T2.Sno +1
  LEFT JOIN (SELECT RANK() OVER (ORDER BY ColumnName ASC) AS SNO, ObjectID, ColumnName FROM TableIndex
       WHERE TableName = @Table AND IXDescription = 'NONCLUSTERED') T4 ON T4.ObjectID = T3.ObjectID AND T4.SNO = T3.Sno +1
  WHERE T1.SNO = 1
 OPEN [GetIndexes]
 FETCH NEXT FROM [GetIndexes] INTO @Table,  @IndexName,  @Type, @Column1, @Column2, @Column3, @Column4, @Unique
 WHILE @@FETCH_STATUS = 0
 BEGIN
  DECLARE @SQL NVARCHAR(1000)
  IF NOT EXISTS (SELECT * from sys.indexes WHERE Name = (@IndexName)
      AND object_id = OBJECT_ID(@Table))
  BEGIN
  IF (@Column2 IS NULL)
  BEGIN
  SELECT @SQL =  'CREATE ' +@Unique+ ' '+ @Type + ' INDEX [' + @IndexName + '] ON [' + @Table + '] '
      +'(['+@Column1+'] ASC)'
      + 'WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF,'
      + 'IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY];'
   END
  IF (@Column2 IS NOT NULL AND @Column3 IS NULL)
  BEGIN
  SELECT @SQL =  'CREATE ' +@Unique+ ' '+ @Type + ' INDEX [' + @IndexName + '] ON [' + @Table + '] '
      +'('+@Column1+','+@Column2+')'
      + 'WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF,'
      + 'IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY];'
   END
   IF (@Column2 IS NOT NULL AND @Column3 IS NOT NULL AND @Column4 IS NULL)
   BEGIN
   SELECT @SQL =  'CREATE ' +@Unique+ ' '+ @Type + ' INDEX [' + @IndexName + '] ON [' + @Table + '] '
      +'('+@Column1+','+@Column2+','+@Column3+')'
      + 'WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF,'
      + 'IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY];'
   END
   ELSE
   BEGIN
   SELECT @SQL =  'CREATE ' +@Unique+ ' '+ @Type + ' INDEX [' + @IndexName + '] ON [' + @Table + '] '
      +'('+@Column1+','+@Column2+','+@Column3+','+@Column4+')'
      + 'WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF,'
      + 'IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY];'
   END
   END
     --Print @SQL
  EXEC SP_EXECUTESQL @SQL
  FETCH NEXT FROM [GETIndexes] INTO @Table,  @IndexName,  @Type, @Column1, @Column2, @Column3, @Column4, @Unique
 END
 CLOSE  [GetIndexes]
 DEALLOCATE [GetIndexes] 
  
END
GO
/****** Object:  StoredProcedure [dbo].[spInsertIndexes]    Script Date: 07/05/2011 12:32:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ====================================================================================
-- Author:  Mahadevan
-- Create date: June 22, 2011
-- Description: To insert all indexes used in a database to table Tableindex
-- ====================================================================================
CREATE PROCEDURE [dbo].[spInsertIndexes]

AS
BEGIN

 SET NOCOUNT ON;
    
     TRUNCATE TABLE dbo.Tableindex
    
     BEGIN
  INSERT INTO   [dbo].[TableIndex]
       (ObjectID
       ,TableName
       ,SchemaName
       ,IndexName
       ,IndexID
       ,IndexType
       ,IXDescription
       ,ColumnName
       ,IsUnique
       ,IsUniqueConstraint)
         (SELECT I.OBJECT_ID
     ,T.NAME TABLE_NAME
     ,S.NAME SCHEMA_NAME
     ,I.NAME INDEX_NAME
     ,I.index_id INDEX_ID
     ,I.type INDEX_TYPE
     ,I.type_desc AS IndexDescription
     ,C.NAME COLUMN_NAME  
     ,CASE I.is_unique WHEN 1 Then 'UNIQUE' ELSE '' END AS IS_UNIQUE
     ,CASE I.is_unique_constraint WHEN 1 Then 'UNIQUE CONSTRAINT' ELSE '' END AS IS_UNIQUE_CONSTRAINT
   FROM SYS.TABLES T       
   INNER JOIN SYS.SCHEMAS S     ON T.SCHEMA_ID = S.SCHEMA_ID       
   INNER JOIN SYS.INDEXES I     ON I.OBJECT_ID = T.OBJECT_ID       
   INNER JOIN SYS.INDEX_COLUMNS IC     ON IC.OBJECT_ID = T.OBJECT_ID       
   INNER JOIN SYS.COLUMNS C     ON C.OBJECT_ID  = T.OBJECT_ID   
   AND IC.INDEX_ID    = I.INDEX_ID AND IC.COLUMN_ID = C.COLUMN_ID 
   WHERE 1=1   AND I.type = 2  AND (S.name = 'dbo')
   )
    END
    
   
END
GO
/****** Object:  StoredProcedure [dbo].[spInsertConstraints]    Script Date: 07/05/2011 12:32:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ====================================================================================
-- Author:  Mahadevan
-- Create date: June 22, 2011
-- Description: To insert all constarints used in a database to table TableConstraints
-- ====================================================================================
CREATE PROCEDURE [dbo].[spInsertConstraints]

@Database Varchar(50),
@Schema Varchar(50)

AS
BEGIN

 SET NOCOUNT ON;
    
     TRUNCATE TABLE dbo.TableConstraints
    
     BEGIN
     INSERT INTO dbo.TableConstraints
           (ObjectID
           ,Constraint_Catalog
           ,Constraint_Schema
           ,Constraint_Name
           ,Table_Catalog
           ,Table_Schema
           ,Table_name
           ,Constraint_Type
           ,Is_Deferrable
           ,Initially_Deferrable
           ,Column_Name
           ,Ordinal_Position
           ,TypeDesc
           ,Is_Unique
           ,Is_Unique_Constraint
           ,ParentObjectID
           ,ParentObject
           ,ReferenceObjectID
           ,ReferenceObject
           ,ReferenceColumnID
           ,ReferenceColumn)
     (SELECT Distinct SO.Object_ID
       ,ISTC.Constraint_Catalog
       ,ISTC.Constraint_Schema
       ,ISTC.Constraint_Name
       ,ISTC.Table_Catalog
       ,ISTC.Table_Schema
       ,ISTC.Table_name
       ,ISTC.Constraint_Type
       ,ISTC.Is_Deferrable
       ,ISTC.Initially_Deferred
       ,ISKC.Column_Name
       ,ISKC.Ordinal_Position
       ,SI.type_desc
       ,CASE WHEN SI.is_unique = 1 THEN 'UNIQUE' ELSE '' END
       ,CASE WHEN SI.is_unique_constraint= 1 THEN 'UNIQUE' ELSE '' END
       ,SO.Parent_object_ID
       ,PO.Name AS ParentObject
       ,SFK.referenced_object_id
       ,RO.NAME AS ReferenceObject
       ,FKC.referenced_column_id
       ,SC.name AS Ref_column
  FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS ISTC
  INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE ISKC ON ISKC.CONSTRAINT_NAME = ISTC.CONSTRAINT_NAME
  AND ISKC.CONSTRAINT_CATALOG = ISTC.CONSTRAINT_CATALOG
  AND ISKC.TABLE_NAME =ISTC.TABLE_NAME
  INNER JOIN Sys.Objects SO ON SO.Name = ISTC.CONSTRAINT_NAME
  LEFT JOIN Sys.indexes SI ON SI.name = ISTC.CONSTRAINT_NAME
  LEFT JOIN sys.foreign_keys SFK ON SFK.object_id = SO.object_id
  LEFT JOIN (SELECT name, object_id from sys.objects) PO ON PO.OBJECT_ID = SO.Parent_Object_ID
  LEFT JOIN (SELECT name, object_id from sys.objects) RO ON RO.OBJECT_ID = SFK.referenced_object_id
  LEFT JOIN sys.foreign_key_columns FKC ON FKC.referenced_object_id = SFK.referenced_object_id
  LEFT JOIN SYSCOLUMNS SC ON SC.id =  SFK.referenced_object_id AND SC.colid = FKC.referenced_column_id 
  WHERE ISTC.CONSTRAINT_CATALOG = @Database AND ISTC.CONSTRAINT_SCHEMA = @Schema)
    END
    
   
END
GO
/****** Object:  StoredProcedure [dbo].[spDropRefFKs]    Script Date: 07/05/2011 12:32:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ======================================================
-- Author:  Mahadevan
-- Create date: 26/6/2011
-- Description: To drop all the Reference FK's in a table
-- ======================================================
CREATE PROCEDURE [dbo].[spDropRefFKs]

 @Tablename VARCHAR(50)
AS
BEGIN

 SET NOCOUNT ON;

 BEGIN
 DECLARE @Table  VARCHAR(128)
 DECLARE @ReferenceObject VARCHAR(100)
 DECLARE @ConstraintName VARCHAR(128)
 DECLARE [GetRefs] CURSOR FOR
  SELECT  Table_name,
     ReferenceObject,
     Constraint_Name
  FROM TableConstraints
  WHERE ReferenceObject = @Tablename
  ORDER BY [Table_Name]
 OPEN [GetRefs]
 FETCH NEXT FROM [GetRefs] INTO  @Table, @ReferenceObject, @ConstraintName
 WHILE @@FETCH_STATUS = 0
 BEGIN
  DECLARE @SQL NVARCHAR(255)
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE name = (@ConstraintName)
  AND referenced_object_id = OBJECT_ID(@ReferenceObject))
  BEGIN
  SELECT @SQL =  'ALTER TABLE ' + @Table + ' DROP CONSTRAINT [' + @ConstraintName + '];'
  END
  --PRINT @SQL
  EXEC    sp_executesql @SQL
  FETCH NEXT FROM [GetRefs] INTO  @Table, @ReferenceObject, @ConstraintName
 END
 CLOSE  [GetRefs]
 DEALLOCATE [GetRefs]

END
  
END
GO

----------------------------------------------
Now design your package as shown below and use appropriate stored procedures in Execute SQL Task.