我尝试在计算列中使用CASE语句有什么问题?

时间:2017-08-01 05:17:11

标签: sql-server case

我正在尝试创建一个基于State找到Region的计算列。

这就是我所拥有的:

CREATE FUNCTION fnDonorRegion (@Region VARCHAR(10))
RETURNS VARCHAR
AS
BEGIN
    RETURN
        CASE
           WHEN DONOR.[State] IN ('Connecticut', 'Maine', 'Massachusetts', 'New Hampshire', 'Rhode Island', 'Vermont', 'New Jersey', 'New York', 'Pennsylvania') 
              THEN 'Northeast'
           WHEN DONOR.[State] IN ('Illinois', 'Indiana', 'Michigan', 'Ohio', 'Wisconsin', 'Iowa', 'Kansas', 'Minnesota', 'Missouri', 'Nebraska', 'North Dakota', 'South Dakota') 
              THEN 'Midwest'
           WHEN DONOR.[State] IN ('Delaware', 'Florida', 'Georgia', 'Maryland', 'North Carolina', 'South Carolina', 'Virginia', 'District of Columbia', 'West Virginia', 'Alabama', 'Kentucky', 'Mississippi', 'Tennessee', 'Arkansas', 'Louisiana', 'Oklahoma', 'Texas') 
              THEN 'South'
           ELSE 'West'
        END
END

ALTER TABLE tblDONOR
ADD Region AS fnDonorRegion

我一直收到错误:

  

Msg 4104,Level 16,State 1,Procedure fnDonorRegion,Line 12
  无法绑定多部分标识符“D.State”。

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

您应该检查参数而不是表格。因此,让我们假设参数是状态。

CREATE FUNCTION fnDonorRegion (@state varchar(10))
RETURNS varchar
AS
BEGIN
  RETURN
  CASE
    WHEN @state  IN ('Connecticut', 'Maine', 'Massachusetts', 'New Hampshire', 'Rhode
        Island', 'Vermont', 'New Jersey', 'New York', 'Pennsylvania') THEN 'Northeast'
    WHEN @state   IN ('Illinois', 'Indiana', 'Michigan', 'Ohio', 'Wisconsin',
      'Iowa', 'Kansas', 'Minnesota', 'Missouri', 'Nebraska', 'North Dakota', 'South Dakota') THEN 'Midwest'
    WHEN @state   IN ('Delaware', 'Florida', 'Georgia', 'Maryland', 'North Carolina',
      'South Carolina', 'Virginia', 'District of Columbia', 'West Virginia', 'Alabama', 'Kentucky',
      'Mississippi', 'Tennessee', 'Arkansas', 'Louisiana', 'Oklahoma', 'Texas') THEN 'South'
    ELSE 'West'
  END
END

ALTER TABLE tblDONOR
ADD Region AS fnDonorRegion([state]) -- assume your table has a column named state

答案 1 :(得分:1)

您应该在CASE语句中使用输入变量。

Create FUNCTION fnDonorRegion (@Region varchar(50))
RETURNS varchar(10)
AS
BEGIN
  RETURN
  CASE
    WHEN @Region IN ('Connecticut', 'Maine', 'Massachusetts', 'New Hampshire', 'Rhode
        Island', 'Vermont', 'New Jersey', 'New York', 'Pennsylvania') THEN 'Northeast'
    WHEN @Region IN ('Illinois', 'Indiana', 'Michigan', 'Ohio', 'Wisconsin',
      'Iowa', 'Kansas', 'Minnesota', 'Missouri', 'Nebraska', 'North Dakota', 'South Dakota') THEN 'Midwest'
    WHEN @Region IN ('Delaware', 'Florida', 'Georgia', 'Maryland', 'North Carolina',
      'South Carolina', 'Virginia', 'District of Columbia', 'West Virginia', 'Alabama', 'Kentucky',
      'Mississippi', 'Tennessee', 'Arkansas', 'Louisiana', 'Oklahoma', 'Texas') THEN 'South'
    ELSE 'West'
  ENd
ENd

select dbo.fnDonorRegion('Mississippi')

答案 2 :(得分:0)

试试这个:

CREATE FUNCTION dbo.fnDonorRegion
    (@Region VARCHAR(100))
RETURNS VARCHAR(100)
AS
BEGIN
    DECLARE @Result  VARCHAR(100)

    SELECT 
        @Result= CASE
                    WHEN DONOR.[State] IN (@Region)
                       THEN 'Northeast'
                    WHEN DONOR.[State] IN(@Region)
                       THEN 'Midwest'                  
                    ELSE 'West'
                 END 
    FROM tblDONOR AS DONOR

    RETURN @Result  
 END;

SELECT dbo.fnDonorRegion('Connecticut')