0

I use below script to insert orders transaction manually. This script processes one order at time (@orderId - using this variable here). I got a list of 200 orders, is there a way i can process all orders using single script?

DECLARE       @return_value int, @exceptionId bigint, @createDate datetime

EXEC   @return_value = [dbo].[uspInsertException]
          @exceptionTypeCode = N'CreateCustomerAccount',
          @exceptionSource = N'SOPS',
          @exceptionCode = N'PUSH2EQ',
          @exceptionDescription = N'CreateCustomerAccount exception MANUALLY pushed to EQ',
          @request = N'',
          @response = N'',
          @orderId = 227614128,
          @sourceSystem = N'OMS',
          @exceptionStatusCode = N'Open',
          @actorId = 1,
          @exceptionSubTypeCode = NULL,
          @exceptionId = @exceptionId OUTPUT,
          @createDate = @createDate OUTPUT

SELECT @exceptionId as N'@exceptionId', @createDate as N'@createDate'

SELECT 'Return Value' = @return_value
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Ankit
  • 1
  • 1
  • 1
    It could be me, but it seems this question is about the script not about the SQL code. Maybe showing us the code for the script could help.... since that is what the question is about? – Hogan Feb 10 '17 at 23:32
  • to be clear, above sql code inserts exceptions/transaction for the given @orderid, is there a way to use the same script to insert transaction for more than 50 orders at a time? – Ankit Feb 10 '17 at 23:47
  • No, but you can run it in a loop for all your order ids. – Zohar Peled Feb 11 '17 at 07:53

1 Answers1

0

Absolutely it can be done. The best way I have found is building nested classes in your application, and then pass it to sql where you can shred it with OPENXML or xPath depending on the size of the xml.

Depending on your needs you can also use a webservice, where you can place the classes and the code to connect to the database. The application then references the classes in the webservice, and passes the data in the class hierarchy format to the web service, which then parses the data and passes it as a full block of xml to the database, where in a stored procedure it is shredded and inserted. If you use this method, make sure you make your c# classes serializable.

You can easily retrieve data from the database as part of the stored proc by using for xml, and I would recommend wrapping it in a transaction so that you don't have half of a file inserted when an error occurs.

If you need some code samples, provide a better description of how you are passing your data to the database.

CREATE PROCEDURE [dbo].[sp_InsertExceptions]
    -- Add the parameters for the stored procedure here
    @pXML XML
AS
BEGIN
SET XACT_ABORT ON;

BEGIN TRY
    BEGIN TRANSACTION;
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
           DECLARE @XML AS XML, @hDoc AS INT
print convert(varchar(max), @pRI)
EXEC sp_xml_preparedocument @hDoc OUTPUT, @pRI

{Put your shredding code here}

EXEC sp_xml_removedocument @hDoc

     COMMIT TRANSACTION;

     EXECUTE sp_GetErrors --This stored procedure is used to retrieve data
                              --previously inserted
END TRY
BEGIN CATCH
    -- Execute error retrieval routine.
    EXECUTE usp_GetErrorInfo; --This stored procedure gets your error 
                              --information and can also store it in the
                              --database to track errors

    -- Test XACT_STATE:
        -- If 1, the transaction is committable.
        -- If -1, the transaction is uncommittable and should 
        --     be rolled back.
        -- XACT_STATE = 0 means that there is no transaction and
        --     a commit or rollback operation would generate an error.

    -- Test whether the transaction is uncommittable.
    IF (XACT_STATE()) = -1
    BEGIN
        PRINT
            N'The transaction is in an uncommittable state.' +
            'Rolling back transaction.'
        ROLLBACK TRANSACTION;
    END;

    -- Test whether the transaction is committable.
    IF (XACT_STATE()) = 1
    BEGIN
        PRINT
            N'The transaction is committable.' +
            'Committing transaction.'
        COMMIT TRANSACTION;   
    END;
END CATCH;
Greg
  • 85
  • 1
  • 8