FROM附近的SQL查询语法错误

时间:2014-04-23 18:46:17

标签: tsql sql-server-2005 syntax-error

在第65和95行获取语法错误:

Use PARCS_DB
GO
TRUNCATE TABLE tbl_Utilities_PARCS_LiveValue
TRUNCATE TABLE tbl_Utilities_PARCS_Totals
TRUNCATE TABLE tbl_Utilities_PARCS_YTD
TRUNCATE TABLE tbl_Utilities_CUB_LiveValue
TRUNCATE TABLE tbl_Utilities_CUB_Totals
TRUNCATE TABLE tbl_Utilities_CUB_YTD
TRUNCATE TABLE tbl_Utilities_BLDG1_LiveValue
TRUNCATE TABLE tbl_Utilities_BLDG1_Totals
TRUNCATE TABLE tbl_Utilities_BLDG1_YTD
GO
DECLARE @Year INT;
SET @Year = 2014;

-- CONVERT TO A DATE TO ALLOW A SARGEABLE PREDICATE IN THE WHERE CLAUSE
DECLARE @Date SMALLDATETIME;
SET @Date = CONVERT(SMALLDATETIME, CONVERT(CHAR(4), @Year), 112);

-- GET PF OLDTOTAL VALUES FROM HISTORY. 
INSERT INTO PARCS_DB.dbo.tbl_Utilities_PARCS_Totals
    (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
SELECT  
        Jan = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 2 THEN value END),
        Feb = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 3 THEN value END),
        Mar = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 4 THEN value END),
        Apr = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 5 THEN value END),
        May = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 6 THEN value END),
        Jun = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 7 THEN value END),
        Jul = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 8 THEN value END),
        Aug = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 9 THEN value END),
        Sep = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 10 THEN value END),
        Oct = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 11 THEN value END),
        Nov = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 12 THEN value END),
        Dec = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 1 AND DATEPART(YEAR,DateTime) = DATEADD(YEAR, 1, @Date)  THEN value END)
FROM    runtime.dbo.History
WHERE   Tagname IN ('FQI_PA_B_001.OldTotal','FQI_PS_B_001.OldTotal','FQI_CW_B_002.OldTotal','FQI_NCW_B_001.OldTotal','FQI_NCW_B_002.OldTotal')
AND     wwVersion = 'Latest'
AND     DateTime >= @Date
AND     DateTime < DATEADD(YEAR, 1, @Date)
GROUP BY TagName;

-- GET PF LIVE VALUES. 
INSERT INTO PARCS_DB.dbo.tbl_Utilities_PARCS_LiveValue
    (Value)
SELECT  Value
FROM    runtime.dbo.v_live
WHERE   Tagname IN ('FQI_CW_B_002.Total','FQI_NCW_B_001.Total','FQI_NCW_B_002.Total','FQI_PA_B_001.Total','FQI_PS_B_001.Total')

-- GET CUB OLDTOTAL VALUES FROM HISTORY. 
INSERT INTO PARCS_DB.dbo.tbl_Utilities_CUB_Totals 
    (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
SELECT  
        Jan = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 2 THEN value END),
        Feb = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 3 THEN value END),
        Mar = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 4 THEN value END),
        Apr = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 5 THEN value END),
        May = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 6 THEN value END),
        Jun = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 7 THEN value END),
        Jul = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 8 THEN value END),
        Aug = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 9 THEN value END),
        Sep = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 10 THEN value END),
        Oct = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 11 THEN value END),
        Nov = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 12 THEN value END),
        Dec = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 1 AND DATEPART(YEAR,DateTime) = DATEADD(YEAR, 1, @Date)  THEN value END)
FROM    RTPCUBHIST_001.runtime.dbo.History
WHERE   Tagname IN ('FQI_DW_A_01.OldTotal','BOA001_STM_TOTAL.OldTotal','BOA002_STM_TOTAL.OldTotal')
AND     wwVersion = 'Latest'
AND     DateTime >= @Date
AND     DateTime < DATEADD(YEAR, 1, @Date)
GROUP BY TagName;

-- GET CUB LIVE VALUES.
INSERT INTO PARCS_DB.dbo.tbl_Utilities_CUB_LiveValue  
    (Value)
SELECT
FROM    RTPCUBHIST_001.runtime.dbo.v_live
WHERE   Tagname IN ('BOA001_STM_TOTAL.Total','BOA002_STM_TOTAL.Total','FQI_DW_A_01.Total')

-- GET BLDG1 OLDTOTAL VALUES FROM HISTORY. 
INSERT INTO PARCS_DB.dbo.tbl_Utilities_BLDG1_Totals 
    (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
SELECT
        Jan = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 2 THEN value END),
        Feb = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 3 THEN value END),
        Mar = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 4 THEN value END),
        Apr = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 5 THEN value END),
        May = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 6 THEN value END),
        Jun = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 7 THEN value END),
        Jul = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 8 THEN value END),
        Aug = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 9 THEN value END),
        Sep = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 10 THEN value END),
        Oct = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 11 THEN value END),
        Nov = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 12 THEN value END),
        Dec = MAX(CASE WHEN DATEPART(MONTH, DateTime) = 1 AND DATEPART(YEAR,DateTime) = DATEADD(YEAR, 1, @Date)  THEN value END)
FROM    RTPBASHIST01.runtime.dbo.History
WHERE   Tagname IN ('PROC_AIR_TOTAL.OldTotal','PLNT_STM_TOTAL.OldTotal','DOM_WTR_TOTAL.OldTotal','SAN_SEW_TOTAL.OldTotal')
AND     wwVersion = 'Latest'
AND     DateTime >= @Date
AND     DateTime < DATEADD(YEAR, 1, @Date)
GROUP BY TagName;

-- GET BLDG1 LIVE VALUES
INSERT INTO PARCS_DB.dbo.tbl_Utilities_BLDG1_LiveValue  
    (Value)
SELECT
FROM    RTPBASHIST01.runtime.dbo.v_live
WHERE   Tagname IN ('DOM_WTR_TOTAL.Total','PLNT_STM_TOTAL.Total','PROC_AIR_TOTAL.Total','SAN_SEW_TOTAL.Total')

使用FROM语句的第一个实例工作正常,但之后的复制语句会抛出错误。唯一不同的是,对于两个错误输出我引用了一个链接服务器。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

-- GET CUB LIVE VALUES.
INSERT INTO PARCS_DB.dbo.tbl_Utilities_CUB_LiveValue  
    (Value)
SELECT  --NEED TO PUT A COLUMN NAME HERE
FROM    RTPCUBHIST_001.runtime.dbo.v_live
WHERE   Tagname IN ('BOA001_STM_TOTAL.Total','BOA002_STM_TOTAL.Total','FQI_DW_A_01.Total')

您没有在select子句中列出列。

您在评论附近的代码块末尾也遇到同样的问题 - 获取BLDG1 LIVE VALUES