IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_up_attach_viewdn]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_up_attach_viewdn]
GO
create  procedure [dbo].[mps_up_attach_viewdn](
@cmpid int ,
@useruid int ,
@operation int ,
@daid bigint 
) as
begin
declare @duid bigint 
select @duid = du_id from mps_attach_log , mps_login
where du_daid=@daid and du_cuid=cu_id and 
du_bcid=cu_bcid and du_dpid=cu_dpid  and du_operation=@operation and cu_id=@useruid 

set @duid = ISNULL(@duid,0) 
if (@duid=0) 
begin
insert into mps_attach_log(du_daid,du_bcid,du_branch,du_dpid,du_depart,du_cuid,du_cuname,du_count,du_operation) 
select @daid , cu_bcid , cu_branch , cu_dpid,cu_depart,cu_id,cu_callname,1,@operation 
from mps_login where cu_id=@useruid 
end else 
begin
update mps_attach_log set du_count=du_count + 1 , du_lastdate = GETDATE() 
where du_id=@duid 
end 
if (@operation=0) 
update mps_attach set da_download = da_download + 1 where da_id=@daid 
else 
update mps_attach set da_view = da_view + 1 where da_id=@daid 
end