標籤:erro 自增欄位 stat table procedure tput sem creat ret
USE [QuanLi]
GO
/****** Object: StoredProcedure [dbo].[BuyCar] Script Date: 11/12/2016 08:25:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[BuyCar] --建立預存程序ByCar
(
@Purchase_State varchar(200), --單據狀態
@Purchase_Remark varchar(200), --備忘
@Supplier_Id int, --外鍵供應商編碼
@WareHose_Id int, --外鍵倉庫編碼
@Employee_Id int, --經辦人
@xml xml --xml資料流
)
AS
Begin tran t12 --事務名稱
insert into Purchase(Purchase_curom,Purchase_date,Purchase_State,Purchase_Remark,Supplier_Id,WareHose_Id,Employee_Id)
values(CONVERT(INT,GETDATE(),112),GETDATE(),@Purchase_State,@Purchase_Remark,@Supplier_Id,@WareHose_Id,@Employee_Id)
declare @id int,@pointer int --定義參數
set @[email protected]@IDENTITY;---擷取自增欄位
create table #oo ---建立暫存資料表
(
Purchase_Id int, --採購主表編號(外鍵)
Product_Id int, --商品編號(外鍵)
PurchaseMate_Num int, --採購數量
PurchaseMate_Price decimal, --商品單價價格
PurchaseMate_Sum decimal --商品總價
)
execute sp_xml_preparedocument @pointer output,@xml--sp_xml_preparedocument語句 --將xml類型的資料迴圈添加到暫存資料表
insert into #oo(Product_Id,PurchaseMate_Num,PurchaseMate_Price,PurchaseMate_Sum)
select Product_Id,PurchaseMate_Num,PurchaseMate_Price,PurchaseMate_Sum from openxml (@pointer,‘/xml/product‘)--系統程式sp_xml_preparedocument指向‘/xml/product’
with(
Product_Id int, --商品編號
PurchaseMate_Num int, --採購數量
PurchaseMate_Price decimal, --商品單價價格
PurchaseMate_Sum decimal --商品總價
)
update #oo set [email protected] --修改值
insert into PurchaseMate(Purchase_Id,Product_Id,PurchaseMate_Num,PurchaseMate_Price,PurchaseMate_Sum) select Purchase_Id,Product_Id,PurchaseMate_Num,PurchaseMate_Price,PurchaseMate_Sum from #oo
if(@@ERROR<>0)
begin
rollback tran t12 --復原
return 0
end
else
begin
commit tran t12 --提交
return 1
end
購物車事務