0

我在 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 之前添加一个分号,但它只是在 ; 上出现错误。

4

3 回答 3

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)
于 2013-05-01T08:21:46.410 回答
0

你会发现在使用多条语句的时候,USE语句后面需要跟一个GO

use gasnominations
GO

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

WITH ....
于 2013-05-01T08:15:20.073 回答
-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)
于 2013-05-01T08:29:45.503 回答