Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想使用存储过程sql asp.net c将ProductTable中的多个产品ID插入到另一个表中#_Sql_Asp.net - Fatal编程技术网

我想使用存储过程sql asp.net c将ProductTable中的多个产品ID插入到另一个表中#

我想使用存储过程sql asp.net c将ProductTable中的多个产品ID插入到另一个表中#,sql,asp.net,Sql,Asp.net,我有三个表:ProductList、PurchaseList和PurchaseDetails。单击一个按钮,我将尝试使用PurchaseList中的purchaseID将ProductList中新添加的产品插入详细信息列表,purchaseID设置为scope_identity。我被卡住了,因为我不知道如何在scope identity获取最后一个ID时插入产品的ProductID 我的存储过程如下: @GrandTotal money,@Status varchar (20),@Method

我有三个表:ProductList、PurchaseList和PurchaseDetails。单击一个按钮,我将尝试使用PurchaseList中的purchaseID将ProductList中新添加的产品插入详细信息列表,purchaseID设置为scope_identity。我被卡住了,因为我不知道如何在scope identity获取最后一个ID时插入产品的ProductID

我的存储过程如下:

@GrandTotal money,@Status varchar (20),@Method varchar (20),@Date date
as
declare @PurchaseID int
insert into PurchaseList values(@GrandTotal,@Status,@Method,@Date)
set @PurchaseID=SCOPE_IDENTITY()


insert into DetailsList(PurchaseID,ProductID)
values (@PurchaseID,)
我不知道在“值”部分放什么,这样我就可以将多个productID绑定到一个purchaseid,这样我就可以根据他们的purchaseid从Details列表中检索它们了。你是说这个吗

DECLARE @myIDTable table (theID int);

insert into PurchaseList 
   (your field names here for grandtotal, status ...) 
   Output inserted.PurchaseId into @myIdTable 
   values (@GrandTotal,@Status,@Method,@Date);

insert into DetailsList(PurchaseID,ProductID)
select theId, yourMissingProductId
from @myIDTable;

注意:output子句返回插入行中的PurchaseId值(假设您的ID字段名为PurchaseId)。因此,在一个表变量@myIDTable中,您将拥有所有插入行的ID。

您的数据结构在哪里?到目前为止,您的问题还很模糊。产品列表:ProductID int identity(1,1)、供应商名称nvarchar(20)、MedicineName nvarchar(20)、价格货币、数量货币、金额货币。。。。。。PurchaseList:PurchaseID int identity(1,1),Grandtotal money,状态nvarchar(50),方法nvarchar(50),purchasedate。。。。DetailsList:detailID int,productID int,purchaseID intI我希望这就是你所说的,我正试图填补你问题中的空白(例如,代码中哪里缺少productID)。你到底在做什么,不管怎样,检查我的回答,如果这是你的意思,唯一的问题是范围的身份。谢谢你。我试图将我在productlist中添加的产品的id插入detailslist,purchaseid是purchaseid在PurchaseTable中的作用域标识。因此,我可以在一次购买中使用多个产品id,这样我可以在执行Join操作时根据purchaseid检索产品的信息,这段代码中的productlist在哪里?您只在purchaselist中插入了一项。代码在上面,但缺少表,无法插入productlist。