SQL Server Management Studio 中的查询优化
Query optimisation in SQL Server Management Studio
在下面给出的查询中,在 where 条件下 table 'a' 不可访问。如何在不影响代码逻辑的情况下使其可访问?
提前致谢。
select *
from Training_Data.dbo.test a
cross apply (select * from Training_data.dbo.test_table) b
where exists (select 1 from a)
要使 table 可访问,请使用真实的 table 名称而不是别名。
select *
from Training_Data.dbo.test a
cross apply (select * from Training_data.dbo.test_table) b
where exists (select 1 from Training_Data.dbo.test)
顺便说一下,我认为在 IF 子句中的 select 之前检查 table 中的数据是否存在并使用交叉连接而不是交叉应用
if exists (select 1 from #T1)
select *
from #T1 a
cross join #T1 b
您可以在 WHERE
中的任何 EXIST
中访问您引用的 tables 列,您只需要正确的语法。
示例:SELECT
如果在另一个 table 中有一条具有相同值的记录(引用来自 table test
的列:
select
*
from
Training_Data.dbo.test a
cross apply (select * from Training_data.dbo.test_table) b
where
exists (
select
1
from
Training_data.dbo.another_test_table c
where
a.someColumn = c.anotherColumn)
示例:SELECT
如果 table 上有记录(不引用任何列):
select
*
from
Training_Data.dbo.test a
cross apply (select * from Training_data.dbo.test_table) b
where
exists (
select
1
from
Training_Data.dbo.test c)
对于最后一个示例,如果您想要 select 来自 table 的行,仅当它们存在于 相同的 table 中时,那么您的 SELECT
将已经处理该问题,因为它将获取 是 的行 table.
程序员可以在 SELECT 查询期间使用别名临时为 table 或列分配另一个名称。分配别名实际上不会重命名列或 table。当 table 或其列的名称很长或很复杂时,这通常很有用。
希望你得到你的答案。如果您不想更改任何内容,可以使用列名放置条件,但不能使用别名 table.
在下面给出的查询中,在 where 条件下 table 'a' 不可访问。如何在不影响代码逻辑的情况下使其可访问?
提前致谢。
select *
from Training_Data.dbo.test a
cross apply (select * from Training_data.dbo.test_table) b
where exists (select 1 from a)
要使 table 可访问,请使用真实的 table 名称而不是别名。
select *
from Training_Data.dbo.test a
cross apply (select * from Training_data.dbo.test_table) b
where exists (select 1 from Training_Data.dbo.test)
顺便说一下,我认为在 IF 子句中的 select 之前检查 table 中的数据是否存在并使用交叉连接而不是交叉应用
if exists (select 1 from #T1)
select *
from #T1 a
cross join #T1 b
您可以在 WHERE
中的任何 EXIST
中访问您引用的 tables 列,您只需要正确的语法。
示例:SELECT
如果在另一个 table 中有一条具有相同值的记录(引用来自 table test
的列:
select
*
from
Training_Data.dbo.test a
cross apply (select * from Training_data.dbo.test_table) b
where
exists (
select
1
from
Training_data.dbo.another_test_table c
where
a.someColumn = c.anotherColumn)
示例:SELECT
如果 table 上有记录(不引用任何列):
select
*
from
Training_Data.dbo.test a
cross apply (select * from Training_data.dbo.test_table) b
where
exists (
select
1
from
Training_Data.dbo.test c)
对于最后一个示例,如果您想要 select 来自 table 的行,仅当它们存在于 相同的 table 中时,那么您的 SELECT
将已经处理该问题,因为它将获取 是 的行 table.
程序员可以在 SELECT 查询期间使用别名临时为 table 或列分配另一个名称。分配别名实际上不会重命名列或 table。当 table 或其列的名称很长或很复杂时,这通常很有用。 希望你得到你的答案。如果您不想更改任何内容,可以使用列名放置条件,但不能使用别名 table.