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

-- =============================================
-- Author:郑华建
-- Create date: 2017年12月28日 11点12分
-- Description:根据wxid 获取获取对应的自动创建的state信息
-- =============================================
create PROCEDURE [dbo].[mps_g_weixinstate_bywxid]
@cmpid int,
@wx_id int
AS
BEGIN
SET NOCOUNT ON;

declare @wxs_id int
declare @FLAG varchar(10)
set @FLAG='##ebs##'
--判断是否存在
select @wxs_id = wxs_id from mps_weixinstate where wxs_wxid = @wx_id and wxs_auto = 1 and wxs_value=@FLAG
if @wxs_id = 0 or @wxs_id is null
begin
exec mps_new_weixin_state @cmpid,'',@wx_id,0,1
select @wxs_id = wxs_id from mps_weixinstate where wxs_wxid = @wx_id and wxs_auto = 1
update mps_weixinstate set 
wxs_value=@FLAG ,
wxs_gzh=@wx_id,
Wxs_qyh=@wx_id,
Wxs_ding=@wx_id
where wxs_id = @wxs_id
end else
select * from mps_weixinstate where wxs_id = @wxs_id

END