/*

15.09.2025, TSC
*/

USE [HciLogin]

IF INDEXPROPERTY(OBJECT_ID('UserAddress'), 'NC_IX_UserAddress__User_FK' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_UserAddress__User_FK ON [HciLogin].[dbo].[UserAddress] ([User_FK]) INCLUDE ([UserAddressType_FK], [AdditionalLine1], [Street], [Zip], [City], [Country])
    PRINT 'created NC_IX_UserAddress__User_FK'
END 

IF INDEXPROPERTY(OBJECT_ID('UserCommunication'), 'NC_IX_UserCommunication__User_FK' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_UserCommunication__User_FK ON [HciLogin].[dbo].[UserCommunication] ([User_FK]) INCLUDE ([UserCommunicationType_FK], [Communication])
    PRINT 'created NC_IX_UserCommunication__User_FK'
END 


USE [SwissRxLogin2]

IF INDEXPROPERTY(OBJECT_ID('AuthorizationCode'), 'NC_IX_AuthorizationCode__AuthorizationCodeHash' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_AuthorizationCode__AuthorizationCodeHash ON [SwissRxLogin2].[dbo].[AuthorizationCode] ([AuthorizationCodeHash])
    PRINT 'created NC_IX_AuthorizationCode__AuthorizationCodeHash'
END 

USE [MyProducts]

IF INDEXPROPERTY(OBJECT_ID('ORD_OrderProduct'), 'NC_IX_ORD_OrderProduct__Order_FK' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_ORD_OrderProduct__Order_FK ON [MyProducts].[dbo].[ORD_OrderProduct] ([Order_FK]) INCLUDE ([CoProductCategoryCode_EXT], [ProductNr], [ForFree], [DiscountSameFI], [ProductFamily], [SalesQty], [UnitPrice], [Discount], [Lifecycle], [LifecycleDate], [Db_Status], [Db_InsDt], [Db_InsUser], [App_InsUser], [Db_UpdDt], [Db_UpdUser], [App_UpdUser], [Db_Timestamp])
    PRINT 'created NC_IX_ORD_OrderProduct__Order_FK'
END 

IF INDEXPROPERTY(OBJECT_ID('ORD_OrderProduct'), 'NC_IX_ORD_OrderProduct__Db_Status' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_ORD_OrderProduct__Db_Status ON [MyProducts].[dbo].[ORD_OrderProduct] ([Db_Status]) INCLUDE ([Order_FK], [CoProductCategoryCode_EXT], [ProductNr], [ProductFamily], [SalesQty], [UnitPrice], [Discount], [Lifecycle])
    PRINT 'created NC_IX_ORD_OrderProduct__Db_Status'
END 

IF INDEXPROPERTY(OBJECT_ID('PMS_ProductMessage'), 'NC_IX_PMS_ProductMessage__CoProductMessageType_FK_PreviewID_Db_Status' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_PMS_ProductMessage__CoProductMessageType_FK_PreviewID_Db_Status ON [MyProducts].[dbo].[PMS_ProductMessage] ([CoProductMessageType_FK], [PreViewId], [Db_Status])
    PRINT 'created NC_IX_PMS_ProductMessage__CoProductMessageType_FK_PreviewID_Db_Status'
END 

IF INDEXPROPERTY(OBJECT_ID('ORD_Order'), 'NC_IX_ORD_Order__OrderContractTimePeriod_FK' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_ORD_Order__OrderContractTimePeriod_FK ON [MyProducts].[dbo].[ORD_Order] ([OrderContractTimePeriod_FK]) INCLUDE ([CoOrderStatus_FK], [CoOrderType_FK], [CompanyPtNr], [Title], [TotalCost], [CheckedOutBy], [PurchaseOrderNr], [InvoiceReference], [MaintenanceDateUpload], [Db_Status], [Db_InsDt], [App_InsUser], [Db_UpdDt], [App_UpdUser])
    PRINT 'created NC_IX_ORD_Order__OrderContractTimePeriod_FK'
END 



USE [CompendiumPortal]
IF INDEXPROPERTY(OBJECT_ID('MON_MonSubVer_MonSection'), 'NC_IX_MON_MonSubVer_MonSection__MonSubVer_FK' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_MON_MonSubVer_MonSection__MonSubVer_FK ON [CompendiumPortal].[dbo].[MON_MonSubVer_MonSection] ([MonSubVer_FK]) INCLUDE ([MonSection_FK], [Db_Status], [Db_InsDt], [Db_UpdDt], [Db_Timestamp])
    PRINT 'created NC_IX_MON_MonSubVer_MonSection__MonSubVer_FK'
END 

IF INDEXPROPERTY(OBJECT_ID('MON_MonSection_Picture'), 'NC_IX_MON_MonSection_Picture__Picture_FK' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_MON_MonSection_Picture__Picture_FK ON [CompendiumPortal].[dbo].[MON_MonSection_Picture] ([Picture_FK]) INCLUDE ([MonSection_FK], [Sort], [Db_Status], [Db_InsDt], [Db_UpdDt], [Db_Timestamp])
    PRINT 'created NC_IX_MON_MonSection_Picture__Picture_FK'
END 


USE [SwissIndex_Products_Work]
IF INDEXPROPERTY(OBJECT_ID('ARTICLES'), 'NC_IX_ARTICLES__ArtDel' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_ARTICLES__ArtDel ON [SwissIndex_Products_Work].[dbo].[ARTICLES] ([ArtDel]) INCLUDE ([ArtNo], [ArtIndexCode], [ArtDT], [ArtXML])
    PRINT 'created NC_IX_ARTICLES__ArtDel'
END 

IF INDEXPROPERTY(OBJECT_ID('ARTICLEWHOLESALERS'), 'NC_IX_ARTICLEWHOLESALERS__AwhIndexCode' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_ARTICLEWHOLESALERS__AwhIndexCode ON [SwissIndex_Products_Work].[dbo].[ARTICLEWHOLESALERS] ([AwhIndexCode]) INCLUDE ([AwhNo], [AwhDT], [AwhXML])
    PRINT 'created NC_IX_ARTICLEWHOLESALERS__AwhIndexCode'
END 



USE [Index_Viewer]

IF INDEXPROPERTY(OBJECT_ID('PRODUCT___PRD'), 'NC_IX_PRODUCT___PRD__BatchType_Trade_Del' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_PRODUCT___PRD__BatchType_Trade_Del ON [Index_Viewer].[dbo].[PRODUCT___PRD] ([BatchType], [TRADE], [DEL]) INCLUDE ([PRDNO], [DSCRD], [QAP])
    PRINT 'created NC_IX_PRODUCT___PRD__BatchType_Trade_Del'
END 

IF INDEXPROPERTY(OBJECT_ID('ARTICLE___ARTINS'), 'NC_IX_ARTICLE___ARTINS__Nincd' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_ARTICLE___ARTINS__Nincd ON [Index_Viewer].[dbo].[ARTICLE___ARTINS] ([NINCD]) INCLUDE ([XmlIndex], [BatchType], [ART_Id])
    PRINT 'created NC_IX_ARTICLE___ARTINS__Nincd'

    --DROP INDEX [NC_IX_ARTICLE__Nincd] ON [ARTICLE___ARTINS]
END

-- IX_ARTICLE_ART__BType_DEL_wIncludes
IF INDEXPROPERTY(OBJECT_ID('dbo.ARTICLE___ART'), 'IX_ARTICLE_ART__BType_DEL_wIncludes' , 'IndexID' ) IS NOT NULL BEGIN;
    DROP INDEX dbo.ARTICLE___ART.IX_ARTICLE_ART__BType_DEL_wIncludes;
END;
IF INDEXPROPERTY(OBJECT_ID('dbo.ARTICLE___ART'), 'IX_ARTICLE_ART__BType_DEL_wIncludes' , 'IndexID' ) IS NULL BEGIN;
    CREATE INDEX IX_ARTICLE_ART__BType_DEL_wIncludes ON dbo.ARTICLE___ART(BatchType, DEL) INCLUDE(ART_Id, DT, PHAR, XmlIndex,[PHARMACODE], [QTY], [DSCRD], [NOPCS]);
END;

USE [JOB]

IF INDEXPROPERTY(OBJECT_ID('JOB_STATE'), 'NC_IX_JOB_STATE__State' , 'IndexID' ) IS NULL 
BEGIN
    CREATE INDEX NC_IX_JOB_STATE__State ON [JOB].[dbo].[JOB_STATE] ([STATE]) INCLUDE ([JOBNAME])
    PRINT 'created NC_IX_JOB_STATE__State'
END


