Posts

Use of Script Task to create SQL table using VB.Net in SSIS

The Below Code will be useful in the cases when we need to deal with flat files, wherein the file structure may be dynamic and we are dealing with multiple files each one with a different schema definition. The approach will be here is to loop through each file and then read the first line of each file and read the column names from the file and then dynamically create the table and then load the data. The below code is used to read the column information from the file and then generate a SQL Command which will create the table in the database. Here it goes -  Code to dynamically create a table using the Input File Data – Public Sub Main()         Dim objReader As New StreamReader(Dts.Variables( "SRCDATAFILE" ).Value.ToString)         Dim sLine As String = ""         Dim arrText As New ArrayList()       ...

Scripting Indexes in SQL 2000

DECLARE IndexCursor CURSOR FOR SELECT OBJECT_NAME ( SI . ID ), SI . ID , SI . Name , SI . indid FROM dbo . sysindexes SI LEFT JOIN INFORMATION_SCHEMA . TABLE_CONSTRAINTS TC ON SI . Name = TC . CONSTRAINT_NAME AND OBJECT_NAME ( SI . ID ) = TC . TABLE_NAME WHERE TC . CONSTRAINT_NAME IS NULL AND OBJECTPROPERTY ( SI . ID , 'IsUserTable' ) = 1 AND INDEXPROPERTY ( SI . [id] , SI . [name] , 'IsStatistics' ) = 0 AND OBJECT_NAME ( SI . ID ) in ( 'Table1' , 'Table2' ) ORDER BY OBJECT_NAME ( SI . ID ), SI . indid DECLARE @IxTable varchar ( 50 ) DECLARE @IxTableID INT DECLARE @IxName varchar ( 50 ) DECLARE @IxID INT -- Loop through all indexes OPEN IndexCursor FETCH NEXT FROM IndexCursor INTO @IxTable , @IxTableID , @IxName , @IxID WHILE ( @@FETCH_STATUS = 0 ) BEGIN    DECLARE @IXSQL NVARCHAR ( 4000 )    SET @IXSQL = 'CREATE '    -- Check if the ...

Generating a Table Creation Script using SQL Query

DECLARE @TABLE_NAME VARCHAR ( 100 ) DECLARE @CUR_TABLE_NAME CURSOR DELETE FROM ISGSQLWT01 . MONet . dbo . Table_Schema_Info_Prod SET @CUR_TABLE_NAME = CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA . TABLES where table_name in ( 'TableName1' , 'TableName2' ) OPEN @CUR_TABLE_NAME FETCH NEXT FROM @CUR_TABLE_NAME INTO @TABLE_NAME       WHILE @@FETCH_STATUS = 0             BEGIN                   INSERT INTO ISGSQLWT01 . MONet . dbo . Table_Schema_Info_Prod                   SELECT                         @TABLE_NAME as 'Table Name' ,       ...

Basics of SQL Server Job Scheduling

To create and attach a schedule to a job 1.     In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance. 2.     Expand SQL Server Agent , expand Jobs , right-click the job you want to schedule, and click Properties . 3.     Select the Schedules page, and then click New . 4.     In the Name box, type a name for the new schedule. 5.     Clear the Enabled check box if you do not want the schedule to take effect immediately following its creation. 6.     For Schedule Type , select one of the following: ·          Click Start automatically when SQL Server Agent starts to start the job when the SQL Server Agent service is started. ·          Click Start whenever the CPUs become idle to start the job when the CPUs reach an idle condition. ·  ...

Deleting Duplicate Data From SQL Server Table

Duplicate data is a very well known and frequently occurring issue in any DBMS and dealing with deletion of duplicate data is something not that easy for one specially one who is dealing with crucial and huge data. There are 2 scenarios which can come while deleting duplicate data - 1. when we have an identity column in our table containing duplicates. The problem becomes much easier here and we can just keep the data with MIN or MAX keys removing all other. --Create New Table create table Person ( ID int identity ( 1 , 1 ), Name varchar ( 30 ), age int ) --Insert Data into Table with multiple Duplicate entries insert into Person ( Name , age ) values ( 'Ashish' , 24 ) insert into Person ( Name , age ) values ( 'Ashish' , 24 ) insert into Person ( Name , age ) values ( 'Gaurav' , 26 ) insert into Person ( Name , age ) values ( 'Nayan' , 25 ) insert into Person ( Name , age ) values ( 'Nayan...