IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_sql_loadtable]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_sql_loadtable]
GO
create procedure [dbo].[sys_sql_loadtable](
  @cmpid int,
@parseid int,
@tbname varchar(20),
@prebank varchar(50),
@script varchar(max) output
)AS
begin
set @script = ''
declare @pretbname varchar(20),@keyfld varchar(20),@isLoaded int,@script2 varchar(max),@dsname varchar(20)
select @pretbname=pretbname,@dsname=dsname,@keyfld=keyfld
from flatDSS
where tbname=@tbname

set @isLoaded=0
if (@pretbname is not null)
begin
select @isLoaded = isLoaded
from flatDSS where tbname=@pretbname
if (@isLoaded = 0)
begin
declare @preB varchar(50)
select @preB = @prebank + '  '
exec sys_sql_loadtable @cmpid,@parseid,@pretbname,@preB,@script2 output
if (@script2 is not null)
select @script = @script + @script2
end
end

exec sys_sql_getfldval @cmpid,@parseid,@dsname,@tbname,@keyfld,@preBank,@script2 output
if (@script2 is not null)
select @script = @script + char(13) + @script2

end