-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
 
IF EXISTS (SELECT * 
             FROM INFORMATION_SCHEMA.COLUMNS a 
			WHERE TABLE_NAME ='EST_PROD_TBL_DESC_RC_CLI' 
			  and not exists (SELECT * 
							    FROM INFORMATION_SCHEMA.COLUMNS b 
							   where a.TABLE_CATALOG = b.TABLE_CATALOG
							     and a.TABLE_NAME    = b.table_name 
								 and b.column_name = 'CD_PROD')
			  )
BEGIN
	ALTER TABLE EST_PROD_TBL_DESC_RC_CLI ADD CD_PROD           INT    DEFAULT ((0)) NOT NULL
	ALTER TABLE EST_PROD_TBL_DESC_RC_CLI ADD PERC_DESC         MONEY  DEFAULT ((0)) NOT NULL
	ALTER TABLE EST_PROD_TBL_DESC_RC_CLI ADD PERC_DESC_MAX     MONEY  DEFAULT ((0)) NOT NULL
	ALTER TABLE EST_PROD_TBL_DESC_RC_CLI ADD PERC_DESC_LIMITE  MONEY  DEFAULT ((0)) NOT NULL
	ALTER TABLE EST_PROD_TBL_DESC_RC_CLI ADD PERC_AUMENTO      MONEY  DEFAULT ((0)) NOT NULL
END;

GO

 
IF EXISTS
   ( SELECT OBJECT_NAME(OBJECT_ID) TABLE_NAME, 
            *  
      FROM SYS.CHANGE_TRACKING_TABLES
     WHERE OBJECT_NAME(OBJECT_ID) = 'EST_PROD_TBL_DESC_RC_CLI'
)
BEGIN
        ALTER TABLE EST_PROD_TBL_DESC_RC_CLI DISABLE CHANGE_TRACKING;
        ALTER TABLE EST_PROD_TBL_DESC_RC_CLI DROP CONSTRAINT PK_EST_PROD_TBL_DESC_RC_CLI;
        ALTER TABLE EST_PROD_TBL_DESC_RC_CLI ADD  CONSTRAINT PK_EST_PROD_TBL_DESC_RC_CLI PRIMARY KEY(CD_EMP, CD_TBL_DESC, CD_CLI, CD_PROD);
        ALTER TABLE EST_PROD_TBL_DESC_RC_CLI ENABLE CHANGE_TRACKING;
END;
    
	 ELSE

BEGIN
        ALTER TABLE EST_PROD_TBL_DESC_RC_CLI DROP CONSTRAINT PK_EST_PROD_TBL_DESC_RC_CLI;
        ALTER TABLE EST_PROD_TBL_DESC_RC_CLI ADD  CONSTRAINT PK_EST_PROD_TBL_DESC_RC_CLI PRIMARY KEY(CD_EMP, CD_TBL_DESC, CD_CLI, CD_PROD);
        ALTER TABLE EST_PROD_TBL_DESC_RC_CLI ENABLE CHANGE_TRACKING;
END;

 
-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 