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

create procedure [dbo].[sys_sql_genvar_global](
@parseid int,
@varparams varchar(2000) output,
@varlocal varchar(2000) output,
@gl_sql_text varchar(2000) output,
@gl_sql_params varchar(2000) output,
@gl_varList varchar(2000) output
)as
begin
-- datatype:3 整数   2：浮点数  1：字符串
Declare @globalname varchar(50),@gid int,@gname varchar(50),@gval varchar(500),@datatype int,@cnt int
select @varparams='',@varlocal='',@gl_sql_text='',@gl_sql_params='',@gl_sql_params='',@cnt=0

Declare gc Cursor static For
select c.g_id, c.g_name,c.g_value
from sys_sql_global a,sys_sql_inf b,mps_global c
where a.parseid=1 and a.parseid=b.parseid and b.cmpid=c.g_cmpid
and (a.globalname=c.g_name or a.globalname=c.g_caption)
open gc
fetch next from gc into @gid,@gname,@gval
while (@@fetch_status=0)
begin
-- 掉用函数，判断gval类型
set @datatype=dbo.getDataType(@gval)

if (@cnt>0)
select @varparams=@varparams+char(13)+'    ,'
,@varlocal=@varlocal+','
,@gl_sql_text=@gl_sql_text + ','
,@gl_sql_params=@gl_sql_params + '|'
,@gl_varList=@gl_varList + ','


if (@datatype=3)
select @varparams=@varparams+'@gl_' + convert(varchar(10),@gid)+ '__ int'
,@varlocal=@varlocal+'@gl_' + convert(varchar(10),@gid)+ '__ int'
  ,@gl_sql_text=@gl_sql_text + ' {global.' + @gname +'}'
,@gl_sql_params=@gl_sql_params+' int'
  ,@gl_varList=@gl_varList+'@gl_' + convert(varchar(10),@gid) + '__'
  
else if (@datatype=2)
select @varparams=@varparams+'@gl_' + convert(varchar(10),@gid)+ '__ decimal(18,4)'
,@varlocal=@varlocal+'@gl_' + convert(varchar(10),@gid)+ '__ decimal(18,4)'
  ,@gl_sql_text=@gl_sql_text + '{global.' + @gname +'}'
,@gl_sql_params=@gl_sql_params+' decimal(18,4)'
  ,@gl_varList=@gl_varList+'@gl_' + convert(varchar(10),@gid)+'__'
else
select @varparams=@varparams+'@gl_' + convert(varchar(10),@gid)+ '__ varchar(500)'
,@varlocal=@varlocal+'@gl_' + convert(varchar(10),@gid)+ '__ varchar(500)'
  ,@gl_sql_text=@gl_sql_text + '{global.' + @gname +'}'
,@gl_sql_params=@gl_sql_params+' varchar(500)'
  ,@gl_varList=@gl_varList+'@gl_' + convert(varchar(10),@gid)+'__'

select @cnt=@cnt+1
fetch next from gc into @gid,@gname,@gval
end

close gc
deallocate gc

end