[英]Query optimisation in SQL Server Management Studio
在下面給出的查詢中,在where條件下,無法訪問表'a'。 如何在不影響代碼邏輯的情況下使其可訪問?
提前致謝。
select *
from Training_Data.dbo.test a
cross apply (select * from Training_data.dbo.test_table) b
where exists (select 1 from a)
要使表可訪問,請使用真實表名而不是別名。
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之前檢查表中數據的存在並使用交叉連接而不是交叉應用會更合適。
if exists (select 1 from #T1)
select *
from #T1 a
cross join #T1 b
您可以在WHERE
內的任何EXIST
訪問引用的表列,只需要正確的語法即可。
例如: SELECT
,如果有另一個表與相同值的記錄(參考從表中的列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
,如果有記錄在表上(不參考任何列):
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
都已經搞定,因為它會取那些在表中的行。
程序員可以使用別名在SELECT查詢期間將臨時名稱臨時分配給表或列。 分配別名實際上不會重命名列或表。 當表或其列的名稱很長或很復雜時,這通常很有用。 希望你能得到答案。 如果您不想更改任何內容,則可以使用列名作為條件,但不能將別名用作表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.