这是其他人的代码。我试图让它工作,但不知道什么是错的。

时间:2018-04-12 13:48:15

标签: syntax

有人可以帮我摆脱这个错误吗?

第15行,第15行,第1行,第12行 关键字' INTO'附近的语法不正确。 Msg 102,Level 15,State 1,Line 24 ' LocationID'附近的语法不正确。

use DB1

-- Declare variables
declare @AnalysisSID varchar(10)
declare @ExposureDB varchar(250)
declare @AnalysisName varchar(250)

-- !! begin user input !! -------------------------------------------------------------------
set @AnalysisSID =9
set @ExposureDB = (select ExposureDataSourceName
                  from t9_LOSS_DimExposureDataSource) --<--!!! update result table number

-- !! end user input !! ----------------------------------------------------------------------
set @AnalysisName = (select AnalysisName from tAnalysisResult where ResultSID = @AnalysisSID)

Exec(
'
if not (exists (select * from sysobjects where name = ''LOSSES_FORFEED''))
begin
CREATE TABLE LOSSES_FORFEED (
Name varchar(250) NOT NULL,
LOCID varchar(100) NOT NULL,
ContractID varchar(100) NOT NULL,
GroundUp_Loss float,
Gross_Loss float)
end

INTO LOSSES_FORFEED
SELECT  
'''+@AnalysisSID+''' as AnalysisID, '''+@AnalysisName+''' as Name,
l.LocationID as LOCID,
c.ContractID,
sum(GroundUpLoss) as GroundUp_Loss, 
sum(GrossLoss) as Gross_Loss
FROM t'+@AnalysisSID+'_LOSS_ByLocationSummary loss
join '+@ExposureDB+'..tLocation l on loss.LocationSID = l.LocationSID
join '+@ExposureDB+'..tContract c on l.ContractSID = c.ContractSID
group by 
c.ContractID,
l.LocationID
')

1 个答案:

答案 0 :(得分:0)

您是否尝试在INSERT之前添加INTO LOSSES_FORFEED

INSERT INTO LOSSES_FORFEED

这应该有效 - 至少对于MS SQl服务器数据库而言。但是参数的数量也不匹配:表LOSSES_FORFEED有5列,但在select语句中你提供了6个参数。