2013-05-21 51 views
0

我使用3个表tblproduct,tblstocktblwinkelSQL存储过程删除重复记录,保留一个

tblstock表中有外键productidwinkelid

tblstock表中还有一个字段stock,它是一个整数。

我只想要有两个外键winkelidproductid的相同组合的1条记录。此记录的股票价值包含具有相同外键组合winkelidproductid的所有其他记录的总和。

所以,我想删除所有其他记录与它相同的2个外键,所以我只是保持1

我的存储过程中一直给以下错误:

Msg 155, Level 15, State 2, Procedure uspRecordsSamenvoegen, Line 11
'int' is not a recognized CURSOR option.

请帮忙吗?

这是迄今为止我的存储过程:

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

CREATE PROCEDURE uspRecordsSamenvoegen 

@winkelid int, @productid int 
AS 
BEGIN 
declare stocktotaal int 

    SET NOCOUNT ON 

select sum(Stock) into stocktotaal from TblStock where WinkelId = @winkelid and ProductId = @productid; 
delete from TblStock where WinkelId = @winkelid and ProductId = @productid; 
insert into TblStock values(@winkelid, @productid, stocktotaal); 

END 
GO 
+0

是winkelid和productid传递给uspRecordsSamenvoegen? – Beth

回答

0

因此您的结果会被捕获:

select WinkelId, ProductId, sum(Stock) as stocktotaal 
from TblStock 
group by WinkelId, ProductId 
2
declare stocktotaal int 

需求是

declare @stocktotaal int 

没有' @'来声明一个变量,即sql pa rser正在设置一个游标。另外,你不能选择一个变量。您的选择应该如下所示:

select @stocktotaal = sum(stock) from ... 
+0

感谢您的快速反应,完美的作品! – user2406976