sql参数化cte查询
我有一个如下所示的查询
select *
from (
select *
from callTableFunction(@paramPrev)
.....< a whole load of other joins, wheres , etc >........
) prevValues
full join
(
select *
from callTableFunction(@paramCurr)
.....< a whole load of other joins, wheres , etc >........
) currValues on prevValues.Field1 = currValues.Field1
....<other joins with the same subselect as the above two with different parameters passed in
where ........
group by ....
以下子选择对于查询栏中的所有子选择都是通用的,@param 到表函数。
select *
from callTableFunction(@param)
.....< a whole load of other joins, wheres , etc >........
一种选择是我将其转换为函数并调用该函数,但我不喜欢这个,因为我可能会更改 经常进行子选择查询......或者我想知道是否有使用 CTE 的替代方案 有
with sometable(@param1) as
(
select *
from callTableFunction(@param)
.....< a whole load of other joins, wheres , etc >........
)
select
sometable(@paramPrev) prevValues
full join sometable(@currPrev) currValues on prevValues.Field1 = currValues.Field1
where ........
group by ....
没有像这样的语法或我可以这样使用的技术。
这是 SQL Server 2008 R2 中的,
谢谢。
I have a query like the following
select *
from (
select *
from callTableFunction(@paramPrev)
.....< a whole load of other joins, wheres , etc >........
) prevValues
full join
(
select *
from callTableFunction(@paramCurr)
.....< a whole load of other joins, wheres , etc >........
) currValues on prevValues.Field1 = currValues.Field1
....<other joins with the same subselect as the above two with different parameters passed in
where ........
group by ....
The following subselect is common to all the subselects in the query bar the @param to the table function.
select *
from callTableFunction(@param)
.....< a whole load of other joins, wheres , etc >........
One option is for me to convert this into a function and call the function, but i dont like this as I may be changing the
subselect query quite often for.....or I am wondering if there is an alternative using CTE
like
with sometable(@param1) as
(
select *
from callTableFunction(@param)
.....< a whole load of other joins, wheres , etc >........
)
select
sometable(@paramPrev) prevValues
full join sometable(@currPrev) currValues on prevValues.Field1 = currValues.Field1
where ........
group by ....
Is there any syntax like this or technique I can use like this.
This is in SQL Server 2008 R2
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尝试执行的语法不受支持 - CTE 无法以这种方式参数化。
在线查看书籍 - http://msdn.microsoft.com/en-us/library /ms175972.aspx。
(CTE 名称后面括号中的值是输出列名称的可选列表)
如果只有两个参数值(
paramPrev
和currPrev
),您也许可以通过将代码分成两个 CTE,代码更容易阅读 - 如下所示:What you're trying to do is not supported syntax - CTE's cannot be parameterised in this way.
See books online - http://msdn.microsoft.com/en-us/library/ms175972.aspx.
(values in brackets after a CTE name are an optional list of output column names)
If there are only two parameter values (
paramPrev
andcurrPrev
), you might be able to make the code a little easier to read by splitting them into two CTEs - something like this:您应该能够将子查询包装为参数化内联表值函数,然后将它们与 OUTER JOIN 一起使用:
You should be able to wrap the subqueries up as parameterized inline table-valued functions, and then use them with an OUTER JOIN:
在
with
之前分配标量变量失败后,我终于得到了一个使用存储过程和临时表的工作解决方案:调用存储过程:
After failing to assign scalar variables before
with
, i finally got a working solution using a stored procedure and a temp table:Calling the stored procedure:
我的示例与您想要的之间可能存在一些差异,具体取决于您后续 ON 语句的制定方式。由于您没有指定,我假设所有后续连接都针对第一个表。
在我的示例中,我使用文字而不是 @prev、@current,但您可以轻松地用变量代替文字来实现您想要的效果。
您想要做的事情类似于数据透视,因此所需查询的复杂性类似于在不使用数据透视语句的情况下创建数据透视结果。
如果您使用数据透视表,重复的行(例如我在本示例中包含的行)将被聚合。这也是在不需要聚合的情况下进行数据透视的解决方案。
There may be some differences between my example and what you want depending on how your subsequent ON statements are formulated. Since you didn't specify, I assumed that all the subsequent joins were against the first table.
In my example I used literals rather than @prev,@current but you can easily substitute variables in place of literals to achieve what you want.
What you want to do is akin to a pivot and so the complexity of the needed query is similar to creating a pivot result without using the pivot statement.
Were you to use Pivot, duplicate rows (such as I included in this example) would be aggreagted. This is also a solution for doing a pivot where aggregation is unwanted.