sql-server - 使用变量调试 CTE?
问题描述
很多时候,我想用@variable 查看某个查询的结果,CTE
而不必通过select * from cte
在末尾添加来改变整个事情。
一个例子是这样的:
declare @From date
declare @To date
declare @city varchar(20)
declare @st varchar(5)
// Lots of code that sets all the @variables
;with cteSales as
(
select * from MyTable
where
From = @From and To = @To and
salesman = @salesman and city = @city and st = @st
)
//HERE GOES A LONG QUERY THAT USES cteSales
我知道在 CTE 中调试查询的唯一方法是 1)用值替换变量并执行查询或 2)在之后注释所有内容cteSales
并添加select * from cteSales
.
后者不太不舒服,但两者都需要从原始代码中更改很多内容。
是否可以在不使用上述两个选项中的任何一个的情况下调试 aselect
中的语句?cte
解决方案
您也可以将非常长的查询包装到 cte 中,然后在底部您所要做的就是注释掉一行。
;with cteSales as
(
select * from MyTable
where
From = @From and To = @To and
salesman = @salesman and city = @city and st = @st
)
, cteVeryLongQuery as (
//HERE GOES A LONG QUERY THAT USES cteSales
)
SELECT * FROM cteVeryLongQuery
-- SELECT * FROM cteSales -- Uncomment this for debugging `cteSales` and comment out the line above.
最后,如果您使用的是 SQL Management Studio,请使用快捷方式Ctrl+K+C
注释掉行并Ctrl+K+U
取消注释。
推荐阅读
- java - 使用 xml 配置休息 Spring bean
- ios - 在另一个文件的函数主视图中显示警报
- spring - Spring application.properties 中的 Integer.MAX_VALUE
- android - 自 API 26 起不推荐使用 Build.SERIAL:Android 8.0 Oreo
- vba - 在 VBA Excel 中设置工作簿语言
- android - 如何从 swig 生成 java 文件?这是由 gradle 完成的还是用命令行完成的?
- java - OrientDB Java 批量导入
- css - Bootstrap 内联块无法正常工作
- mysql - 如何在计数查询中使用“AS”?
- windows - 在 Mac 上为 64 位 Windows 编译 Haskell