IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_depart_manager]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_depart_manager]
GO


-- 查询员工是否合法
create proc [dbo].[mps_g_depart_manager]
@cmpid int,
@name nvarchar(100)
as 
begin
set nocount on

declare @num bigint;
set @num = -1;


select @num = count(cu_name) from mps_employee 
where cu_cmpid = @cmpid and cu_name = @name 
if @num>1
begin
select 0 as cu_id
end else
if @num = 1
begin
select cu_id,cu_name from mps_employee 
where cu_cmpid = @cmpid and cu_name = @name
end else
if @num = 0
begin
-- 查询员工编码
select @num = count(cu_code) from mps_employee 
where cu_cmpid = @cmpid and cu_code = @name
if @num >1
begin
select 0 as cu_id
end else
if @num = 1
begin
select cu_id,cu_name from mps_employee 
where cu_cmpid = @cmpid and cu_code = @name
end
else if @num=0
begin
-- 查询员工手机号
select @num = count(cu_code) from mps_employee 
where cu_cmpid = @cmpid and cu_mobile = @name
if @num >1
begin
select 0 as cu_id
end else
if @num = 1
begin
select cu_id,cu_name from mps_employee 
where cu_cmpid = @cmpid and cu_mobile = @name
end
else
begin
select 0 as cu_id
end
end
end
else
begin
select 0 as cu_id
end

set nocount off
end