如何修复SQL Server错误“多部分标识符'X.Y'无法绑定”?

时间:2014-08-13 08:58:27

标签: sql sql-server tsql

我一直在努力开发税务计算器。我正在尝试在SQL中进行计算,但对它不好,所以请耐心等待。

IF (12500 > WTAX_DAILY.HI_LIMIT)
(
    SELECT (HI_LIMIT) "DEDUCTION"
    FROM
    (
        SELECT HI_LIMIT, EXEMPTION  FROM WTAX_MONTHLY WHERE LO_LIMIT <= 12500 AND HI_LIMIT >= 12500
    )T
)
ELSE IF (12500 < WTAX_DAILY.HI_LIMIT)
(
    SELECT (LO_LIMIT) "DEDUCTION"
    FROM
    (
        SELECT LO_LIMIT, EXEMPTION  FROM WTAX_MONTHLY WHERE LO_LIMIT <= 12500 AND HI_LIMIT >= 12500
    )T
)

这是我得到的错误:

[Err] 42000 - [SQL Server]The multi-part identifier "WTAX_DAILY.HI_LIMIT" could not be bound.
42000 - [SQL Server]The multi-part identifier "WTAX_DAILY.HI_LIMIT" could not be bound.

3 个答案:

答案 0 :(得分:1)

试试这个:(删除THEN FROM IF声明)

IF EXISTS(SELECT 1 FROM WTAX_MONTHLY WHERE HI_LIMIT < 12500)
    SELECT (HI_LIMIT) "DEDUCTION"
    FROM
        (SELECT HI_LIMIT, EXEMPTION
         FROM   WTAX_MONTHLY
         WHERE  LO_LIMIT <= 12500 AND HI_LIMIT >= 12500
        ) AS T
ELSE IF EXISTS(SELECT 1 FROM WTAX_MONTHLY WHERE HI_LIMIT > 12500) 
    SELECT (LO_LIMIT) "DEDUCTION"
    FROM
        (SELECT LO_LIMIT, EXEMPTION 
         FROM   WTAX_MONTHLY
         WHERE  LO_LIMIT <= 12500 AND HI_LIMIT >= 12500
        ) AS T

<强>更新

SELECT CASE 
           WHEN HI_LIMIT > 12500 THEN 
                HI_LIMIT 
           ELSE LO_LIMIT 
       END AS DEDUCTION
FROM   WTAX_MONTHLY

答案 1 :(得分:0)

你得到错误的原因是因为你试图在IF..ELSE语句中使用列名,你不能这样做...... IF..ELSE只能比较标量值。硬编码或变量但只有单个值。您需要在Select语句中使用CASE语句。见下文:

SELECT CASE
           WHEN 12500 > HI_LIMIT 
             THEN HI_LIMIT 
            ELSE LO_LIMIT END AS  [DEDUCTION]
FROM  WTAX_MONTHLY
--WHERE LO_LIMIT <= 12500 
--  AND HI_LIMIT >= 12500

答案 2 :(得分:0)

关于Jesuraja,我刚刚修改了他的答案,这就是出现的情况。现在它完美运行。谢谢Jesu

IF EXISTS(SELECT 1 FROM WTAX_MONTHLY WHERE HI_LIMIT < 15850)
SELECT (LO_LIMIT) "DEDUCTION"
FROM
    (SELECT LO_LIMIT, EXEMPTION
     FROM   WTAX_MONTHLY
     WHERE  LO_LIMIT <= 15850 AND HI_LIMIT >= 15850
    ) AS T
ELSE IF EXISTS(SELECT 1 FROM WTAX_MONTHLY WHERE HI_LIMIT > 15850) 
SELECT (HI_LIMIT) "DEDUCTION"
FROM
    (SELECT HI_LIMIT, EXEMPTION 
     FROM   WTAX_MONTHLY
     WHERE  LO_LIMIT <= 15850 AND HI_LIMIT >= 15850
    ) AS T