sql使用select case值插入表中

时间:2013-04-04 19:49:36

标签: sql sql-server-2008

出于某种原因,我在使用此声明时遇到了问题

Insert into TblStuff
(FullName,Address,City,Zip)

Select
Case
When Middle is Null Then Fname + LName as FullName,
Else Fname +' ' + Middle + ' '+ Lname as FullName,
End
Case
When Address2 is Null Then Address1 as Address,
else Address1 +', ' + Address2 as  Address,
End
City as City,
Zip as Zip
from tblImport

我在关键字'as'

附近得到错误的语法

编辑添加到此问题,如果我需要添加新帖子,请告诉我。

我知道以下几点不同,但你能否做出与下面类似的案例陈述?以下声明是否有意义?

Insert into TblStuff
    (NickName,FirstName,MiddleName,Suffix)

    Case when FirstName IS NULL then 
        NickName as Nickname,
        IsNULL(FirstName,'''') as FirstName,
        IsNULL(MiddelName,'''') as MiddleName,
        IsNULL(NameSuffix,'''') as Suffix,
    Else
        IsNull(NickName2,'''') as NickName,
        IsNULL(FirstName,'''') as FirstName,
        IsNULL(MiddelName,'''') as Middlename,
        Case when NameSuffix2 is NULL then
            IsNULL(NameSuffix,'''')as suffix,
        Else
            IsNULL(NameSuffix,'''') + '''', '''' + IsNULL(NameSuffix2,'''') as suffix,
        End
    End
From tblImport

4 个答案:

答案 0 :(得分:14)

end完成案例陈述后,您需要逗号。并且,“as”在case语句之后,而不在其中:

Insert into TblStuff(FullName, Address, City, Zip)
    Select (Case When Middle is Null Then Fname + LName
                 Else Fname +' ' + Middle + ' '+ Lname
            End)  as FullName,
           (Case When Address2 is Null Then Address1
                 else Address1 +', ' + Address2
            End)  as  Address,
           City as City,
           Zip as Zip
    from tblImport

答案 1 :(得分:8)

你在案例中有别名,它必须在END之外:

Insert into TblStuff (FullName,Address,City,Zip)
Select
  Case
    When Middle is Null 
    Then Fname + LName
    Else Fname +' ' + Middle + ' '+ Lname
  End as FullName,
  Case
    When Address2 is Null Then Address1
    else Address1 +', ' + Address2 
  End as  Address,
  City as City,
  Zip as Zip
from tblImport

答案 2 :(得分:2)

您也可以使用COALESCE代替CASE表达式。因为将任何内容连接到NULL的结果,甚至本身,总是NULL

INSERT TblStuff(FullName,Address,City,Zip)
SELECT COALESCE(Fname + ' ' + Middle + ' ' + Lname, Fname + LName) AS FullName,
       COALESCE(Address1 + ', ' + Address2, Address1) AS Address, City, Zip
FROM tblImport

SQLFiddle上的演示

答案 3 :(得分:0)

如果FName和LName包含NULL值,那么您将需要特殊处理以避免不必要的额外前置,尾随和中间空格。此外,如果Address1包含NULL值,那么您需要进行特殊处理以防止在地址字符串的开头添加不必要的','。

如果您使用的是SQL Server 2012,则可以使用CONCAT(NULL会自动视为空字符串)和IIF:

INSERT INTO TblStuff (FullName, Address, City, Zip)
SELECT FullName = REPLACE(RTRIM(LTRIM(CONCAT(FName, ' ', Middle, ' ', LName))), '  ', ' ')
    , Address = CONCAT(Address1, IIF(Address2 IS NOT NULL, CONCAT(', ', Address2), ''))
    , City
    , Zip
FROM tblImport (NOLOCK);

否则,这将起作用:

INSERT INTO TblStuff (FullName, Address, City, Zip)
SELECT FullName = REPLACE(RTRIM(LTRIM(ISNULL(FName, '') + ' ' + ISNULL(Middle, '') + ' ' + ISNULL(LName, ''))), '  ', ' ')
    , Address = ISNULL(Address1, '') + CASE
        WHEN Address2 IS NOT NULL THEN ', ' + Address2
        ELSE '' END
    , City
    , Zip
FROM tblImport (NOLOCK);