如何使用SQL Server中的三列填充字段

时间:2018-03-10 10:20:38

标签: sql sql-server-2012

我希望在SQL Server 2012中构建一个函数。我想根据国家,城市和办公室的三个列顺序填充Country。例如,如果代码查找国家/地区可用,请将country设置为正确的情况。如果国家是空白或null去市,找那个城市的国家,把国家。如果国家和城市都为空或空白,则前往办公室并计算国家/地区。我使用下面的代码,但它无法正常工作。

黄色输出:

enter image description here

代码:

ALTER FUNCTION [dbo].[CONTRACT_COUNTRY_vk]
    (@COUNTRY NVARCHAR(30),
     @CITY NVARCHAR(60),
     @OFFICE NVARCHAR(60))
RETURNS NVARCHAR(30)
AS
BEGIN
    DECLARE @CONTRACT_COUNTRY NVARCHAR(30)
    DECLARE @CITY NVARCHAR(60)
    DECLARE @OFF NVARCHAR(60)

    IF (@COUNTRY = 'United Kingdom' )
       SET @COUNTRY = 'UK'
    ELSE IF (@COUNTRY = 'GERMANY' )
       SET @COUNTRY = 'Germany'
    ELSE IF (@COUNTRY = 'SWITZERLAND' )
       SET @COUNTRY = 'Switzerland'
    ELSE IF (@COUNTRY IN ('ABC', 'DEFAULT ORGANIZATION'))
       SET @COUNTRY ='Unknown'
    ELSE 
       SET @COUNTRY = @COUNTRY

    SET @CONTRACT_COUNTRY = @COUNTRY

If (@COUNTRY IS NULL OR @COUNTRY = 'Blank') 
begin
--SET @CITY = @CITY 
SET @CONTRACT_COUNTRY = 
(case WHEN ( @CITY = 'AMSTERDAM' ) THEN 'Netherlands'
WHEN ( @CITY = 'HANNOVER' ) THEN 'Germany'
WHEN ( @CITY = 'Hanover' ) THEN 'Germany'
ELSE 'UNKNOWN' END)

If (@COUNTRY = 'Blank' or @COUNTRY IS NULL ) and (@CITY = 'Blank' or @CITY 
IS NULL)
begin
SET @OFF = @OFFICE 
SET @CONTRACT_COUNTRY = 
(CASE   
WHEN ( @OFF = 'Viena') THEN 'Austria'
WHEN ( @OFF = 'LYON') THEN 'France'
ELSE 'UNKNOWN'
END)
END
END
RETURN(@CONTRACT_COUNTRY)
END

此代码计算不正确。

1 个答案:

答案 0 :(得分:0)

我修改了你拥有的东西并让它发挥作用。您不需要在函数内声明城市和办公室变量。这些值已经传入。您可以使用它们来确定@CONTRACT_COUNTRY

ALTER FUNCTION [dbo].[CONTRACT_COUNTRY_vk]
    (@COUNTRY NVARCHAR(30),
     @CITY NVARCHAR(60),
     @OFFICE NVARCHAR(60))
RETURNS NVARCHAR(30)
AS
BEGIN
    DECLARE @CONTRACT_COUNTRY NVARCHAR(30)
    --Check to see if @COUNTRY is null or 'blank'
    --If not, determine @CONTRACT_COUNTRY based on 
    --CASE statement using @COUNTRY value
    IF (@COUNTRY IS NOT NULL OR @COUNTRY <> 'Blank') 
    BEGIN
        SET @CONTRACT_COUNTRY = (CASE WHEN @COUNTRY = 'United Kingdom' THEN 'UK'
                                WHEN  @COUNTRY = 'GERMANY' THEN 'Germany'
                                WHEN  @COUNTRY = 'SWITZERLAND'  THEN 'Switzerland'
                                ELSE 'UNKNOWN' END)
    END
    --When @COUNTRY is null or 'blank', then use this logic
    IF (@COUNTRY IS NULL OR @COUNTRY = 'Blank') 
    BEGIN
        --Check to see if @CITY is null
        --If so, determine @CONTRACT_COUNTRY based on 
        --CASE statement using @OFFICE value
        IF (@CITY IS NULL OR @CITY = 'Blank')
        BEGIN
            SET @CONTRACT_COUNTRY = (CASE WHEN @OFFICE = 'Vienna' THEN 'Austria'
                                    WHEN  @OFFICE = 'HANNOVER' THEN 'Germany'
                                    WHEN  @OFFICE = 'LYON'  THEN 'France'
                                    ELSE 'UNKNOWN' END)
        END
        --When @CITY is NOT null, then use this logic
        --Determine @CONTRACT_COUNTRY based on 
        --CASE statement using @CITY value
        ELSE
        BEGIN
            SET @CONTRACT_COUNTRY = (CASE WHEN @CITY = 'AMSTERDAM' THEN 'Netherlands'
                                    WHEN  @CITY = 'HANNOVER' THEN 'Germany'
                                    WHEN  @CITY = 'Hannover'  THEN 'Germany'
                                    ELSE 'UNKNOWN' END)
        END
    END    
    RETURN(@CONTRACT_COUNTRY)
END

这是我用来测试你提供的场景的方法。

SELECT dbo.CONTRACT_COUNTRY_vk('United Kingdom' ,'London','London') 'Contract_Country'
UNION ALL
SELECT dbo.CONTRACT_COUNTRY_vk('GERMANY' ,'Berlin','Blank')
UNION ALL
SELECT dbo.CONTRACT_COUNTRY_vk('Switzerland' ,'Berne','Blank')
UNION ALL
SELECT dbo.CONTRACT_COUNTRY_vk('Blank' ,'Blank','LYON')
UNION ALL
SELECT dbo.CONTRACT_COUNTRY_vk('Blank' ,'HANNOVER','Blank')
UNION ALL
SELECT dbo.CONTRACT_COUNTRY_vk('Blank' ,'Blank','Blank')

结果

+-----------------+
|Contract_Country |
+-----------------+
|   UK            |
|   Germany       |
|   Switzerland   |
|   France        |
|   Germany       |
|   UNKNOWN       |
+-----------------+