简体   繁体   English

在SQL Server 2008中使用动态SQL生成临时表

[英]Build temporary table with dynamic sql in SQL Server 2008

To make a long story short... 使长话短说...

I'm building a web app in which the user can select any combination of about 40 parameters. 我正在构建一个Web应用程序,用户可以在其中选择约40个参数的任意组合。 However, for one of the results they want(investment experience), I have to extract information from a different table and compare the values in six different columns(stock exp, mutual funds exp, etc) and return only the highest value of the six for that specific record. 但是,对于他们想要的结果之一(投资经验),我必须从不同的表中提取信息,并比较六个不同列(股票exp,共同基金exp等)中的值,并且仅返回六个中的最高值该特定记录。

This is not the issue. 这不是问题。 The issue is that at runtime, my query to find the investment exp doesn't necessarily know the account id. 问题在于,在运行时,我用于查找投资exp的查询不一定知道帐户ID。 Considering a table scan would bring well over half a million clients, this is not an option. 考虑到表扫描将带来超过五百万的客户,这不是一个选择。 So what I'm trying to do is edit a copy of my main dynamically built query, but instead of returning 30+ columns, it'll just return 2, the accountid and experienceid (which is the PK for the experience table) so I can do the filtering deal. 所以我想做的是编辑我主要的动态构建查询的副本,但是与其返回30+列,不如返回2,accountid和experienceid(这是Experience表的PK),所以我可以做过滤交易。

Some of you may define dynamic SQL a little different than myself. 你们中有些人可能定义的动态SQL与我自己有所不同。 My query is a string that depending on the arguments sent to my procedure, portions of the where clause will be turned on or off by switches. 我的查询是一个字符串,根据发送到我的过程的参数,where子句的某些部分将通过开关打开或关闭。 In the end I execute, it's all done on the server side, all the web app does is send an array of arguments to my proc. 最后,我执行完毕,这一切都在服务器端完成,Web应用程序要做的就是向我的proc发送一个参数数组。

My over simplified code looks essentially like this: 我过度简化的代码本质上是这样的:

declare @sql varchar(8000)
set @sql = 
'select [columns]
into #tempTable
from [table]
[table joins]' + @dynamicallyBuiltWhereClause

exec(@sql)

after this part I try to use #tempTable for the investment experience filtering process, but i get an error telling me #tempTable doesn't exist. 在这部分之后,我尝试使用#tempTable进行投资经验筛选过程,但是我收到一条错误消息,告诉我#tempTable不存在。

Any and all help would be greatly appreciated. 任何和所有的帮助将不胜感激。

The problem is the scope of your temp table only exists within the exec() statement. 问题是临时表的范围仅存在于exec()语句中。 You can transform your temp table into a "global" temp table by using 2 hash signs -> ##tempTable. 您可以使用2个哈希符号-> ## tempTable将临时表转换为“全局”临时表。 However, I wonder why you are using a variable @dynamicallyBuiltWhereClause to generate your SQL statement. 但是,我不知道为什么要使用变量@dynamicallyBuiltWhereClause生成SQL语句。

I have done what you are doing in the past, but have had better success generating SQL from the application (using C# to generate my SQL). 我已经做了过去的工作,但是从应用程序生成SQL方面取得了更好的成功(使用C#生成我的SQL)。

Also, you may want to look into Table Variables. 另外,您可能需要查看表变量。 I have seen some strange instances using temp tables where an application re-uses a connection and the temp table from the last query is still there. 我已经看到了一些使用临时表的奇怪实例,其中应用程序重新使用了连接,而最后一个查询的临时表仍然存在。

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

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