【发布时间】:2014-05-28 04:11:56
【问题描述】:
我正在尝试在存储过程中使用 With
USE [BusOprtn]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddRepairedStock]
@RepairedItems [dbo].[RepairedItems] readonly
AS
BEGIN
declare @productNo bigint,@productManufacturer bigint
SET NOCOUNT ON;
begin try
begin transaction
UPDATE [BusOprtn].[dbo].[RepairItem] SET [ReturnedQuantity] = rdi.ReturnedQuantity,[AmountPaid] = rdi.AmountPaid,[ReturnDate] = rdi.ReturnDate from [BusOprtn].[dbo].[RepairItem] as ri inner join @RepairedItems as rdi on ri.id=rdi.id;
;with y as (
select [PartUsedId],rdi.[ReturnedQuantity] from [BusOprtn].[dbo].[RepairItem] as ri inner join @RepairedItems as rdi on ri.Id=rdi.id
), x as (
SELECT [PartNo] ,[ManufacturerId],[ReturnedQuantity] FROM [BusOprtn].[dbo].[PartUsed] as p inner join y
on p.id = y.PartUsedId
)
UPDATE [BusOprtn].[dbo].[ProductMaster] SET [RepairedStock] =( [RepairedStock]+x.[ReturnedQuantity]) from [BusOprtn].[dbo].[ProductMaster] as pm inner join x on x.[PartNo]=pm.Id;
UPDATE [BusOprtn].[dbo].[ProductStockManufacturer] SET [RepairedCurrentStock] = ([RepairedCurrentStock]+x.[ReturnedQuantity]) from [BusOprtn].[dbo].[ProductStockManufacturer] as pm inner join x on x.[PartNo]=pm.[ProductNo] and x.[ManufacturerId]= pm.[ManufacturerId];
commit transaction
end try
BEGIN CATCH
declare @ErrorMessage nvarchar(max), @ErrorSeverity
int, @ErrorState int;
select @ErrorMessage = ERROR_MESSAGE() + ' Line ' + cast
(ERROR_LINE() as nvarchar(5)), @ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
rollback transaction;
raiserror (@ErrorMessage, @ErrorSeverity, @ErrorState);
END CATCH
END
当我执行上面的命令时,它执行成功。但是当我尝试在运行时调用存储过程时,它给出了错误
`Invalid object name 'x'.
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.`
早期的问题是子查询,因为它不允许使用“WITH”,因此尝试使用另一个临时表y,但错误仍在继续。我为 WITH 中允许的项目推荐了 Allowed Items。
【问题讨论】:
标签: c# sql stored-procedures try-catch