SQL - 使用with子句,获取错误

时间:2013-05-01 07:54:34

标签: sql sql-server sql-server-2008

我在MSSQL 2008中有以下sql语句。

use gasnominations

INSERT INTO dbo.GasData (readDate,TagName,Value, amendedValue)

with emptce as
(SELECT timestamp AS Interval, Left(Right(TagName,Len(TagName)-5),Len(TagName)-10) as TagName,
CONVERT(decimal(10, 3), ROUND(value, 3)) As Value
FROM 
   OPENQUERY(IHISTORIAN,'
                SET starttime =''yesterday +6h'', endtime =''today +6h''  
                SELECT timestamp, tagname, value
                FROM ihRawData
                WHERE tagname = "UMIS.99FC9051.F_CV"
                   OR tagname = "UMIS.99F851C.F_CV"
                   OR tagname = "UMIS.35GTGAS.F_CV"
                   OR tagname = "UMIS.35HRSGGAS.F_CV"
       OR tagname = "UMIS.99XXG546.F_CV"
       OR tagname = "UMIS.99XXG547.F_CV"
       OR tagname = "UMIS.99F9082.F_CV"
       OR tagname = "UMIS.99FC20107.F_CV"
       OR tagname = "UMIS.95FIQ5043.F_CV"
       OR tagname = "UMIS.99PBGAS.F_CV"
       OR tagname = "UMIS.99FE1100.F_CV"
       OR tagname = "UMIS.99FE1200.F_CV"
       OR tagname = "UMIS.99FC8279.F_CV"
       OR tagname = "UMIS.35FI8316.F_CV"
                AND timestamp BETWEEN ''timestamp'' and ''timestamp'' 
                AND SamplingMode =Calculated
                AND CalculationMode =Average
                AND IntervalMilliseconds =1h
                ORDER BY tagname, timestamp'))
                select emptce.Interval, emptce.TagName, emptce.Value, gasdata.amendedValue  from emptce inner join gasdata on emptce.TagName = gasData.tagName COLLATE DATABASE_DEFAULT
and emptce.Interval = DATEADD(DAY, 1, readDate)

我收到以下错误'如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则必须以分号结束前一个语句。'

我尝试在with之前添加一个半冒号,但它只是在;

上的错误

3 个答案:

答案 0 :(得分:1)

使用分号终止上一个语句,然后在WITH之前使用INSERT

use gasnominations;

with emptce as (...)
INSERT INTO dbo.GasData (readDate,TagName,Value, amendedValue)
select emptce.Interval, emptce.TagName, emptce.Value, gasdata.amendedValue  from emptce inner join gasdata on emptce.TagName = gasData.tagName COLLATE DATABASE_DEFAULT
and emptce.Interval = DATEADD(DAY, 1, readDate)

答案 1 :(得分:0)

使用多个语句时,您会发现USE语句需要跟随GO

use gasnominations
GO

INSERT INTO dbo.GasData (readDate,TagName,Value, amendedValue);

WITH ....

答案 2 :(得分:-1)

试试这个

with emptce as
(SELECT timestamp AS Interval, Left(Right(TagName,Len(TagName)-5),Len(TagName)-10) as TagName,
CONVERT(decimal(10, 3), ROUND(value, 3)) As Value
FROM 
   OPENQUERY(IHISTORIAN,'
                SET starttime =''yesterday +6h'', endtime =''today +6h''  
                SELECT timestamp, tagname, value
                FROM ihRawData
                WHERE tagname = "UMIS.99FC9051.F_CV"
                   OR tagname = "UMIS.99F851C.F_CV"
                   OR tagname = "UMIS.35GTGAS.F_CV"
                   OR tagname = "UMIS.35HRSGGAS.F_CV"
       OR tagname = "UMIS.99XXG546.F_CV"
       OR tagname = "UMIS.99XXG547.F_CV"
       OR tagname = "UMIS.99F9082.F_CV"
       OR tagname = "UMIS.99FC20107.F_CV"
       OR tagname = "UMIS.95FIQ5043.F_CV"
       OR tagname = "UMIS.99PBGAS.F_CV"
       OR tagname = "UMIS.99FE1100.F_CV"
       OR tagname = "UMIS.99FE1200.F_CV"
       OR tagname = "UMIS.99FC8279.F_CV"
       OR tagname = "UMIS.35FI8316.F_CV"
                AND timestamp BETWEEN ''timestamp'' and ''timestamp'' 
                AND SamplingMode =Calculated
                AND CalculationMode =Average
                AND IntervalMilliseconds =1h
                ORDER BY tagname, timestamp'))
                INSERT INTO dbo.GasData (readDate,TagName,Value, amendedValue)
                select emptce.Interval, emptce.TagName, emptce.Value, gasdata.amendedValue                  
                from emptce inner join gasdata on emptce.TagName = gasData.tagName COLLATE DATABASE_DEFAULT
and emptce.Interval = DATEADD(DAY, 1, readDate)