简体   繁体   English

T-SQL:创建具有灵活结构的临时表/表变量

[英]T-SQL: Creating a temp Table/ table Variable with Flexible structure

I am working on a reporting project based in SQL but I have restricted access to the DB; 我正在基于SQL的报告项目上工作,但是我对数据库的访问受到限制; I can only make SELECT Queries and insert the data I retrieve into temp Tables/table variables. 我只能进行SELECT查询,并将检索到的数据插入到临时表/表变量中。 I cannot create/execute stored procedures or any sort of functions. 我无法创建/执行存储过程或任何类型的功能。

The query I am running is meant to pool together all Engineers and the different key skills that they have so that we can later on see what Skills each engineer has or which Engineers fall under a certain skill. 我正在运行的查询旨在将所有工程师及其拥有的不同关键技能集中在一起,以便我们以后可以查看每位工程师具有哪些技能或属于某个技能的工程师。

To this end, I am trying to create a table variable/temp table with a flexible structure, a structure based on previously obtained values in the same query. 为此,我试图创建一个具有灵活结构的表变量/临时表,该结构基于同一查询中先前获得的值。

For Eg 例如

1st Output: Adam Brad Julio Martinez 第一输出:Adam Brad Julio Martinez

2nd Output (Skill separated by white space): VOIP TTS DBA Exchange Server 第二输出(技能用空格隔开):VOIP TTS DBA Exchange Server

Create temp table/table variable that uses 1st output as rows and 2nd output as columns or vice versa. 创建临时表/表变量,将第一个输出用作行,第二个输出用作列,反之亦然。 I will then populate this new table according to different values on the main DB. 然后,我将根据主数据库上的不同值填充此新表。

Please advise how this can be done, or provide any other solution to this problem. 请告知如何完成此操作,或提供其他解决方案。 Thank you 谢谢

I believe you can. 我相信你可以。

First of all you need to create temp table with dynamic structure based on query. 首先,您需要基于查询创建具有动态结构的临时表。 It can be done like this: 可以这样完成:

declare script template: 声明脚本模板:

Set @ScriptTmpl = 'Alter table #tempTable Add [?] varchar(100); 设置@ScriptTmpl ='Alter table #tempTable Add [?] varchar(100);

build script that will insert columns you need based on query: 构建脚本,将根据查询插入您需要的列:

Select @TableScript = @TableScript + Replace(@ScriptTmpl, '?', ColumnName) From ... Where ... 选择@TableScript = @TableScript + Replace(@ScriptTmpl,'?',ColumnName)从...

then execute script and then fill your new table with values from second query 然后执行脚本,然后使用第二个查询的值填充新表

UPD: UPD:

here is the full sample of temporary table dynamic creation. 这是临时表动态创建的完整示例。 I used global temporary table in my sample: 我在示例中使用了全局临时表:

 declare @scriptTemplate nvarchar(MAX) declare @script nvarchar(MAX) declare @tableTemplate nvarchar(MAX) SET @tableTemplate = 'create table ##tmptable (?)' SET @scriptTemplate = '? nvarchar(500),' SET @script = '' Drop table ##tmptable Select @script = @script + Replace(@scriptTemplate, '?', [Name]) From Account Where name like 'ES_%' SET @script = LEFT(@script, LEN(@script) - 1) SET @script = Replace(@tableTemplate, '?', @script) Select @script exec(@script) Select * from ##tmptable 

Firstly, you may be able to achieve what you want through pivots , rather than temporary tables. 首先,您可能可以通过数据透视表而不是临时表来实现所需的功能。

Secondly, if you really want to create a table with column name "Adam Brad", the solution is dynamic SQL , which you may not be able to do based on your permissions. 其次,如果您确实要创建一个列名称为“ Adam Brad”的表,则解决方案是动态SQL ,您可能无法根据您的权限执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM