如何在SQL Server中创建临时表 [英] how to create a temp table in sql server
本文介绍了如何在SQL Server中创建临时表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DECLARE @cols NVARCHAR(300)
DECLARE @dt NVARCHAR(1000)
DECLARE @pt NVARCHAR(2000)
SET @cols = STUFF((SELECT DISTINCT '],[' + [text]
FROM B
ORDER BY '],[' + [text]
FOR XML PATH('')),1,2,'') + ']'
SET @dt = 'SELECT B.[ID], B.[Text], B.[Value] ' +
'FROM A LEFT JOIN B ON A.[ID] = B.[ID] '
EXEC(@dt)
SET @pt = 'SELECT [ID], ' + @cols + ' ' +
'FROM (' + @dt + ') AS DT ' +
'PIVOT(MAX([Value]) FOR [Text] IN (' + @cols + ')) AS PT ' +
'ORDER BY [ID] '
EXEC(@pt)
我有一个存储过程,现在我要在其中创建动态列,我想将结果存储在临时表中.
I have this stored procedure in which I create dynamic column now I want to store that result in temp table.
推荐答案
用于创建临时表:
EX:
创建表#tempTable(Name varchar(30))//#TableName用于创建临时表
For creating Temporary table :
EX :
create table #tempTable(Name varchar(30)) // #TableName for creating temp table
用于创建临时表:
For create temporary table:
Create Table #tablename(columnname datatype,...)
除表名外,其他所有内容都与普通表创建命令几乎相同. #符号添加为前缀.
该表在范围内可用.并且最好通过
Every thing are almost same as normal table create command except table name. A # symbol add as prefix.
The table available within the scope. And it''s better to drop table by
drop table #tablename
使用后
这篇关于如何在SQL Server中创建临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文