遍历临时 table 列以 select 它们
Iterate through temporary table columns to select them
我有一个最终的临时 table (#tempTable),其列号未知。
我的最终 select 是这样的,有效:
SELECT temp.* FROM #tempTable temp
但我想单独调用每一列,而不是“*”:
SELECT temp.col1, temp.col2 FROM #tempTable temp
为此,我需要遍历我的列名并创建一个过程,我试过这样的事情:
DECLARE @ColName VARCHAR(255)
SELECT @ColName = min(name) FROM tempdb.sys.columns
WHERE object_id = Object_id('tempdb..#TEMPTABLE');
WHILE @ColName is not null
BEGIN
-- i need to do it all in once and not each time....
declare @sql varchar(max) = 'SELECT tp.'+'@COlName'+'FROM #TEMPTABLE tp'
exec(@sql)
-- Increment the value, how to go to next column ?
select @ColName = min(name) FROM tempdb.sys.columns WHERE object_id =
Object_id('tempdb..#TEMPTABLE') > @ColName -- does not work because it is a string (column name)
END
试试这个:
DECLARE @ColName VARCHAR(2000) = 'select '
SELECT @ColName = @ColName + ' temp.' + name + ',' FROM tempdb.sys.columns
WHERE object_id = Object_id('tempdb..#TEMPTABLE')
--delete last character, which is comma and append table name
@ColName = substring(@ColName, 1, LEN(@ColName) - 1) + ' from #TEMPTABLE temp'
exec(@ColName)
此查询构建了整个 table 列表并合并到 select ... from ...
语句中。我增加了 varchar
变量的大小,因此它可以容纳长查询。
此外,IMO 变量名称如 @sql
或 @query
会更有意义。
基于集合的方法
IF OBJECT_ID('tempdb..#TEMPTABLE','U') IS NOT NULL
DROP TABLE #TEMPTABLE;
CREATE TABLE #TEMPTABLE (
Id INT IDENTITY(1,1)
,Col1 INT
,Col2 BIGINT
,Col3 BIGINT
,Col4 DATETIME
,Col5 DATETIME
) ;
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = N'SELECT ' + SUBSTRING((
SELECT N', temp.' + S.name
FROM
tempdb.sys.columns S
WHERE
S.object_id = OBJECT_ID('tempdb..#TEMPTABLE')
ORDER BY
S.column_id
FOR XML PATH('')
)
,2
,200000
) + N' FROM #TEMPTABLE temp'
EXEC sys.sp_executesql @SQL
我有一个最终的临时 table (#tempTable),其列号未知。 我的最终 select 是这样的,有效:
SELECT temp.* FROM #tempTable temp
但我想单独调用每一列,而不是“*”:
SELECT temp.col1, temp.col2 FROM #tempTable temp
为此,我需要遍历我的列名并创建一个过程,我试过这样的事情:
DECLARE @ColName VARCHAR(255)
SELECT @ColName = min(name) FROM tempdb.sys.columns
WHERE object_id = Object_id('tempdb..#TEMPTABLE');
WHILE @ColName is not null
BEGIN
-- i need to do it all in once and not each time....
declare @sql varchar(max) = 'SELECT tp.'+'@COlName'+'FROM #TEMPTABLE tp'
exec(@sql)
-- Increment the value, how to go to next column ?
select @ColName = min(name) FROM tempdb.sys.columns WHERE object_id =
Object_id('tempdb..#TEMPTABLE') > @ColName -- does not work because it is a string (column name)
END
试试这个:
DECLARE @ColName VARCHAR(2000) = 'select '
SELECT @ColName = @ColName + ' temp.' + name + ',' FROM tempdb.sys.columns
WHERE object_id = Object_id('tempdb..#TEMPTABLE')
--delete last character, which is comma and append table name
@ColName = substring(@ColName, 1, LEN(@ColName) - 1) + ' from #TEMPTABLE temp'
exec(@ColName)
此查询构建了整个 table 列表并合并到 select ... from ...
语句中。我增加了 varchar
变量的大小,因此它可以容纳长查询。
此外,IMO 变量名称如 @sql
或 @query
会更有意义。
基于集合的方法
IF OBJECT_ID('tempdb..#TEMPTABLE','U') IS NOT NULL
DROP TABLE #TEMPTABLE;
CREATE TABLE #TEMPTABLE (
Id INT IDENTITY(1,1)
,Col1 INT
,Col2 BIGINT
,Col3 BIGINT
,Col4 DATETIME
,Col5 DATETIME
) ;
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = N'SELECT ' + SUBSTRING((
SELECT N', temp.' + S.name
FROM
tempdb.sys.columns S
WHERE
S.object_id = OBJECT_ID('tempdb..#TEMPTABLE')
ORDER BY
S.column_id
FOR XML PATH('')
)
,2
,200000
) + N' FROM #TEMPTABLE temp'
EXEC sys.sp_executesql @SQL