IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_gen_mpssql]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sys_gen_mpssql]
GO
CREATE PROCEDURE [dbo].[mps_sys_gen_mpssql](
@cmpid int,
@procname varchar(100),
@params varchar(max)
)
AS
BEGIN
SET NOCOUNT ON

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @sql_text varchar(max)
declare @sql_params varchar(max)
declare @s varchar(max)
declare @s1 varchar(100)

declare @index int
set @s=@params
set @s=REPLACE(@s,' =default','');
set @s=REPLACE(@s,'=default','');
set @s=REPLACE(@s,char(10),' ');
set @s=REPLACE(@s,char(13),' ');
set @s=REPLACE(@s,char(9),' ');
set @s=REPLACE(@s,char(8),' ');
while CHARINDEX('  ',@s) > 0
set @s=REPLACE(@s,'  ',' ');
set @s=REPLACE(@s,' (','(');
set @s=REPLACE(@s,') ',')');
set @s=REPLACE(@s,'( ','(');
set @s=REPLACE(@s,' )',')');
set @s=REPLACE(@s,' @','@');
set @s=REPLACE(@s,' ,',',');
set @s=REPLACE(@s,', ',',');
set @s=REPLACE(@s,char(10),'');
set @s=REPLACE(@s,char(13),'');
set @s=REPLACE(@s,char(9),'');
set @s=REPLACE(@s,char(8),'');
set @s=REPLACE(@s,'@','');
set @s=REPLACE(@s,' out','');
set @s=REPLACE(@s,' output','')+',,';

set @sql_text=''
set @sql_params=''

while @s > ''
begin
--处理空格 参数名称
set @index=CHARINDEX(' ',@s)
if @index >0
set @sql_text+='{'+ SUBSTRING(@s,1,@index-1) +'},'
else
break
set @s = SUBSTRING(@s,@index+1,LEN(@s))
--处理逗号 (参数类型)
set @index=CHARINDEX(',',@s)
set @s1=SUBSTRING(@s,1,@index-1)
if CHARINDEX('numeric',@s1) >0 or CHARINDEX('NUMERIC',@s1) >0 or CHARINDEX('decimal',@s1) >0 or CHARINDEX('DECIMAL',@s1) >0
begin
set @index=CHARINDEX(',',@s,@index+1);
end
set @sql_params += SUBSTRING(@s,1,@index-1)+'|';
set @s = SUBSTRING(@s,@index+1,LEN(@s))
end
--去掉多余的分隔符
if LEN(@sql_text) > 0
set @sql_text = SUBSTRING(@sql_text,1,LEN(@sql_text)-1);
if LEN(@sql_params) > 0
set @sql_params = SUBSTRING(@sql_params,1,LEN(@sql_params)-1);

--先直接更新,就算先判断也会执行一个查询,这样子可能会少执行一个查询
set @sql_text=@procname+' '+@sql_text
update mps_sql 
set sql_text=@sql_text ,
sql_params=@sql_params
where sql_cmpid=@cmpid
and sql_name=@procname
and sql_type='MSSQL'
--如果发现没有更新的行,就说明表中没有对应的数据,就插入一条语句
if @@ROWCOUNT = 0
insert into mps_sql(sql_cmpid,sql_name,sql_type,sql_text,sql_params) 
values(@cmpid,@procname,'MSSQL',@sql_text,@sql_params);
END