背景:我为第三方网络平台编写自定义SQL报告 - 多个数据库,其中大多数是SQL Server 2008 R2或2012 - 我遇到了一种情况其中临时表未在我的脚本的不同部分中维护。我无法确定是否只是范围不同,或者这些部分是否使用单独的连接。在任何情况下,对我来说这都是一个大问题,因为它意味着必须从头开始多次重建临时表,或者在我不需要时创建永久表。
为简化起见,我编写了一个存储过程,它接受临时表(或任何表,实际上)的内容,将其转动,并将列作为行存储在永久表中。这是永久表:
CREATE TABLE [dbo].[CustomTempRows] (
[who] [varchar](25) NOT NULL -- A session variable from the web server
,[dtdate] [datetime] NOT NULL -- The date the row was entered
,[stable] [varchar](100) NOT NULL -- The name of the source table
,[scolumn] [varchar](100) NOT NULL -- The name of the source column
,[irow] [int] NOT NULL -- The row number from the source table
,[icolumn] [int] NOT NULL -- The column number from the source table
,[itype] [int] NOT NULL -- Whether or not the column is a string, date, or number
,[dvalue] [numeric](24, 8) NULL -- The value from the source table if it's a number.
,[svalue] [nvarchar](max) NULL -- The value from the source table if it's a string.
,[dtvalue] [datetime] NULL -- The value from the source table if it's a date.
)
该过程获取临时表中每一行的每一列,并将其作为一行插入CustomTempRows
表中。它要求临时表具有名为TID
的标识列,以确定行号,该行号进入irow
。为简单起见,它将值转换为nvarchar,numeric或datetime - 我相信在99%的情况下这应该没问题。作为参考,这是程序:
create procedure Custom_Table_Columns_to_Rows (@who varchar(max), @dtdate datetime, @stablename varchar(max), @breverse int)
as
BEGIN
/* NOTES
1. Temp table MUST have an identity column called TID
2. The procedure strips out timestamp and sysname columns
*/
declare @temptable varchar(100)
set @temptable = '##' + convert(varchar,@@SPID)
if @breverse = 0 /* This parameter indicates that we are inserting to the custom table
rather than selecting. Not sure if selecting is actually possible
in stored procedure */
BEGIN
/* Clean up the custom table */
delete customtemprows where (who = @who and stable = @stablename) or datediff(mi,dtdate,getdate() )>60
/* Dynamic SQL to create a global temp table and copy the user's temp table into it */
exec('if OBJECT_ID(''tempdb..' + @temptable + ''') is not null drop table ' + @temptable + ';
select * into ' + @temptable + ' from ' + @stablename )
/* Insert into customtemprows for the row and column numbers, as well as the column names and data types
(which are limited to nvarchar, numeric, and datetime -- everything is converted to one of these)
The data columns are left blank for the moment. */
exec('
insert customtemprows (who, dtdate, stable, scolumn, irow, icolumn, itype)
select 5555
, getdate()
, ''' + @stablename + '''
, c.name
, te.tid
, c.column_id
, itype = case when t.name like ''%char%'' or t.name like ''%name%'' then 1
when t.name like (''%date%'') then 2
else 3 end
from tempdb.sys.columns c
inner join sys.types t on c.system_type_id = t.system_type_id
cross join ' + @temptable + ' te
where
object_id = object_id(''tempdb..' + @temptable + ''')
and t.name not in (''sysname'', ''timestamp'')
and c.name<>''tid''
order by te.tid, c.column_id')
/* Create variables to use when running the loop */
declare @cols as table (icolumn int, scolumn varchar(100), itype int)
insert @cols
select distinct icolumn, scolumn, itype from customtemprows where who=@who and stable = @stablename and scolumn not in ('tid') order by icolumn
declare @icolumn int, @scolumn varchar(100), @itype int
select top 1 @icolumn = icolumn, @scolumn = scolumn, @itype = itype from @cols order by icolumn
/* Loop through as long as there are columns */
while exists (select * from @cols)
BEGIN
/* If this column is a string, put it into the svalue column */
exec('update r
set svalue = t.' + @scolumn + '
FROM
customtemprows r
inner join
(SELECT tid, ' + @scolumn + '
FROM ' + @temptable + ') t on r.irow=t.tid
where
r.itype = 1
and r.icolumn = ' + @icolumn + '
and r.who = ''' + @who + '''
and r.stable = ''' + @stablename+ '''' )
/* If this column is a date, put it into the dtvalue column */
exec('update r
set dtvalue = t.' + @scolumn + '
FROM
customtemprows r
inner join
(SELECT tid, ' + @scolumn + '
FROM ' + @temptable + ') t on r.irow=t.tid
where
r.itype = 2
and r.icolumn = ' + @icolumn + '
and r.who = ''' + @who + '''
and r.stable = ''' + @stablename+ '''' )
/* If this column is a number, put it into the dvalue column */
exec('update r
set dvalue = convert(numeric(24,10),t.' + @scolumn + ')
FROM
customtemprows r
inner join
(SELECT tid, ' + @scolumn + '
FROM ' + @temptable + ') t on r.irow=t.tid
where
r.itype = 3
and r.icolumn = ' + @icolumn + '
and r.who = ''' + @who + '''
and r.stable = ''' + @stablename+ '''' )
delete @cols where icolumn = @icolumn
select top 1 @icolumn = icolumn, @scolumn = scolumn, @itype = itype from @cols order by icolumn
END /* Loop */
/* Return the inserted values. For troubleshooting */
select * from customtemprows where who = @who and stable = @stablename order by icolumn, irow
END /* @breverse = 0 */
END
我的问题正在找出从CustomTempRows
中获取数据并返回临时表的最佳方法,以便在我的报告的其他部分中使用。我写了一份完成这项工作的声明,但我对此并不满意。我将解释原因,但首先,这是查询:
declare @who varchar(100), @stablename varchar(100), @temptable varchar(100)
/* Set user variables */
set @who = '5555' /* Usually Session ID */
set @stablename = '#temp' /* The temp table you started with */
set @temptable = '##global' /* The name of the global temp table to create */
/* Drop the global temp table if it exists */
exec('if OBJECT_ID(''tempdb..' + @temptable + ''') is not null drop table ' + @temptable )
declare @columns varchar(max)
declare @rows as table (irow int, icolumn int, scolumn varchar(100), itype int, svalue nvarchar(max))
declare @irow int, @icolumn int, @scolumn varchar(100), @itype int, @svalue nvarchar(max), @convert varchar(50)
declare @sql varchar(max)
/* Create a list of columns to include in the temp table */
set @columns = '';
select @columns = @columns + ', ' + scolumn + ' ' + case r.itype when 1 then 'nvarchar(max)' when 2 then 'datetime' else 'numeric(24,10)' end + ' NULL'
FROM (select distinct icolumn, scolumn, itype from customtemprows
where stable=@stablename and who = @who) r
select @columns = STUFF(@columns, 1, 2, '')
/* Create the global temp table and populate the TID column */
exec('create table ' + @temptable + ' (tid int, ' + @columns + ');
insert ' + @temptable + ' (tid)
select distinct irow from customtemprows where stable=''' + @stablename + ''' and who = ''' + @who + '''')
/* create data for the loop */
insert @rows
select irow, icolumn, scolumn, itype
/* Can't combine data types; convert everything to string */
, svalue = coalesce(svalue, convert(nvarchar,dvalue), convert(nvarchar,dtvalue))
from customtemprows where stable = @stablename and who = @who
order by icolumn, irow
select top 1 @irow = irow, @icolumn = icolumn, @scolumn = scolumn, @itype = itype, @svalue = svalue
/* For converting the string back to its previous data type */
, @convert = case when itype = 1 then 'convert(nvarchar(max),svalue)'
when itype = 2 then 'convert(datetime,svalue)'
else 'convert(numeric(24,10),svalue)' end
from @rows order by icolumn, irow
/* As long as there are rows */
while exists (select * from @rows)
BEGIN
set @sql = ''
/* Update the temp table one column at a time with data from the table variable */
select @sql = 'update t
set ' + @scolumn + ' = ' + @convert + '
from
' + @temptable + ' t
inner join (
select irow, icolumn, scolumn, itype, svalue = coalesce(svalue, convert(nvarchar,dvalue), convert(nvarchar,dtvalue))
from customtemprows where stable=''' + @stablename + ''' and who = ''' + @who + ''') r
on r.irow=t.tid and r.icolumn = ' + convert(varchar,@icolumn)
exec(@sql)
delete @rows where icolumn = @icolumn
select top 1 @irow = irow, @icolumn = icolumn, @scolumn = scolumn, @itype = itype, @svalue = svalue
, @convert = case when itype = 1 then 'convert(nvarchar(max),svalue)'
when itype = 2 then 'convert(datetime,svalue)'
else 'convert(numeric(24,10),svalue)' end
from @rows order by icolumn, irow
END /* Loop */
select * from ##global
这不是最优雅的解决方案,我想最终用基于集合的东西替换while循环,但这些是更大的问题:
但是,为了做到这一点,我必须在调用过程之前定义临时表(我正在开发的平台的限制)。因为我使用动态SQL来创建表,所以我无法弄清楚如何在需要使用它的同一范围内创建表。
我还宁愿使用序号来命名全局临时表,以防止两个用户同时访问报表时发生冲突。但是,如果我这样做,我的报告代码的其余部分将不知道表的名称。
我的问题:甚至可以根据CustomTempRows
中可用于我的报告范围的数据定义本地临时表,然后使用存储过程填充该表根据我的长询?
感谢您将其发布到我的帖子的末尾,以及您可能提出的任何建议。很感激。
答案 0 :(得分:0)
创建一个包含1列的临时表,然后动态运行动态SQL以将其他字段添加到临时表中。
DECLARE @SQL varchar(max)
CREATE TABLE #tmp(Id int)
SET @SQL = 'ALTER TABLE #tmp ADD Column1 varchar(20)'
EXEC(@SQL)