IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_document]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_document]
GO
create proc [dbo].[mps_new_document]
@cmpid int,
@useruid int,
@doctag int,
@dcid int
as
begin
    set nocount on
    declare @docid  bigint 
--查询当前员工是否存在未存档的
select top 1 @docid = doc_id from mps_document 
where doc_tag = @doctag  and doc_dcid= @dcid and doc_cmpid = @cmpid and doc_cuid = @useruid and doc_archive = 1 and doc_title='' 
if @docid >0
begin
    update mps_document set doc_create = getdate() where doc_id = @docid
    select * from mps_document where doc_id = @docid
end 
else
begin
    insert into mps_document(doc_cmpid,doc_bcid,doc_branch,doc_dpid,doc_depart,doc_cuid,doc_creator,doc_tag,doc_dcid)
select @cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_id,cu_callname,@doctag,@dcid 
from mps_login where cu_id=@useruid 
select @docid = @@identity
select * from mps_document where doc_id = @docid
end
set nocount off
end