IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_event]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_event]
GO
CREATE procedure [dbo].[mps_save_event]
(
@evtid int ,
@evt_params nvarchar(2000) ,
@evt_sql nvarchar(max) ,
@evt_note nvarchar(2000)
) as
begin

set nocount on

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @cmpid int 
declare @caption nvarchar(100) 
update mps_bizevent set 
evt_params = case when @evt_params='_none_' then evt_params else @evt_params end  ,
evt_note = case when @evt_note='_none_' then evt_note else @evt_note end  ,
evt_sql = case when @evt_sql='_none_' then evt_sql else @evt_sql end  
where evt_id=@evtid 

declare @code varchar(50) 
select top 1 @cmpid=evt_cmpid,@code=evt_code, @caption=evt_caption, @evt_params=evt_params , @evt_sql=evt_sql from mps_bizevent with(nolock) where evt_id=@evtid 
set @code = isnull(@code,'') 
if (@code='') set @code='user'+CONVERT(varchar(10),@evtid) 
else set @code = @code+'_'+convert(varchar(10),@cmpid) 

declare @sql nvarchar(4000)
-- 删除现有的
set @sql = 'drop procedure zevent_'+@code 
 

set @sql = @evt_params 
set @sql = LTRIM(rtrim(@sql)) 
if (@sql='') set @sql='@cmpid int' 
declare @i int 
set @i = charindex(',',@sql) 
if (@i=1) set @sql = SUBSTRING(@sql,2,2000) 
set @sql = LTRIM(rtrim(@sql)) 

set @i = charindex(',',@sql) 
if (@i=1) set @sql = SUBSTRING(@sql,2,2000) 
set @sql = LTRIM(rtrim(@sql)) 

set @i = charindex(',',@sql) 
if (@i=1) set @sql = SUBSTRING(@sql,2,2000) 
set @sql = LTRIM(rtrim(@sql)) 

set @i = charindex(',',@sql) 
if (@i=LEN(@sql)) set @sql = SUBSTRING(@sql,1,len(@sql)-1) 
set @sql = LTRIM(rtrim(@sql)) 

set @i = charindex(',',@sql) 
if (@i=LEN(@sql)) set @sql = SUBSTRING(@sql,1,len(@sql)-1) 
set @sql = LTRIM(rtrim(@sql)) 

set @i = charindex(',',@sql) 
if (@i=LEN(@sql)) set @sql = SUBSTRING(@sql,1,len(@sql)-1) 
set @sql = LTRIM(rtrim(@sql)) 

declare @psql nvarchar(max) 

declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
  select iv_caption,iv_code,iv_datatype , iv_unit , iv_value  from mps_bizevent_indexvalue with(nolock) 
  where iv_cmpid=@cmpid order by iv_type,iv_code 
  open status1
  declare @iv_caption nvarchar(100) 
  declare @iv_code varchar(100) 
  declare @iv_datatype nvarchar(100) 
  declare @iv_unit nvarchar(100) 
  declare @iv_value nvarchar(1000) 
  set @psql = '' 
  FETCH NEXT FROM status1 INTO @iv_caption,@iv_code,@iv_datatype , @iv_unit , @iv_value
  WHILE (@@FETCH_STATUS = 0)
  BEGIN
   if (@psql>'') set @psql=@psql+N'
   '
   set @psql = @psql +'declare @'+@iv_code+''
   if (@iv_datatype='整数') set @psql = @psql +'int='+case when @iv_value='' then '0' else @iv_value end else 
   if (@iv_datatype='小数') set @psql = @psql +'decimal(18,6)='+case when @iv_value='' then '0' else @iv_value end else 
   set @psql = @psql +'nvarchar(1000)='''+ @iv_value +'''' 
   set @psql = @psql +'--' +@iv_unit+''+@iv_caption 
   FETCH NEXT FROM status1 INTO @iv_caption,@iv_code,@iv_datatype , @iv_unit , @iv_value
  END
  CLOSE status1
DEALLOCATE status1 

declare @p1 nvarchar(100) 

if (dbo.checkprocedure('zevent_'+@code)=0) set @p1 = 'alter ' else set @p1 = 'create ' 

set @sql = @p1+'procedure zevent_'+@code+'('+@sql+') as 
begin
-- '+@caption+'
set nocount on

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
'
+@psql+N'
'+CHAR(13)+char(10)
+@evt_sql+
'
set nocount off
end
'

exec myprint @sql 

execute(@sql) 

set nocount off 
end