我有一个插入语句,像下面这样,会出现语法错误"the multi-part identifier "t2.Col1" could not be bound."
。我简化了语句,它看起来像下面这样:
INSERT INTO dbo.T1
(
Col1,
Col2,
Col3
)
SELECT
t2.Col1,
SUBSTRING(aCase.CaseColumn, 0, CHARINDEX('%', aCase.CaseColumn)), --I expect this line gets the value "2"
SUBSTRING(aCase.CaseColumn, CHARINDEX('%', aCase.CaseColumn) + 1, LEN(aCase.CaseColumn) - CHARINDEX('%', aCase.CaseColumn)) --I expect this line gets the value "3"
FROM
dbo.T2 t2
LEFT JOIN
(
SELECT
CASE --I have hundreds of WHEN conditions below and need to access the parent T2 tables' properties
WHEN t2.Col1 = 1 THEN '2%3' --This line has a syntax error of "the multi-part identifier "t2.Col1" could not be bound."
END AS CaseColumn
)
AS aCase ON 1 = 1
我使用 LEFT JOIN
和 CASE
的原因是我有数百个条件需要选择不同的列值。我不想为所有列重复相同的 CASE
语句。因此,我使用一个单独的 CASE
将值与分隔符连接起来,然后解析该连接字符串并将适当的值放在其位置。