2013-05-21 103 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