Sql server 使用单个脚本插入多个异常
我使用下面的脚本手动插入订单事务。此脚本一次处理一个订单(@orderId-此处使用此变量)。我有一个200个订单的列表,有没有一种方法可以使用单个脚本处理所有订单Sql server 使用单个脚本插入多个异常,sql-server,stored-procedures,jet-sql,Sql Server,Stored Procedures,Jet Sql,我使用下面的脚本手动插入订单事务。此脚本一次处理一个订单(@orderId-此处使用此变量)。我有一个200个订单的列表,有没有一种方法可以使用单个脚本处理所有订单 DECLARE @return_value int, @exceptionId bigint, @createDate datetime EXEC @return_value = [dbo].[uspInsertException] @exceptionTypeCode = N'CreateCu
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
绝对可以做到。我发现的最好的方法是在应用程序中构建嵌套类,然后将其传递给sql,在sql中可以根据xml的大小使用OPENXML或xPath将其分解 根据您的需要,您还可以使用webservice,您可以在其中放置类和代码以连接到数据库。然后,应用程序引用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;
可能是我,但这个问题似乎与脚本有关,而不是与SQL代码有关。也许给我们看脚本的代码会有帮助。。。。因为这就是问题所在?要清楚,上面的sql代码为给定的@orderid插入异常/事务,有没有方法使用同一脚本一次插入50多个订单的事务?没有,但可以在循环中为所有订单ID运行它。