SQL中的表中的重复记录

时间:2014-02-03 11:51:20

标签: c# asp.net sql sql-server-2008 stored-procedures

我有一个表格,主管,经理和导演aspx表格。当任何用户上传文件时,该文件首先进入supervsior然后是经理,然后是导演批准/拒绝

用户“kat”uplaod文件和他/她的文件批准/拒绝supervsior,经理,导演,它似乎在表中

Seqno DocID ApproveID ApproveBy DesigID ApproveDate

1        16        1              abc            1           10/11/2013

2        16        1               def           2           11/12/2013

3       16          1            xyzz         3             14/12/2013

当用户“kat”看到文件天文件批准/拒绝它似乎是这样的

docname fileuploaded uploadedate deptype status

finad   fina.docx   04/11/2013  finance approve

当另一个用户上传文件时,以及此文件批准给supervsior,经理,主管

然后在supervsior形式,它似乎是这个

docid    docname     filename department      email             uploadedby uploadedate

  16     finad      fina.docx    fiance   sadas@gmail.com        kat        04/11/2013 (this is old document)

   17    hrrr         hrr.docs  finance    abc@gmail.com         john   15/11/2013  (this is new document)

所以当supervsior批准文件(docid 17)那么已经批准/拒绝的旧文件也保存在数据库中,之后在表中就好像这样

Seqno DocID     ApproveID         ApproveBy   DesigID    ApproveDate

1        16        1              abc            1           10/11/2013

2        16        1               def           2           11/12/2013

3       16          1              xyzz          3             14/12/2013
4       16          1              ssd           1             15/11 2013
5       17          3              dfsdf         1             15/11/2013

这里1是批准,3正在等待

“我使用下拉菜单并在下拉菜单中填写下拉列表中的所有值”批准,拒绝,待处理“

drodown 所以,当“kat”agian看到他/她的文件时,它就像我这样看到了

docname fileuploaded uploadedate deptype status

finad   fina.docx   04/11/2013  finance reject

我像这样使用sp

ALTER procedure [dbo].[approveddd]
@DocID int,
@ApproveID int,     
@ApproveBy nvarchar(50),
@DesigID int
as

IF EXISTS(Select DocID from Approval where DocID=@DocID and AppoveBy=@ApproveBy)
Update Approval set ApproveID=@ApproveID where DocID=@DocID and AppoveBy=@ApproveBy
ELSE
insert Approval (DocID,ApproveID,AppoveBy,DesigID,ApproveDate)
values(@DocID,@ApproveID,@ApproveBy,@DesigID,GETDATE())

1 个答案:

答案 0 :(得分:0)

我对你的数据集了解不多,但是如何使用 唯一序列号(seqno)而不是 DocIDApproveBy?这让我省了几次。它可能会更好,因为 seqno是独一无二的。我想你必须考虑如何处理 如果一个DocIDApproveBy生成> 1 seqno。

所以而不是

IF EXISTS(Select DocID from Approval where DocID=@DocID and AppoveBy=@ApproveBy)
    Update Approval set ApproveID=@ApproveID where DocID=@DocID and AppoveBy=@ApproveBy
ELSE
    insert Approval (DocID,ApproveID,AppoveBy,DesigID,ApproveDate)
    values(@DocID,@ApproveID,@ApproveBy,@DesigID,GETDATE())
;

-- Get the sequence number  
DECLARE @seqno int;
SET @Seqno = (SELECT seqno 
              FROM Approval a
              where a.[ DocID ]=@DocID and 
              a.[ApproveBy ]==@ApproveBy);


-- Update record if exist, otherwise insert
IF (@seqno is not null or @seqno = '') 
BEGIN 
    Update Approval set ApproveID=@ApproveID where seqno=@seqno
end 
else 
    begin 
    insert Approval (DocID,ApproveID,AppoveBy,DesigID,ApproveDate)
    values(@DocID,@ApproveID,@ApproveBy,@DesigID,GETDATE())
end ;