IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_new_barn]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_new_barn]
GO
 
CREATE procedure [dbo].[mps_new_barn](
@cmpid int)
as
begin
set nocount on
insert into mps_warehouse(wh_cmpid) values(@cmpid) 
declare @whid  int 
select top 1 @whid = wh_id from mps_warehouse where wh_cmpid=@cmpid order by wh_id desc 

select  a.* , ISNULL(bc_company,'') bc_company , ISNULL(dp_depart,'') dp_depart  
from mps_warehouse a LEFT OUTER JOIN mps_depart On wh_dpid = dp_id and dp_cmpid=@cmpid 
LEFT OUTER JOIN mps_groupbranch ON wh_bcid = bc_id and bc_cmpid=@cmpid   
where a.wh_id=@whid  

set nocount off 
end