Posts

Showing posts from 2013

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' ,                         c . name as 'Column Name' ,                         t . Name as 'Data type' ,                         c . [length] as 'Max Length' ,                         c . [prec] ,                         isnull ( c . scale , 0 ) ,                         columnproperty ( c . id , c . name , 'AllowsNull' ),       

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. ·          Click Recurring if you want a schedule to run repeatedly. To set the recurring schedule, complete the Frequency , Daily Frequency , and Duration groups on the dialog.

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

Generic Function for Padding a Character in SQL Server

Padding character to a string is something which we use quite often in our day to day development activities and is what i found as a very good generic way of doing this - CREATE FUNCTION dbo . fn_PadValue (   @InValue varchar ( 50 ),   @PadLimit int ,   @PadChar char ( 1 ),   @PadSide char ( 1 ) ) RETURNS varchar ( 50 ) AS BEGIN   Declare @Result varchar ( 50 )   Declare @Padding int   --Determine the amount of padding   Set @Padding = @PadLimit - Len ( @InValue )   IF @Padding < 0     Set @Padding = 0   IF @PadSide = 'L' -- Pad the left side     Set @Result = Replicate ( @PadChar , @Padding ) + @InValue   IF @PadSide = 'R' -- Pad the right side     Set @Result = @InValue + Replicate ( @PadChar , @Padding )   RETURN ( @Result ) END The Above code can be used in both LEFT and RIGHT side padding just by passing appropriate parameter to the function. Usage -- select dbo