IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[pub_gen_webservice_proc]') AND type = 'P')
DROP PROCEDURE [dbo].[pub_gen_webservice_proc]
GO
 
CREATE proc [dbo].[pub_gen_webservice_proc]
@cmpid int,
@svcid bigint,
@extsql varchar(max)
as
begin
set nocount on

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'svc',@svcid,0,@tbname output,@fkey output ,@dbname output 
set @tbname  = @dbname + @tbname 


declare @mysql nvarchar(max) 
set @mysql = '' 

declare @flds nvarchar(4000) 
declare @dsname nvarchar(500) 
declare @s1 nvarchar(500) 
declare @s2 nvarchar(500) 
declare @cap nvarchar(100) 
declare @fldtype varchar(100)
declare @size int 
declare @decimal int 
declare @csql nvarchar(4000) = ''
declare @isql nvarchar(4000) = ''
declare @usql nvarchar(4000) = ''
declare @xsql nvarchar(4000) = '' 
declare @i int 
declare @noupdate int = 1 
set @extsql = ISNULL(@extsql,'') 
if (@extsql='_none_') 
begin
set @noupdate = 0 
select top 1 @extsql=svc_extsql, @flds=svc_displayflds,@dsname=svc_resultfld from ebs_kernel.dbo.pub_webservice where svc_id=@svcid 
end 
else 
select top 1 @flds=svc_displayflds,@dsname=svc_resultfld from ebs_kernel.dbo.pub_webservice where svc_id=@svcid 

set @extsql = ISNULL(@extsql,'') 

set @dsname = ISNULL(@dsname,'') 
set @flds = ISNULL(@flds,'') 

-- code:科目代码:varchar(10):fmt:align|title:科目名称:varchar(100):fmt:align
-- code:科目代码:varchar(10):align也可以，但不能code:科目代码:varchar(10):align:fmt 
if (@flds>'' and @dsname>'') 
begin
-- 如果表存在，则删除之
exec mps_sys_checktable @tbname,@i output  
if (@i=0)
begin
set @mysql =' drop table '+@tbname 
execute(@mysql) 
delete mps_fieldinfo where fldi_cmpid=@cmpid and fldi_tbtype='web' and fldi_tbid=@svcid 
end 
-- 分析建表语句及XMLSQL语句
while (@flds>'') 
begin
set @i = CHARINDEX('|',@flds) 
if (@i>0) 
begin
set @s1 = SUBSTRING(@flds,1,@i-1) 
set @flds = SUBSTRING(@flds,@i+1,4000) 
end else
begin
set @s1 = @flds 
set @flds = '' 
end 
-- @s1 code:科目代码:varchar(10):fmt:a....
set @i = CHARINDEX(':',@s1)
if (@i>0) 
begin
set @s2 = SUBSTRING(@s1,1,@i-1) -- 这里是字段名
set @s1 = SUBSTRING(@s1,@i+1,500)  -- 这里是字段中文以 科目代码:varchar(10):f 
set @i = CHARINDEX(':',@s1)
if (@i>0) -- 说明还有字段
begin
set @cap = SUBSTRING(@s1,1,@i-1) 
set @s1 = SUBSTRING(@s1 ,  @i + 1 , 500) -- varchar(10):f  
set @i = CHARINDEX(':',@s1)
if (@i>0) 
begin
set @s1 = SUBSTRING(@s1,1,@i-1) 
end  
set @s1 = LOWER(@s1)  -- @s1 为字段类型
if (@csql>'') set @csql = @csql + ',' 
set @csql = @csql + @s2 +' '+@s1+' NULL  ' 
if (CHARINDEX('int',@s1)>0 or CHARINDEX('bigint',@s1)>0 or CHARINDEX('dec',@s1)>0 or CHARINDEX('float',@s1)>0 or CHARINDEX('long',@s1)>0 or CHARINDEX('real',@s1)>0) 
begin
set @csql = @csql + ' default(0)  ' 
end else 
if (CHARINDEX('date',@s1)>0 or CHARINDEX('time',@s1)>0) 
begin
set @csql = @csql + ' default(''1900-01-01'')  ' 
end else
begin
set @csql = @csql + ' default('''')  ' 
end 
if (@xsql>'') set @xsql = @xsql + ',' 
set @xsql=@xsql + 'c.value('''+@s2+'[1]'','''+@s1+''') as '+@s2 

if (@isql>'') set @isql = @isql + ',' 
set @isql = @isql + @s2  
if (@usql>'') set @usql = @usql + ',' 
set @usql = @usql + 'o.'+@s2 +'=n.'+@s2 

set @fldtype = @s1 
if (CHARINDEX('date',@fldtype)>0)
begin
set @size = 8 
set @decimal = 0 
end else 
if (CHARINDEX('bigint',@fldtype)>0 or CHARINDEX('long',@fldtype)>0)
begin
set @size = 8 
set @decimal = 0 
end else
if (CHARINDEX('int',@fldtype)>0 or CHARINDEX('small',@fldtype)>0)
begin
set @size = 4 
set @decimal = 0 
end else
if (CHARINDEX('float',@fldtype)>0 or CHARINDEX('real',@fldtype)>0)
begin
set @size = 18
set @decimal = 10 
end else
if (CHARINDEX('decimal',@fldtype)>0)
begin
set @i = CHARINDEX('(',@fldtype) 
if (@i<=0) 
begin
set @size = 18
set @decimal = 4
end else 
begin
set @fldtype = substring(@fldtype,@i+1,100) 
set @i = CHARINDEX(')',@fldtype) 
set @fldtype = substring(@fldtype,1,@i-1) 
if (@fldtype='') set @fldtype='18,4' 
set @i = CHARINDEX(',',@fldtype) 
if (@i<=0) set @fldtype=@fldtype+',4' 
set @i = CHARINDEX(',',@fldtype) 
set @size = CONVERT(int,substring(@fldtype,1,@i-1)) 
set @decimal = CONVERT(int,substring(@fldtype,@i+1,10)) 
end 
set @fldtype='decimal' 
end else 
begin
set @i = CHARINDEX('(',@fldtype) 
if (@i<=0) 
begin
set @size=50 
set @decimal = 0 
end else 
begin
set @s1 = SUBSTRING(@fldtype,@i+1,100) 
set @fldtype = SUBSTRING(@fldtype,1,@i-1) 
set @i = CHARINDEX(')',@s1) 
if (@i>0) set @s1 = SUBSTRING(@s1,1,@i-1) 
set @i = CHARINDEX(',',@s1) 
if (@i>0) set @s1 = '50' 
if (@s1='') set @s1='50' 
set @size = CONVERT(int,@s1) 
set @decimal = 0
end 
end 
insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_tbname,fldi_fieldcaption,fldi_fieldname,fldi_system,fldi_datatype,fldi_ident,fldi_size,fldi_decimal,fldi_default,fldi_key,fldi_changed)
values(@cmpid,'web',@svcid,'zsvc_temp_'+convert(varchar(10),@svcid),@cap,@s2,1,@fldtype,1,@size,@decimal,'','svc_id',1)

end else 
begin
if (@csql>'') set @csql = @csql + ',' 
set @csql = @csql + @s2 +' nvarchar(500) NULL default('''') ' 
if (@xsql>'') set @xsql = @xsql + ',' 
set @xsql=@xsql + 'c.value('''+@s2+'[1]'',''nvarchar(500)'') as '+@s2 

if (@isql>'') set @isql = @isql + ',' 
set @isql = @isql + @s2  
if (@usql>'') set @usql = @usql + ',' 
set @usql = @usql + 'o.'+@s2 +'=n.'+@s2 
insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_tbname,fldi_fieldcaption,fldi_fieldname,fldi_system,fldi_datatype,fldi_ident,fldi_size,fldi_decimal,fldi_default,fldi_key,fldi_changed)
values(@cmpid,'web',@svcid,'zsvc_temp_'+convert(varchar(10),@svcid),@s2,@s2,1,'nvarchar',1,500,0,'','svc_id',1)
end 
end else 
begin
if (@csql>'') set @csql = @csql + ',' 
set @csql = @csql + @s1 +' nvarchar(500) NULL default('''') ' 
if (@xsql>'') set @xsql = @xsql + ',' 
set @xsql=@xsql + 'c.value('''+@s1+'[1]'',''nvarchar(500)'') as '+@s1 

if (@isql>'') set @isql = @isql + ',' 
set @isql = @isql + @s1  
if (@usql>'') set @usql = @usql + ',' 
set @usql = @usql + 'o.'+@s1 +'=n.'+@s1 

insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_tbname,fldi_fieldcaption,fldi_fieldname,fldi_system,fldi_datatype,fldi_ident,fldi_size,fldi_decimal,fldi_default,fldi_key,fldi_changed)
values(@cmpid,'web',@svcid,'zsvc_temp_'+convert(varchar(10),@svcid),@s1,@s1,1,'nvarchar',1,500,0,'','svc_id',1)
end 
end 
insert into mps_fieldinfo(fldi_cmpid,fldi_tbtype,fldi_tbid,fldi_tbname,fldi_fieldcaption,fldi_fieldname,fldi_system,fldi_datatype,fldi_ident,fldi_size,fldi_decimal,fldi_default,fldi_key,fldi_changed)
values(@cmpid,'web',@svcid,'zsvc_temp_'+convert(varchar(10),@svcid),'svc_id','唯一码',1,'int',1,4,0,'','svc_id',1)
-- 建表
set @mysql = 'create table '+@tbname+'('+@csql+', cmpid int default(0) ,useruid int, md5 varchar(32) default '''' , svc_id int IDENTITY(1,1) NOT NULL) ' 
execute(@mysql) 
-- 建索引
set @mysql = ' ALTER TABLE '+@tbname+' ADD  CONSTRAINT [PK_'+@tbname+'_index] PRIMARY KEY CLUSTERED 
(
[svc_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] '
execute(@mysql) 

-- 以下开始构造sql程序
set @mysql='
-- 删除以前的老数据
delete '+@tbname+' where cmpid=@cmpid and md5=@md5 and useruid=@useruid 
insert into '+@tbname+'(cmpid,md5,useruid,'+@isql+') 
select @cmpid,@md5,@useruid,'+@xsql+' from @xmldoc.nodes('''+@dsname+''') as T(c)

'
-- 如果有#master 
if (CHARINDEX('#master',@extsql)>0) 
begin
set @mysql=@mysql+N'
exec drop_temp_table ''#master''  
create table #master('+@csql+', new int default(0),svcid int default(0) , objid int) 
insert into #master('+@isql+',new,svcid,objid) 
select '+@isql+',1,svc_id,svc_id  from '+@tbname+' where cmpid=@cmpid and md5=@md5 and useruid=@useruid 
'
end 
-- 如果有insert into #master 
if (dbo.checksyntax(@extsql,'delete','#master','')=0) 
begin
set @mysql=@mysql+N'
declare @oldmaster table(oldsvcid int) 
insert into @oldmaster(oldsvcid) select svcid from #master 
'
end 
--- 
set @mysql = @mysql+N'
'+@extsql
if (dbo.checksyntax(@extsql,'insert','into','#master')=0) 
begin
set @mysql=@mysql+N'
insert into '+@tbname+'('+@isql+',cmpid,md5,useruid) 
select '+@isql+',@cmpid,@md5,@useruid from #master where new=0 

delete #master 
insert into #master('+@isql+',new,svcid,objid) 
select '+@isql+',1,svc_id,svc_id  from '+@tbname+' where cmpid=@cmpid and md5=@md5 and useruid=@useruid 

'
end 
if (dbo.checksyntax(@extsql,'delete','#master','')=0) 
begin
set @mysql=@mysql+N'
delete @oldmaster from #master where oldsvcid=svcid 
delete '+@tbname+' from @oldmaster where svc_id=oldsvcid 
'
end 
if (dbo.checksyntax(@extsql,'update','#master','')=0) 
begin
set @mysql=@mysql+N'
update o set '+@usql +' from '+@tbname+' o , #master n where o.svc_id=n.svcid 
'
end 
set @mysql=@mysql+N'
select '+@isql+',svc_id , svc_id objid from '+@tbname+' where cmpid=@cmpid and md5=@md5 and useruid=@useruid order by svc_id  
'
if (dbo.checksyntax(@extsql,'#master','','')=0) 
begin
set @mysql=@mysql+N'
exec drop_temp_table ''#master''  
'
end 
end else
begin
set @mysql = @extsql 
if (LEN(@mysql)<=5) set @mysql = '' 
end 

declare @procname varchar(200)
declare @psql nvarchar(max) 
declare @err int
set @err = 1

set @procname = 'webservice_query_' + CONVERT(varchar(200),@svcid)

if @svcid > 0
begin
declare @sqlname varchar(300)
declare @sqlstr varchar(4000)
declare @sqlparam varchar(4000)
declare @svcnote varchar(500)

select @svcnote = svc_note from  ebs_kernel..pub_webservice where svc_id = @svcid

if len(@mysql) >0
begin

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@procname+']') and OBJECTPROPERTY(id, N'IsProcedure') = 1) set @psql='alter ' else set @psql='create '
set @psql = @psql+' procedure '+@procname+char(13)+
'@cmpid int,'+ char(13)+
'@branchid int,'+ char(13)+
'@userdpid int,'+ char(13)+
'@useruid int ,'+ char(13)+
'@xml nvarchar(max),'+ char(13)+
'@md5 varchar(32)'+ char(13)+
'as '+ char(13)+
'begin'+char(13) +char(9)+
'set nocount on
 declare @xmldoc xml
 set @xml = REPLACE(@xml, ''encoding="UTF-8"'', '''')
 set @xml = REPLACE(@xml, ''encoding=''''UTF-8'', '''')
 set @xml = REPLACE(@xml, ''encoding=UTF-8'', '''')
 set @xml = REPLACE(@xml, ''xmlns="http'', ''xmln="http'')
 set @xml = REPLACE(@xml, ''xmlns=''''http'', ''xmln=''''http'')
 set @xml = REPLACE(@xml, ''xmlns=http'', ''xmln=http'')
 set @xmldoc = @xml
' + @mysql+N'
 set nocount off
 end' 
exec myprint @psql 
BEGIN TRY
execute(@psql)
END TRY
BEGIN CATCH
set @err = 0
select 'sql语法错误:LINE='+CONVERT(varchar(10),ERROR_LINE())
+',ERROR_MESSAGE='+ERROR_MESSAGE() as error
END CATCH
if @err > 0
begin
--更新extsql
if (@noupdate=1) 
update ebs_kernel..pub_webservice set svc_extsql = @extsql where svc_id = @svcid

set @sqlname = 'g_webservice_'+CONVERT(varchar(100),@svcid)
set @sqlstr = @sqlname + ' {cmpid},{branchid},{userdpid},{useruid},{xml},{md5}'
set @sqlparam = 'int|int|int|int|nvarchar(max)|varchar(32)'

if (not exists(select * from mps_sql where sql_cmpid=@cmpid and sql_name=@sqlname))
begin
insert into mps_sql(sql_cmpid,sql_name,sql_type,sql_text,sql_note,sql_sourcetables,sql_targettables,sql_params,sql_keyparam)
values(@cmpid,@sqlname,'MSSQL',@sqlstr,'','','',@sqlparam,'')
end else 
begin
update mps_sql set 
sql_text=@sqlstr,
sql_sourcetables='',
sql_targettables='',
sql_params=@sqlparam,
sql_keyparam='' 
where sql_cmpid=@cmpid and sql_name=@sqlname
end
end
end else
begin
set @psql = 'drop proc ' + @procname
set @sqlname = 'g_webservice_'+CONVERT(varchar(100),@svcid)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].['+@procname+']') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
begin
exec myprint @psql
exec(@psql)
end
if (@noupdate=1) 
update ebs_kernel..pub_webservice set svc_extsql = @extsql where svc_id = @svcid

delete from mps_sql where sql_name = @sqlname and sql_cmpid=@cmpid
end
end
else 
begin
if (dbo.checkprocedure(@procname)>0)
begin
set @psql = 'drop proc ' + @procname
exec myprint @psql
exec(@psql)
if (@noupdate=1) 
update ebs_kernel..pub_webservice set svc_extsql = @extsql where svc_id = @svcid
end
end

set nocount off
end