在SQL Server 2008中使用动态sql构建临时表
长话短说......
我正在构建一个 Web 应用程序,用户可以在其中选择大约 40 个参数的任意组合。然而,对于他们想要的结果之一(投资经验),我必须从不同的表中提取信息并比较六个不同列中的值(股票指数、共同基金指数等),并仅返回六个中的最高值对于该特定记录。
这不是问题。问题是,在运行时,我查找投资 exp 的查询不一定知道帐户 ID。考虑到表扫描会带来超过 50 万个客户端,这不是一个选择。所以我想做的是编辑我的主要动态构建查询的副本,但它不会返回 30 多个列,而是只返回 2,accountid 和 experienceid(这是经验表的 PK),所以我可以做过滤处理。
你们中的一些人可能对动态 SQL 的定义与我自己略有不同。我的查询是一个字符串,根据发送到我的过程的参数,where 子句的部分将通过开关打开或关闭。最后我执行,这一切都在服务器端完成,所有网络应用程序所做的就是向我的过程发送一组参数。
我的过于简化的代码基本上是这样的:
declare @sql varchar(8000)
set @sql =
'select [columns]
into #tempTable
from [table]
[table joins]' + @dynamicallyBuiltWhereClause
exec(@sql)
在这部分之后,我尝试使用 #tempTable 进行投资经验过滤过程,但我收到一条错误消息,告诉我 #tempTable 不存在。
任何和所有的帮助将不胜感激。
To make a long story short...
I'm building a web app in which the user can select any combination of about 40 parameters. 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.
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. 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.
Some of you may define dynamic SQL a little different than myself. 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. 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.
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.
Any and all help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是临时表的范围仅存在于 exec() 语句中。您可以使用 2 个哈希符号将临时表转换为“全局”临时表 -> ##临时表。但是,我想知道为什么你使用变量 @dynamicallyBuiltWhereClause 来生成 SQL 语句。
我已经完成了您过去正在做的事情,但从应用程序生成 SQL 更成功(使用 C# 生成我的 SQL)。
另外,您可能想研究一下表变量。我见过一些使用临时表的奇怪实例,其中应用程序重复使用连接,并且上次查询的临时表仍然存在。
The problem is the scope of your temp table only exists within the exec() statement. You can transform your temp table into a "global" temp table by using 2 hash signs -> ##tempTable. However, I wonder why you are using a variable @dynamicallyBuiltWhereClause to generate your SQL statement.
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).
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.