create procedure Proc1 as begin select 1 p1, 'b' p2 end
insert ... exec ...
construction insert ... exec ...
if object_id(N'tempdb..#t1',N'U') is not null drop table #t1 create table #t1(p1 int, p2 varchar(max)) insert #t1 exec Proc1 select * from #t1
select
, external table creation and insert
). Plus, the enumeration of fields occurs with each new similar call. (I add this criterion, because with a large number of revisions and a set of places to call a procedure, the process of changing the output data becomes very time consuming)exec Proc1
procedure in the simple output mode, no additional actions are required, it is enough to run exec Proc1
without insert
create procedure Proc1 as begin insert #t1(p1, p2) select 1 p1, 'b' p2 end
if object_id(N'tempdb..#t1',N'U') is not null drop table #t1 create table #t1(p1 int, p2 varchar(max)) exec Proc1 select * from #t1
Proc1
tables received from Proc1
, or determine when to output them inside Proc1
. For example, based on the existence of a table for insertion: alter procedure Proc1 as begin declare @show bit if object_id(N'tempdb..#t1',N'U') is null begin set @show = 1 create table #t1(p1 int, p2 varchar(max)) end insert #t1(p1, p2) select 1 p1, 'b' p2 if (@show = 1) begin select * from #t1 end end
create type tt1 as table(p1 int, p2 varchar(max)) go create procedure Proc1 as begin insert #t1(p1, p2) select 1 p1, 'b' p2 end go -- : declare @t1 tt1 if object_id(N'tempdb..#t1',N'U') is not null drop table #t1 select * into #t1 from @t1 exec Proc1 select * from #t1
select ... into
constructions select ... into
create procedure Proc1 as begin insert #t1(p1, p2) select 1 p1, 'b' p2 end go create procedure Proc1_AlterTable as begin alter table #t1 add p1 int, p2 varchar(max) alter table #t1 drop column delmy end go -- : if object_id(N'tempdb..#t1',N'U') is not null drop table #t1 create table #t1(delmy int) exec Proc1_AlterTable exec Proc1 select * from #t1
alter table #t1
structure, and the process waits for the complete completion of the Proc1
(not Proc1_AlterTable
!) Parallel query. If anyone knows what this is connected with - share, I will be glad to hear :) if object_id('tempdb..#ttInclusion', 'U') is null create table #ttInclusion(lvl int, i int) exec util.InclusionBegin
#Output
(for example into #Output
, into #Output5
, into #OutputMySelect
). If the procedure does not create a result set, then no action is required. exec util.InclusionEnd -- , #Output, util.InclusionBegin
create procedure Proc1 as begin if object_id('tempdb..#ttInclusion', 'U') is null create table #ttInclusion(lvl int, i int) exec util.InclusionBegin select 1 p1, 'b' p2 into #Output1 exec util.InclusionEnd -- , #Output, util.InclusionBegin end
if object_id('tempdb..#ttInclusionParameters', 'U') is null create table #ttInclusionParameters(lvl int, pr int, val varchar(max)) exec util.InclusionRun' select * from #InclusionOutput1 ', 1, '#InclusionOutput' exec Proc1
util.InclusionRun
functions are passed 3 parameters:@sql
- SQL script that executes inside the called procedure@notShowOutput
- if = 1, then block output of tables starting with #Output
@replaceableTableName
- (default = '#Output'
) set the prefix in the name of the tables used in @sql
, to replace it with the corresponding #Output*
table in the script. For example, if you specify #InclusionOutput
, and two tables #Output55
and #Output0A
created in the procedure, in @sql
you can refer to #Output55
as #InclusionOutput1
, and #Output0A
as #InclusionOutput2
Proc1
, without first running util.InclusionRun
leads to the natural work of the procedure with the output of all the data that it displayed before processing.return
in the procedure, since it needs to be started util.InclusionEnd
util.InclusionEnd
)#Output
tables will be displayed if object_id('dbo.TestInclusion') is not null drop procedure dbo.TestInclusion go create procedure dbo.TestInclusion @i int as begin if object_id('tempdb..#ttInclusion', 'U') is null create table #ttInclusion(lvl int, i int) exec util.InclusionBegin if object_id('tempdb..#tmp2', 'U') is not null drop table #tmp2 select @i myI into #tmp2 if object_id('tempdb..#tmp3', 'U') is not null drop table #tmp3 select @i + 1 myI into #tmp3 select * into #Output0 -- ( util.InclusionEnd) from #tmp2 union all select * from #tmp3 select ' TestInclusion' alt into #OutputQwerty -- ( util.InclusionEnd) exec util.InclusionEnd -- #Output util.InclusionBegin end go set nocount on set ansi_warnings off if object_id('tempdb..#ttInclusionParameters', 'U') is not null drop table #ttInclusionParameters go select ' 1: TestInclusion. myI : 2 3. 1 : " TestInclusion"' exec dbo.TestInclusion 2 go select ' 2: TestInclusion. testSum : 5' if object_id('tempdb..#ttInclusionParameters', 'U') is null create table #ttInclusionParameters(lvl int, pr int, val varchar(max)) exec util.InclusionRun ' select sum(myI) testSum from #InclusionOutput1 ', 1, '#InclusionOutput' exec dbo.TestInclusion 2
----------------------------------------------------------------------------------------------------------------------------------------------------------- 1: TestInclusion. myI : 2 3. 1 : " TestInclusion" myI ----------- 2 3 alt ----------------------- TestInclusion ------------------------------------------------------------------------------------------------------ 2: TestInclusion. testSum : 5 testSum ----------- 5
if not exists(select top 1 null from sys.schemas where name = 'util') begin exec ('create schema util') end go alter procedure util.InclusionBegin as begin /* : 1. 1.1. : if object_id('tempdb..#ttInclusion', 'U') is null create table #ttInclusion(lvl int, i int) exec util.InclusionBegin 1.1. select' #Output ( into #Output, into #Output5, into #OutputMySelect) 1.2. : exec util.InclusionEnd -- , #Output, util.InclusionBegin 2. , , ( #Output* ): if object_id('tempdb..#ttInclusionParameters', 'U') is null create table #ttInclusionParameters(lvl int, pr int) exec util.InclusionRun('<sql >') . util.InclusionRun */ set nocount on set ansi_warnings off declare @lvl int if object_id('tempdb..#ttInclusionParameters', 'U') is not null begin select @lvl = max(lvl) from #ttInclusionParameters -- null , if (@lvl is not null) begin insert #ttInclusionParameters(lvl, pr) select @lvl+1 lvl, null pr end end if object_id('tempdb..#ttInclusion', 'U') is not null begin -- #Output, util.InclusionEnd insert #ttInclusion(lvl, i) select isnull(@lvl, 0), so.object_id i from tempdb.sys.objects so where so.type = 'U' and so.name like '#[^#]%' and object_id('tempdb..' + so.name, 'U') is not null and not exists (select top 1 null from #ttInclusion where i = so.object_id) end end GO go alter procedure util.InclusionEnd as begin /* : 1. 1.1. : if object_id('tempdb..#ttInclusion', 'U') is null create table #ttInclusion(lvl int, i int) exec util.InclusionBegin 1.1. select' #Output ( into #Output, into #Output5, into #OutputMySelect) 1.2. : exec util.InclusionEnd -- , #Output, util.InclusionBegin 2. , , ( #Output* ): if object_id('tempdb..#ttInclusionParameters', 'U') is null create table #ttInclusionParameters(lvl int, pr int) exec util.InclusionRun('<sql >') . util.InclusionRun */ set nocount on set ansi_warnings off ---------------------------------------------------------------------------------------------------- -- declare @lvl int , @p0 varchar(max) --(@sql) sql , @p1 varchar(max) --(@notShowOutput) '1' , #Output, , @p2 varchar(max) --(@replaceableTableName) if object_id('tempdb..#ttInclusionParameters', 'U') is not null begin -- select @p1 = max(val) from #ttInclusionParameters where pr = 1 -- (max(lvl) - null util.InclusionBegin) select @lvl = max(lvl) - 1 from #ttInclusionParameters if @lvl is not null begin -- select @p0 = max(case when pr = 0 then val end) , @p2 = max(case when pr = 2 then val end) from #ttInclusionParameters where lvl = @lvl having max(pr) is not null -- , , null- delete #ttInclusionParameters where lvl >= @lvl and (lvl > @lvl or @p0 is not null) end end ---------------------------------------------------------------------------------------------------- -- #Output if object_id('tempdb..#InclusionOutputs', 'U') is not null drop table #InclusionOutputs create table #InclusionOutputs(i int, tableName varchar(max), num int) if object_id('tempdb..#ttInclusion', 'U') is not null begin insert #InclusionOutputs(i, tableName, num) select so.object_id i, left(so.name, charindex('_', so.name)-1) tableName, row_number() over (order by so.create_date) num from tempdb.sys.objects so where so.type = 'U' and so.name like '#[^#]%' and object_id('tempdb..' + so.name, 'U') is not null and so.name like '#Output%' and not exists (select top 1 null from #ttInclusion where i = so.object_id and lvl <= isnull(@lvl, lvl)) -- , delete #ttInclusion where lvl <= @lvl end else begin insert #InclusionOutputs(i, tableName, num) select so.object_id i, left(so.name, charindex('_', so.name)-1) tableName, row_number() over (order by so.create_date) num from tempdb.sys.objects so where so.type = 'U' and so.name like '#[^#]%' and object_id('tempdb..' + so.name, 'U') is not null and so.name like '#Output%' end ---------------------------------------------------------------------------------------------------- -- ( - #Output) declare @srcsql varchar(max) -- util.InclusionRun if (@p0 is not null and @p0 <> '') begin -- @replaceableTableName if (@p2 is not null and @p2 <> '') begin select @p0 = replace(@p0, @p2 + cast(num as varchar(10)), replace(tableName, '#', '#<tokenAfterReplace>')) from #InclusionOutputs order by num desc select @p0 = replace(@p0, '<tokenAfterReplace>', '') end -- select @srcsql = isnull(@srcsql + ' ' + char(13), '') + @p0 + ' ' + char(13) end -- #Output if (@p1 is null or @p1 <> '1') -- 1, ! begin -- select @srcsql = isnull(@srcsql + ' ' + char(13), '') -- select @srcsql = isnull(@srcsql + ' ', '') + 'select * from ' + tableName from #InclusionOutputs order by num asc end if (@srcsql is not null) begin exec (@srcsql) end end go alter procedure util.InclusionRun @sql varchar(max), --sql ( util.InclusionEnd) @notShowOutput bit, -- = 1, #Output @replaceableTableName varchar(100) = '#Output' -- @sql, #Output* . -- , #InclusionOutput, #Output55 #Output0A, -- @sql #Output55 #InclusionOutput1, #Output0A #InclusionOutput2 as begin set nocount on set ansi_warnings off if object_id('tempdb..#ttInclusionParameters', 'U') is null begin print ' util.InclusionRun , .. #ttInclusionParameters! ' return end declare @lvl int select @lvl = isnull(max(lvl), 0) + 1 from #ttInclusionParameters insert #ttInclusionParameters(lvl, pr, val) select @lvl, 0, @sql union all select @lvl, 1, '1' where @notShowOutput = 1 union all select @lvl, 2, @replaceableTableName end
OUTPUT
) and, based on its value, restore the table. For example, you can pass a cursor or XML.Source: https://habr.com/ru/post/217649/
All Articles