How to use direct select and insert or load to speedup the process instead of cursur

  • Hi friends,

    I have stored procedure .In SP i am using cursur to load data from Parent to several child table.

    I have attached the script with this message.

    And my problem is how to use direct select and insert or load to speedup the process instead of cursur.

    Can any one please suggest me how to change this scripts pls.

    USE [IconicMarketing]

    GO

    /****** Object: StoredProcedure [dbo].[SP_DMS_INVENTORY] Script Date: 3/6/2015 3:34:03 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    -- =============================================

    -- Author:<ARUN,NAGARAJ>

    -- Create date: <11/21/2014>

    -- Description:<STORED PROCEDURE FOR DMS_INVENTORY>

    -- =============================================

    ALTER PROCEDURE [dbo].[SP_DMS_INVENTORY]

    @Specific_Date varchar(20) ,

    @DealerNum Varchar(6),

    @Date_Daily varchar(50)

    AS

    BEGIN

    SET NOCOUNT ON;

    --==========================================================================

    -- INVENTORY_CURSUR

    --==========================================================================

    DECLARE

    @FileType varchar(50),

    @ACDealerID varchar(50),

    @ClientDealerID varchar(50),

    @DMSType varchar(50),

    @StockNumber varchar(50),

    @InventoryDate datetime ,

    @StockType varchar(100),

    @DMSStatus varchar(50),

    @InvoicePrice numeric(18, 2),

    @CostPack varchar(50),

    @SalesCost numeric(18, 2),

    @HoldbackAmount numeric(18, 2),

    @ListPrice numeric(18, 2),

    @MSRP varchar(max),

    @LotLocation varchar(50),

    @TagLine varchar(max),

    @Certification varchar(max),

    @CertificationNumber varchar(max),

    @VehicleVIN varchar(50),

    @VehicleYear bigint ,

    @VehicleMake varchar(50),

    @VehicleModel varchar(50),

    @VehicleModelCode varchar(50),

    @VehicleTrim varchar(50),

    @VehicleSubTrimLevel varchar(max),

    @Classification varchar(max),

    @TypeCode varchar(100),

    @VehicleMileage bigint ,

    @EngineCylinderCount varchar(10) ,

    @TransmissionType varchar(50),

    @VehicleExteriorColor varchar(50),

    @VehicleInteriorColor varchar(50),

    @CreatedDate datetime ,

    @LastModifiedDate datetime ,

    @ModifiedFlag varchar(max),

    @InteriorColorCode varchar(50),

    @ExteriorColorCode varchar(50),

    @PackageCode varchar(50),

    @CodedCost varchar(50),

    @Air varchar(100),

    @OrderType varchar(max),

    @AgeDays bigint ,

    @OutstandingRO varchar(50),

    @DlrAccessoryRetail varchar(50),

    @DlrAccessoryCost varchar(max),

    @DlrAccessoryDesc varchar(max),

    @ModelDesc varchar(50),

    @Memo1 varchar(1000),

    @Memo2 varchar(max),

    @Weight varchar(max),

    @FloorPlan numeric(18, 2),

    @Purchaser varchar(max),

    @PurchasedFrom varchar(max),

    @InternetPrice varchar(50),

    @InventoryAcctDollar numeric(18, 2),

    @VehicleType varchar(50),

    @DealerAccessoryCode varchar(50),

    @AllInventoryAcctDollar numeric(18, 2),

    @BestPrice varchar(50),

    @instock bigint ,

    @AccountingMake varchar(50),

    @GasDiesel varchar(max),

    @BookValue varchar(10),

    @FactoryAccessoryDescription varchar(max),

    @TotalReturn varchar(10),

    @TotalCost varchar(10),

    @ss varchar(max),

    @VehicleBody varchar(max),

    @StandardEquipment varchar(max),

    @Account varchar(max),

    @CalculatedPrice varchar(10),

    @OriginalCost varchar(10),

    @AccessoryCore varchar(10),

    @OtherDollar varchar(10),

    @PrimaryBookValue varchar(10),

    @AmountDue varchar(10),

    @LicenseFee varchar(10),

    @ICompany varchar(max),

    @InvenAcct varchar(max),

    @Field23 varchar(max),

    @Field24 varchar(max),

    @SalesCode bigint,

    @BaseRetail varchar(10),

    @BaseInvAmt varchar(10),

    @CommPrice varchar(10),

    @Price1 varchar(10),

    @Price2 varchar(10),

    @StickerPrice varchar(10),

    @TotInvAmt varchar(10),

    @OptRetail varchar(max),

    @OptInvAmt varchar(10),

    @OptCost varchar(10),

    @Options1 varchar(max),

    @Category varchar(max),

    @Description varchar(max),

    @Engine varchar(max),

    @ModelType varchar(max),

    @FTCode varchar(max),

    @Wholesale varchar(max),

    @Retail varchar(max),

    @Draft varchar(max),

    @myerrorvarchar(500),

    @Inventoryid int,

    @errornumber int,

    @errorseverity varchar(500),

    @errortable varchar(50),

    @errorstate int,

    @errorprocedure varchar(500),

    @errorline varchar(50),

    @errormessage varchar(1000),

    @Invt_Id int,

    @flatfile_createddate datetime,

    @FtpDate date,

    @Inv_cur varchar(1000),

    @S_Year varchar(4),

    @S_Month varchar(2),

    @S_Date varchar(2),

    @Date_Specfic varchar(50),

    @Param_list nvarchar(max),

    @Daily_Date Varchar(50);

    --====================================================================================

    --DECLARE CURSUR FOR SPECIFIC DATE (OR) DEALER-ID WITH SPECIFIC DATE (OR) CURRENT DATE

    --====================================================================================

    set @Date_Specfic = Substring(@Specific_Date,1,4) +'-'+Substring(@Specific_Date,5,2)+'-'+Substring(@Specific_Date,7,2);

    set @Daily_Date = SUBSTRING(@Date_Daily,14,4) + '-' + SUBSTRING(@Date_Daily,18,2)+ '-' + SUBSTRING(@date_Daily,20,2)

    IF @Daily_Date IS NOT NULL

    BEGIN

    Delete From [dbo].[DMS_INVENTORY_DETAILS]

    Where DMSInventoryID in(select ID from [dbo].[DMS_INVENTORY] where CONVERT (date,FtpDate)=CONVERT (date,GETDATE()));

    Delete From [dbo].[DMS_INVENTORY_AMOUNT]

    Where DMSInventoryID in(select ID from [dbo].[DMS_INVENTORY] where CONVERT (date,FtpDate)=CONVERT (date,GETDATE()));

    Delete From [dbo].[ICONIC_INVENTORY_VEHICLE]

    Where DMSInventoryVehicleID in(select ID from [dbo].[DMS_INVENTORY] where CONVERT (date,FtpDate)=CONVERT (date,GETDATE()));

    Delete From [dbo].[DMS_INVENTORY_VEHICLE]

    Where DMSInventoryID in(select ID from [dbo].[DMS_INVENTORY] where CONVERT (date,FtpDate)=CONVERT (date,GETDATE()));

    Delete From [dbo].[ICONIC_EQUITY_INVENTORY_COMPARE]

    Where InventoryVehicleId in(select ID from [dbo].[DMS_INVENTORY] where CONVERT (date,FtpDate)=CONVERT (date,GETDATE()));

    Delete From [dbo].[DMS_INVENTORY]

    Where ID in(select ID from [dbo].[DMS_INVENTORY] where CONVERT (date,FtpDate)=CONVERT (date,GETDATE()));

    DECLARE Inventory_Cursor CURSOR FOR

    SELECT * from [dbo].[FLATFILE_INVENTORY] where

    CONVERT (date,flatfile_createddate) = CONVERT (date,GETDATE()) order by flatfile_createddate;

    END

    Else

    BEGIN

    if (@Date_Specfic IS NOT NULL AND @DealerNum != '?????')

    BEGIN

    Delete From [dbo].[DMS_INVENTORY_DETAILS]

    Where DMSInventoryID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic AND DMSDealerID='ACTEST' + @DealerNum);

    Delete From [dbo].[DMS_INVENTORY_AMOUNT]

    Where DMSInventoryID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic AND DMSDealerID='ACTEST' + @DealerNum);

    Delete From [dbo].[ICONIC_INVENTORY_VEHICLE]

    Where DMSInventoryVehicleID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic AND DMSDealerID='ACTEST' + @DealerNum);

    Delete From [dbo].[DMS_INVENTORY_VEHICLE]

    Where DMSInventoryID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic AND DMSDealerID='ACTEST' + @DealerNum);

    Delete From [dbo].[ICONIC_EQUITY_INVENTORY_COMPARE]

    Where InventoryVehicleId in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic AND DMSDealerID='ACTEST' + @DealerNum);

    Delete From [dbo].[DMS_INVENTORY]

    Where ID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic AND DMSDealerID='ACTEST' + @DealerNum);

    DECLARE Inventory_Cursor CURSOR FOR

    SELECT * from [dbo].[FLATFILE_INVENTORY] where FtpDate=@Date_Specfic AND ACDealerID='ACTEST' + @DealerNum;

    END

    ELSE

    BEGIN

    Delete From [dbo].[DMS_INVENTORY_DETAILS]

    Where DMSInventoryID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic);

    Delete From [dbo].[DMS_INVENTORY_AMOUNT]

    Where DMSInventoryID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic);

    Delete From [dbo].[ICONIC_INVENTORY_VEHICLE]

    Where DMSInventoryVehicleID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic);

    Delete From [dbo].[DMS_INVENTORY_VEHICLE]

    Where DMSInventoryID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic);

    Delete From [dbo].[ICONIC_EQUITY_INVENTORY_COMPARE]

    Where InventoryVehicleId in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic);

    Delete From [dbo].[DMS_INVENTORY]

    Where ID in(select ID from [dbo].[DMS_INVENTORY] where FtpDate=@Date_Specfic);

    DECLARE Inventory_Cursor CURSOR FOR

    SELECT * from [dbo].[FLATFILE_INVENTORY] where FtpDate=@Date_Specfic;

    END

    END

    OPEN Inventory_Cursor

    FETCH NEXT FROM Inventory_Cursor

    INTO

    @FileType ,

    @ACDealerID ,

    @ClientDealerID ,

    @DMSType ,

    @StockNumber ,

    @InventoryDate ,

    @StockType ,

    @DMSStatus ,

    @InvoicePrice ,

    @CostPack ,

    @SalesCost ,

    @HoldbackAmount ,

    @ListPrice ,

    @MSRP ,

    @LotLocation ,

    @TagLine ,

    @Certification ,

    @CertificationNumber ,

    @VehicleVIN ,

    @VehicleYear ,

    @VehicleMake ,

    @VehicleModel ,

    @VehicleModelCode ,

    @VehicleTrim ,

    @VehicleSubTrimLevel ,

    @Classification ,

    @TypeCode ,

    @VehicleMileage ,

    @EngineCylinderCount ,

    @TransmissionType ,

    @VehicleExteriorColor ,

    @VehicleInteriorColor ,

    @CreatedDate ,

    @LastModifiedDate ,

    @ModifiedFlag ,

    @InteriorColorCode ,

    @ExteriorColorCode ,

    @PackageCode ,

    @CodedCost ,

    @Air ,

    @OrderType ,

    @AgeDays ,

    @OutstandingRO ,

    @DlrAccessoryRetail ,

    @DlrAccessoryCost ,

    @DlrAccessoryDesc ,

    @ModelDesc ,

    @Memo1 ,

    @Memo2 ,

    @Weight ,

    @FloorPlan ,

    @Purchaser ,

    @PurchasedFrom ,

    @InternetPrice ,

    @InventoryAcctDollar ,

    @VehicleType ,

    @DealerAccessoryCode ,

    @AllInventoryAcctDollar ,

    @BestPrice ,

    @instock ,

    @AccountingMake ,

    @GasDiesel ,

    @BookValue ,

    @FactoryAccessoryDescription ,

    @TotalReturn ,

    @TotalCost ,

    @ss ,

    @VehicleBody ,

    @StandardEquipment ,

    @Account ,

    @CalculatedPrice ,

    @OriginalCost ,

    @AccessoryCore ,

    @OtherDollar ,

    @PrimaryBookValue ,

    @AmountDue ,

    @LicenseFee ,

    @ICompany ,

    @InvenAcct ,

    @Field23 ,

    @Field24 ,

    @SalesCode ,

    @BaseRetail ,

    @BaseInvAmt ,

    @CommPrice ,

    @Price1 ,

    @Price2 ,

    @StickerPrice ,

    @TotInvAmt ,

    @OptRetail ,

    @OptInvAmt ,

    @OptCost ,

    @Options1 ,

    @Category ,

    @Description ,

    @Engine ,

    @ModelType ,

    @FTCode ,

    @Wholesale ,

    @Retail ,

    @Draft ,

    @flatfile_createddate,

    @FtpDate;

    WHILE @@FETCH_STATUS = 0

    BEGIN

    --==========================================================================

    -- INSERT INTO INVENTORY (PARENT TABLE)

    --==========================================================================

    BEGIN TRY

    INSERT INTO [dbo].[DMS_INVENTORY]

    (

    DMSDealerID,

    StockNumber,

    DMSType,

    InventoryDate,

    FtpDate

    )

    VALUES (@ClientDealerID,@StockNumber,@DMSType,@InventoryDate,@FtpDate);

    END TRY

    BEGIN CATCH

    SELECT

    @errornumber = ERROR_NUMBER(),

    @errorseverity = ERROR_SEVERITY(),

    @errortable = 'DMS_INVENTORY',

    @errorstate = ERROR_STATE(),

    @errorprocedure = ERROR_PROCEDURE(),

    @errorline = ERROR_LINE(),

    @errormessage = ERROR_MESSAGE();

    --==========================================================================

    -- INSERT ERRORS INTO DMSLOG_INVENTORY_ERROR

    --==========================================================================

    EXEC [SP_DMS_INVENTORY_ERROR] @FileType,@ACDealerID,@ClientDealerID,@DMSType,@StockNumber,@InventoryDate,@StockType,@DMSStatus,@InvoicePrice,@CostPack,

    @SalesCost,@HoldbackAmount,@ListPrice,@MSRP,@LotLocation,@TagLine,@Certification,@CertificationNumber,@VehicleVIN,@VehicleYear,@VehicleMake,@VehicleModel,@VehicleModelCode,

    @VehicleTrim,@VehicleSubTrimLevel,@Classification,@TypeCode,@VehicleMileage,@EngineCylinderCount,@TransmissionType,@VehicleExteriorColor,@VehicleInteriorColor,

    @CreatedDate,@LastModifiedDate,@ModifiedFlag,@InteriorColorCode,@ExteriorColorCode,@PackageCode,@CodedCost,@Air,@OrderType,@AgeDays,@OutstandingRO,

    @DlrAccessoryRetail,@DlrAccessoryCost,@DlrAccessoryDesc,@ModelDesc,@Memo1,@Memo2,@Weight,@FloorPlan,@Purchaser,@PurchasedFrom,@InternetPrice,

    @InventoryAcctDollar,@VehicleType,@DealerAccessoryCode,@AllInventoryAcctDollar,@BestPrice,@InStock,@AccountingMake,@GasDiesel,@BookValue,

    @FactoryAccessoryDescription,@TotalReturn,@TotalCost,@SS,@VehicleBody,@StandardEquipment,@Account,@CalculatedPrice,@OriginalCost,@AccessoryCore,

    @OtherDollar,@PrimaryBookValue,@AmountDue,@LicenseFee,@ICompany,@InvenAcct,@Field23,@Field24,@SalesCode,@BaseRetail,@BaseInvAmt,@CommPrice,@Price1,

    @Price2,@StickerPrice,@TotInvAmt,@OptRetail,@OptInvAmt,@OptCost,@Options1,@Category,@Description,@Engine,@ModelType,@FTCode,@Wholesale,@Retail,@Draft,

    @ERRORNUMBER,@ERRORSEVERITY,@ERRORTABLE,@ERRORSTATE,@ERRORPROCEDURE,@ERRORLINE,@errormessage,@FtpDate

    END CATCH

    -- PRINT @errornumber;

    -- PRINT @errorseverity;

    -- PRINT @errortable;

    -- PRINT @errorprocedure;

    -- PRINT @errorline;

    -- PRINT @errormessage;

    -- PRINT @errorstate;

    set @myerror = @@ERROR;

    -- This -- PRINT statement -- PRINTs 'Error = 0' because

    -- @@ERROR is reset in the IF statement above.

    -- PRINT N'Error = ' + @myerror;

    set @Inventoryid = scope_identity();

    -- PRINT @Inventoryid;

    --==========================================================================

    -- INSERT INTO DMS_INVENTORY_DETAILS (CHILD TABLE)

    --==========================================================================

    BEGIN TRY

    INSERT INTO [dbo].[DMS_INVENTORY_DETAILS]

    (

    DMSInventoryID,

    StockType,

    DMSStatus,

    LotLocation,

    TagLine,

    Certification,

    CertificationNumber,

    CreatedDate,

    LastModifiedDate,

    ModifiedFlag,

    PackageCode,

    OrderType,

    AgeDays,

    OutstandingRO,

    Memo1,

    Memo2,

    Purchaser,

    PurchasedFrom,

    DealerAccessoryCode,

    InStock,

    AccountingMake,

    SS,

    Account,

    AccessoryCore,

    ICompany,

    InvenAcct,

    Field23,

    Field24,

    SalesCode,

    Draft,

    FTCode,

    FtpDate

    )

    VALUES (

    @InventoryID,

    @StockType,

    @DMSStatus,

    @LotLocation,

    @TagLine,

    @Certification,

    @CertificationNumber,

    @CreatedDate,

    @LastModifiedDate,

    @ModifiedFlag,

    @PackageCode,

    @OrderType,

    @AgeDays,

    @OutstandingRO,

    @Memo1,

    @Memo2,

    @Purchaser,

    @PurchasedFrom,

    @DealerAccessoryCode,

    @instock,

    @AccountingMake,

    @ss,

    @Account,

    @AccessoryCore,

    @ICompany,

    @InvenAcct,

    @Field23,

    @Field24,

    @SalesCode,

    @Draft,

    @FTCode,

    @FtpDate

    );

    END TRY

    BEGIN CATCH

    SELECT

    @errornumber = ERROR_NUMBER(),

    @errorseverity = ERROR_SEVERITY(),

    @errorstate = ERROR_STATE(),

    @errortable = 'DMS_INVENTORY_DETAILS',

    @errorprocedure = ERROR_PROCEDURE(),

    @errorline = ERROR_LINE(),

    @errormessage = ERROR_MESSAGE();

    --==========================================================================

    -- INSERT ERRORS INTO DMSLOG_INVENTORY_ERROR

    --==========================================================================

    EXECUTE [SP_DMS_INVENTORY_ERROR] @FileType,@ACDealerID,@ClientDealerID,@DMSType,@StockNumber,@InventoryDate,@StockType,@DMSStatus,@InvoicePrice,@CostPack,

    @SalesCost,@HoldbackAmount,@ListPrice,@MSRP,@LotLocation,@TagLine,@Certification,@CertificationNumber,@VehicleVIN,@VehicleYear,@VehicleMake,@VehicleModel,@VehicleModelCode,

    @VehicleTrim,@VehicleSubTrimLevel,@Classification,@TypeCode,@VehicleMileage,@EngineCylinderCount,@TransmissionType,@VehicleExteriorColor,@VehicleInteriorColor,

    @CreatedDate,@LastModifiedDate,@ModifiedFlag,@InteriorColorCode,@ExteriorColorCode,@PackageCode,@CodedCost,@Air,@OrderType,@AgeDays,@OutstandingRO,

    @DlrAccessoryRetail,@DlrAccessoryCost,@DlrAccessoryDesc,@ModelDesc,@Memo1,@Memo2,@Weight,@FloorPlan,@Purchaser,@PurchasedFrom,@InternetPrice,

    @InventoryAcctDollar,@VehicleType,@DealerAccessoryCode,@AllInventoryAcctDollar,@BestPrice,@InStock,@AccountingMake,@GasDiesel,@BookValue,

    @FactoryAccessoryDescription,@TotalReturn,@TotalCost,@SS,@VehicleBody,@StandardEquipment,@Account,@CalculatedPrice,@OriginalCost,@AccessoryCore,

    @OtherDollar,@PrimaryBookValue,@AmountDue,@LicenseFee,@ICompany,@InvenAcct,@Field23,@Field24,@SalesCode,@BaseRetail,@BaseInvAmt,@CommPrice,@Price1,

    @Price2,@StickerPrice,@TotInvAmt,@OptRetail,@OptInvAmt,@OptCost,@Options1,@Category,@Description,@Engine,@ModelType,@FTCode,@Wholesale,@Retail,@Draft,

    @ERRORNUMBER,@ERRORSEVERITY,@ERRORTABLE,@ERRORSTATE,@ERRORPROCEDURE,@ERRORLINE,@errormessage,@FtpDate

    END CATCH

    --==========================================================================

    -- INSERT INTO DMS_INVENTORY_AMOUNT (CHILD TABLE)

    --==========================================================================

    BEGIN TRY

    INSERT INTO [dbo].[DMS_INVENTORY_AMOUNT]

    (

    DMSInventoryID,

    AllInventoryAcctDollar,

    OtherDollar,

    PrimaryBookValue,

    AmountDue,

    LicenseFee,

    CalculatedPrice,

    OriginalCost,

    BookValue,

    TotalReturn,

    TotalCost,

    DlrAccessoryRetail,

    DlrAccessoryCost,

    DlrAccessoryDesc,

    InternetPrice,

    InventoryAcctDollar,

    BestPrice,

    Weight,

    FloorPlan,

    CodedCost,

    InvoicePrice,

    CostPack,

    SalesCost,

    HoldbackAmount,

    ListPrice,

    MSRP,

    BaseRetail,

    BaseInvAmt,

    CommPrice,

    Price1,

    Price2,

    StickerPrice,

    TotInvAmt,

    OptRetail,

    OptInvAmt,

    OptCost,

    Wholesale,

    Retail,

    FtpDate

    )

    VALUES (

    @InventoryID,

    @AllInventoryAcctDollar,

    @OtherDollar,

    @PrimaryBookValue,

    @AmountDue,

    @LicenseFee,

    @CalculatedPrice,

    @OriginalCost,

    @BookValue,

    @TotalReturn,

    @TotalCost,

    @DlrAccessoryRetail,

    @DlrAccessoryCost,

    @DlrAccessoryDesc,

    @InternetPrice,

    @InventoryAcctDollar,

    @BestPrice,

    @Weight,

    @FloorPlan,

    @CodedCost,

    @InvoicePrice,

    @CostPack,

    @SalesCost,

    @HoldbackAmount,

    @ListPrice,

    @MSRP,

    @BaseRetail,

    @BaseInvAmt,

    @CommPrice,

    @Price1,

    @Price2,

    @StickerPrice,

    @TotInvAmt,

    @OptRetail,

    @OptInvAmt,

    @OptCost,

    @Wholesale,

    @Retail,

    @FtpDate

    );

    END TRY

    BEGIN CATCH

    SELECT

    @errornumber = ERROR_NUMBER(),

    @errorseverity = ERROR_SEVERITY(),

    @errortable = 'DMS_INVENTORY_AMOUNT',

    @errorstate = ERROR_STATE(),

    @errorprocedure = ERROR_PROCEDURE(),

    @errorline = ERROR_LINE(),

    @errormessage = ERROR_MESSAGE();

    --==========================================================================

    -- INSERT ERRORS INTO DMSLOG_INVENTORY_ERROR

    --==========================================================================

    EXEC [SP_DMS_INVENTORY_ERROR] @FileType,@ACDealerID,@ClientDealerID,@DMSType,@StockNumber,@InventoryDate,@StockType,@DMSStatus,@InvoicePrice,@CostPack,

    @SalesCost,@HoldbackAmount,@ListPrice,@MSRP,@LotLocation,@TagLine,@Certification,@CertificationNumber,@VehicleVIN,@VehicleYear,@VehicleMake,@VehicleModel,@VehicleModelCode,

    @VehicleTrim,@VehicleSubTrimLevel,@Classification,@TypeCode,@VehicleMileage,@EngineCylinderCount,@TransmissionType,@VehicleExteriorColor,@VehicleInteriorColor,

    @CreatedDate,@LastModifiedDate,@ModifiedFlag,@InteriorColorCode,@ExteriorColorCode,@PackageCode,@CodedCost,@Air,@OrderType,@AgeDays,@OutstandingRO,

    @DlrAccessoryRetail,@DlrAccessoryCost,@DlrAccessoryDesc,@ModelDesc,@Memo1,@Memo2,@Weight,@FloorPlan,@Purchaser,@PurchasedFrom,@InternetPrice,

    @InventoryAcctDollar,@VehicleType,@DealerAccessoryCode,@AllInventoryAcctDollar,@BestPrice,@InStock,@AccountingMake,@GasDiesel,@BookValue,

    @FactoryAccessoryDescription,@TotalReturn,@TotalCost,@SS,@VehicleBody,@StandardEquipment,@Account,@CalculatedPrice,@OriginalCost,@AccessoryCore,

    @OtherDollar,@PrimaryBookValue,@AmountDue,@LicenseFee,@ICompany,@InvenAcct,@Field23,@Field24,@SalesCode,@BaseRetail,@BaseInvAmt,@CommPrice,@Price1,

    @Price2,@StickerPrice,@TotInvAmt,@OptRetail,@OptInvAmt,@OptCost,@Options1,@Category,@Description,@Engine,@ModelType,@FTCode,@Wholesale,@Retail,@Draft,

    @ERRORNUMBER,@ERRORSEVERITY,@ERRORTABLE,@ERRORSTATE,@ERRORPROCEDURE,@ERRORLINE,@errormessage,@FtpDate

    END CATCH

    --==========================================================================

    -- INSERT INTO DMS_INVENTORY_VEHICLE (CHILD TABLE)

    --==========================================================================

    BEGIN TRY

    INSERT INTO [dbo].[DMS_INVENTORY_VEHICLE]

    (

    DMSInventoryID,

    InteriorColorCode,

    ExteriorColorCode,

    Air,

    ModelDesc,

    VehicleType,

    VehicleVIN,

    VehicleYear,

    VehicleMake,

    VehicleModel,

    VehicleModelCode,

    VehicleTrim,

    VehicleSubTrimLevel,

    Classification,

    TypeCode,

    VehicleMileage,

    FtpDate,

    EngineCylinderCount

    )

    VALUES (

    @InventoryID,

    @InteriorColorCode,

    @ExteriorColorCode,

    @Air,

    @ModelDesc,

    @VehicleType,

    @VehicleVIN,

    @VehicleYear,

    @VehicleMake,

    @VehicleModel,

    @VehicleModelCode,

    @VehicleTrim,

    @VehicleSubTrimLevel,

    @Classification,

    @TypeCode,

    @VehicleMileage,

    @FtpDate,

    @EngineCylinderCount

    );

    END TRY

    BEGIN CATCH

    SELECT

    @errornumber = ERROR_NUMBER(),

    @errorseverity = ERROR_SEVERITY(),

    @errortable = 'DMS_INVENTORY_VEHICLE',

    @errorstate = ERROR_STATE(),

    @errorprocedure = ERROR_PROCEDURE(),

    @errorline = ERROR_LINE(),

    @errormessage = ERROR_MESSAGE();

    --==========================================================================

    -- INSERT ERRORS INTO DMSLOG_INVENTORY_ERROR

    --==========================================================================

    EXEC [SP_DMS_INVENTORY_ERROR] @FileType,@ACDealerID,@ClientDealerID,@DMSType,@StockNumber,@InventoryDate,@StockType,@DMSStatus,@InvoicePrice,@CostPack,

    @SalesCost,@HoldbackAmount,@ListPrice,@MSRP,@LotLocation,@TagLine,@Certification,@CertificationNumber,@VehicleVIN,@VehicleYear,@VehicleMake,@VehicleModel,@VehicleModelCode,

    @VehicleTrim,@VehicleSubTrimLevel,@Classification,@TypeCode,@VehicleMileage,@EngineCylinderCount,@TransmissionType,@VehicleExteriorColor,@VehicleInteriorColor,

    @CreatedDate,@LastModifiedDate,@ModifiedFlag,@InteriorColorCode,@ExteriorColorCode,@PackageCode,@CodedCost,@Air,@OrderType,@AgeDays,@OutstandingRO,

    @DlrAccessoryRetail,@DlrAccessoryCost,@DlrAccessoryDesc,@ModelDesc,@Memo1,@Memo2,@Weight,@FloorPlan,@Purchaser,@PurchasedFrom,@InternetPrice,

    @InventoryAcctDollar,@VehicleType,@DealerAccessoryCode,@AllInventoryAcctDollar,@BestPrice,@InStock,@AccountingMake,@GasDiesel,@BookValue,

    @FactoryAccessoryDescription,@TotalReturn,@TotalCost,@SS,@VehicleBody,@StandardEquipment,@Account,@CalculatedPrice,@OriginalCost,@AccessoryCore,

    @OtherDollar,@PrimaryBookValue,@AmountDue,@LicenseFee,@ICompany,@InvenAcct,@Field23,@Field24,@SalesCode,@BaseRetail,@BaseInvAmt,@CommPrice,@Price1,

    @Price2,@StickerPrice,@TotInvAmt,@OptRetail,@OptInvAmt,@OptCost,@Options1,@Category,@Description,@Engine,@ModelType,@FTCode,@Wholesale,@Retail,@Draft,

    @ERRORNUMBER,@ERRORSEVERITY,@ERRORTABLE,@ERRORSTATE,@ERRORPROCEDURE,@ERRORLINE,@errormessage,@FtpDate

    END CATCH

    --==========================================================================

    -- MOVE CURSUR TO NEXT RECORD

    --==========================================================================

    FETCH NEXT FROM Inventory_Cursor

    INTO @FileType ,

    @ACDealerID ,

    @ClientDealerID ,

    @DMSType ,

    @StockNumber ,

    @InventoryDate ,

    @StockType ,

    @DMSStatus ,

    @InvoicePrice ,

    @CostPack ,

    @SalesCost ,

    @HoldbackAmount ,

    @ListPrice ,

    @MSRP ,

    @LotLocation ,

    @TagLine ,

    @Certification ,

    @CertificationNumber ,

    @VehicleVIN ,

    @VehicleYear ,

    @VehicleMake ,

    @VehicleModel ,

    @VehicleModelCode ,

    @VehicleTrim ,

    @VehicleSubTrimLevel ,

    @Classification ,

    @TypeCode ,

    @VehicleMileage ,

    @EngineCylinderCount ,

    @TransmissionType ,

    @VehicleExteriorColor ,

    @VehicleInteriorColor ,

    @CreatedDate ,

    @LastModifiedDate ,

    @ModifiedFlag ,

    @InteriorColorCode ,

    @ExteriorColorCode ,

    @PackageCode ,

    @CodedCost ,

    @Air ,

    @OrderType ,

    @AgeDays ,

    @OutstandingRO ,

    @DlrAccessoryRetail ,

    @DlrAccessoryCost ,

    @DlrAccessoryDesc ,

    @ModelDesc ,

    @Memo1 ,

    @Memo2 ,

    @Weight ,

    @FloorPlan ,

    @Purchaser ,

    @PurchasedFrom ,

    @InternetPrice ,

    @InventoryAcctDollar ,

    @VehicleType ,

    @DealerAccessoryCode ,

    @AllInventoryAcctDollar ,

    @BestPrice ,

    @instock ,

    @AccountingMake ,

    @GasDiesel ,

    @BookValue ,

    @FactoryAccessoryDescription ,

    @TotalReturn ,

    @TotalCost ,

    @ss ,

    @VehicleBody ,

    @StandardEquipment ,

    @Account ,

    @CalculatedPrice ,

    @OriginalCost ,

    @AccessoryCore ,

    @OtherDollar ,

    @PrimaryBookValue ,

    @AmountDue ,

    @LicenseFee ,

    @ICompany ,

    @InvenAcct ,

    @Field23 ,

    @Field24 ,

    @SalesCode ,

    @BaseRetail ,

    @BaseInvAmt ,

    @CommPrice ,

    @Price1 ,

    @Price2 ,

    @StickerPrice ,

    @TotInvAmt ,

    @OptRetail ,

    @OptInvAmt ,

    @OptCost ,

    @Options1 ,

    @Category ,

    @Description ,

    @Engine ,

    @ModelType ,

    @FTCode ,

    @Wholesale ,

    @Retail ,

    @Draft ,

    @flatfile_createddate,

    @FtpDate;

    END

    CLOSE Inventory_Cursor;

    DEALLOCATE Inventory_Cursor;

    SET ANSI_PADDING OFF

    END

  • You really don't want us to go through hundreds of lines of code? Please explain the problem in plain English: it will be faster.

    Please also post table definitions as CREATE TABLE statements and some sample data as INSERT statements. Also post expected results. See the first article linked in my signature line for guidelines.

    -- Gianluca Sartori

  • Actually i have data in master table.

    I need to poplulate those data in to respective parent and child table.

    For Example,

    Master table name is Customer_car details

    Columns like

    1:FIletype

    2:AcdealerID

    3:ClientID

    4:Type

    5:Stocknumber

    6:Stock type

    7:Invoice price

    8:Salescost

    9:Customer address

    10:Vehicle number...like this i have 70 columns

    (where all the details of customer,Car,and other details will be there)

    Parents table is DM_Customer_car details

    Columns like

    1:ID

    2:DMid

    3:Stocknumber

    4:Stock type

    5 Child Tables are theres where severral columns are there.

    when i am loading data from master to parent and child table i am using cursurin stored procedure

    My question is instead of using cursur how can i use direct select and insert or load to speedup the process.

    And another question while loading the duplicate data it must delete the old data and need to update the new one.It must got delete in parent as well as all 5 child table.

    How to do this ?

  • Not enough information.

    You didn't read the article I suggested, did you?

    Let me say it again: read it (here's the link http://www.sqlservercentral.com/articles/Best+Practices/61537/) post the information in the format described in the article and we'll do our best to help you. First, help us help you.

    -- Gianluca Sartori

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply