0

我使用下面的脚本手动插入订单交易。此脚本一次处理一个订单(@orderId - 在此处使用此变量)。我有 200 个订单的列表,有没有办法可以使用单个脚本处理所有订单?

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
4

1 回答 1

0

绝对可以做到的。我发现最好的方法是在您的应用程序中构建嵌套类,然后将其传递给 sql,您可以根据 xml 的大小使用 OPENXML 或 xPath 将其切碎。

根据您的需要,您还可以使用 Web 服务,您可以在其中放置类和代码以连接到数据库。然后,应用程序引用 Web 服务中的类,并将类层次结构格式的数据传递给 Web 服务,Web 服务然后解析数据并将其作为完整的 xml 块传递给数据库,在存储过程中将其分解并插入。如果您使用此方法,请确保您的 c# 类可序列化。

您可以通过使用 for xml 轻松地从数据库中检索数据作为存储过程的一部分,我建议将其包装在事务中,以便在发生错误时不会插入一半文件。

如果您需要一些代码示例,请更好地描述您如何将数据传递到数据库。

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;
于 2017-02-11T23:41:30.920 回答