TSQL案例语法

时间:2010-07-30 14:17:59

标签: tsql

您好我需要有关此代码语法的帮助:

IF OBJECT_ID('TEMPDB..#LTS_MAP') IS NOT NULL
BEGIN DROP TABLE #LTS_MAP END


SELECT
dtMicIssue as  LATE_CHARGE_FACTOR
,CASE   
WHEN (dtMicIssue is NOT NULL) AND (dtMicIssue  <> '1900-01-01 00:00:00') 
 THEN CONVERT(SMALLDATETIME,dtMicIssue)  
     ELSE NULL END 


INTO    #LTS_MAP
FROM    SBAURSQL001.LTSArchive.dbo.LoanMaster_0609
  

收到错误:消息8155,级别16,状态1,行5   没有为'#LTS_MAP'的第2列指定列。

尝试将案例功能与选择的dtMicIssue结合起来并弄乱语法......

4 个答案:

答案 0 :(得分:2)

正是它所说的

每个列都需要您正在创建的表#LTS_MAP中的名称...

SELECT
dtMicIssue as  LATE_CHARGE_FACTOR
,CASE   
WHEN (dtMicIssue is NOT NULL) AND (dtMicIssue  <> '1900-01-01 00:00:00') 
 THEN CONVERT(SMALLDATETIME,dtMicIssue)  
     ELSE NULL END  AS SomethingMeaningfulHere   --you're missing this: a column name!


INTO    #LTS_MAP
FROM    SBAURSQL001.LTSArchive.dbo.LoanMaster_0609

答案 1 :(得分:2)

由于您正在执行SELECT INTO,因此SQL Server需要知道每列的名称。您的第二列(具有CASE的列)没有明确的名称。决定你想要什么,然后把

AS ColumnName
在结束END

CASE之后立即

答案 2 :(得分:2)

试试这个

SELECT dtMicIssue as LATE_CHARGE_FACTOR,
   CASE WHEN (dtMicIssue is NOT NULL) AND 
          (dtMicIssue <> '1900-01-01 00:00:00') 
        THEN CONVERT(SMALLDATETIME,dtMicIssue)
        ELSE NULL END As SomeColumnName

INTO #LTS_MAP 
FROM SBAURSQL001.LTSArchive.dbo.LoanMaster_0609

答案 3 :(得分:1)

您需要为CASE语句设置别名,以使其具有列名:

IF OBJECT_ID('TEMPDB..#LTS_MAP') IS NOT NULL
BEGIN DROP TABLE #LTS_MAP END


SELECT
dtMicIssue as  LATE_CHARGE_FACTOR
,CASE   
WHEN (dtMicIssue is NOT NULL) AND (dtMicIssue  <> '1900-01-01 00:00:00') 
 THEN CONVERT(SMALLDATETIME,dtMicIssue)  
     ELSE NULL END AS DATE


INTO    #LTS_MAP
FROM    SBAURSQL001.LTSArchive.dbo.LoanMaster_0609