想要通过执行SELECT
操作来减少OPENQUERY
上JOIN
的数量来调整性能。
这是我的查询的样子。
查询:
;WITH CTE AS
(
SELECT
[EmployeeFirstName], [EmployeeMiddleName],
[EmployeeLastName], [EmployeeAddress]
FROM
Employee
WHERE
[EmployeeSalary] >= 200000
)
SELECT
[First Name], [Fathers Husband Name], [Last Name], [Department Name]
FROM
OPENQUERY ([Depart], 'SELECT [First Name], [Fathers Name], [Last Name],[Department Name]
FROM Department
INNER JOIN CTE C1 ON PATINDEX(''[First Name]'',''C1.[EmployeeFirstName]'') > 0');
错误:
无效的对象名称“ CTE”
答案 0 :(得分:1)
尝试以下
;WITH CTE AS
(
SELECT
[EmployeeFirstName], [EmployeeMiddleName],
[EmployeeLastName], [EmployeeAddress]
FROM
LocalServer.Database.Schema.Employee
WHERE
[EmployeeSalary] >= 200000
)
SELECT
[First Name], [Fathers Husband Name], [Last Name], [Department Name]
--It's better to specify the table eg: C1.ColumnName
FROM
(
SELECT [First Name], [Fathers Name], [Last Name],[Department Name]
FROM LinkedServer.Database.Schema.Department
INNER JOIN CTE C1
ON PATINDEX([First Name],C1.[EmployeeFirstName]) > 0
) TBL;