Monday 12 March 2012

Find StoredProceduer ,Table or Column of the table Exists or Not & Find Root Parent ID

Find StoredProceduer Exists or Not

IF EXISTS ( SELECT  *
                     FROM    sysobjects
                     WHERE   id = OBJECT_ID(N'[spGetLeafProducts]')
                     AND OBJECTPROPERTY(id, N'IsProcedure') = 1 )

DROP PROC [spGetLeafProducts]
    GO

Find Table is having specifed column or not & Add the column with default value of "0"

IF NOT EXISTS ( SELECT  *
                              FROM    INFORMATION_SCHEMA.COLUMNS
                             WHERE   TABLE_NAME = 'Administrator'
                              AND COLUMN_NAME = 'Reviews' )

    ALTER TABLE Administrator
    ADD Reviews BIT NOT NULL 
    CONSTRAINT Constraint_name DEFAULT 0 WITH VALUES

Find Specified Table is Exists or not

IF NOT EXISTS ( SELECT  *
                FROM    INFORMATION_SCHEMA.TABLES
                WHERE   TABLE_NAME = 'MagicZoomPlusSettings' )

    CREATE TABLE [dbo].[MagicZoomPlusSettings]


Find Root Parent ID

DECLARE @RootCId BIGINT
             
            IF @CID IS NOT NULL
                WHILE ( @CID IS NOT NULL )
                    BEGIN
                        SET @RootCId = @CID
                        SELECT  @CID = FKCategoryID
                        FROM    dbo.Category
                        WHERE   PKCategoryID = @CID
                    END




No comments:

Post a Comment