我无法使用由错误导致的此查询将新记录插入到表中

时间:2014-02-11 07:05:10

标签: sql sql-server

我的目标是仅插入新记录来更新我刚刚创建的表,但它给出了一条错误消息102,如下所示。当我突出显示或单击SSMS中错误消息区域上的错误消息时。它突出显示脚本的 ss.sitedesc = b.sitedesc 部分。我没有看到它有什么问题。有人可以帮帮我吗?谢谢。

Msg 102,Level 15,State 1,Line 39 'ss'附近的语法不正确。

enter image description here

目前这是我的整个剧本。

insert into SEC_ODS.dbo.ODSCustomerBase    
select 
--Identity as CustomerBaseID
'E4SE' as Recordsource, 
'C' as RecordType, 
ss.siteURN,
ss.sitedesc,
ss.FS_URL + 'frmcustomer.aspx?CustomerID=' + customerid as CustomerLink,
co.HomeCurrencyCode, 
c.customerid, 
c.currencycode as customercurrencycode,
c.entityname as CustomerName, 
c.entityshortname CustomerShortName, 
c.address, 
c.city, 
c.postalcode, 
c.countrycode, 
cn.countryname, 
c.statecode,
s.statename, 
c.phone,
c.fax, 
c.taxcode, 
c.prospectid,
c.createdate, 
c.lastupdatedate 
from country cn, 
SECSite ss, 
company co, 
customer c 
left outer join state s 
on c.statecode = s.statecode
where  ss.LocalSiteFlag = 1
and cn.countrycode = c.countrycode

and not exists(select * from SEC_ODS.dbo.ODSCustomerBase b
                where(ss.siteURN=b.siteURN
                      ss.sitedesc=b.sitedesc
                      ss.FS_URL=b.FS_URL
                      co.HomeCurrencyCode=b.HomeCurrencyCode
                      c.customerid=b.customerid
                      c.currencycode=b.currencycode
                      c.entityname=b.entityname
                      c.entityshortname=b.entityshortname
                      c.address=b.address
                      c.city=b.city
                      c.postalcode=b.postalcode
                      c.countrycode=b.countrycode
                      cn.countryname=b.countryname
                      c.statecode=b.statecode
                      s.statename=b.statename
                      c.phone=b.phone
                      c.fax=b.fax
                      c.taxcode=b.taxcode
                      c.prospectid=b.prospectid
                      c.createdate=b.createdate
                      c.lastupdatedate=b.lastupdatedate))

但是当我把AND放在where条件下时。

它提示我使用以下无效列

时出错

enter image description here

这是我用来创建表的脚本。这可能有所帮助。

USE SEC_ODS
GO 
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[ODSCustomerBase]') AND type in (N'U'))
DROP TABLE [ODSCustomerBase]
Go
Create Table ODSCustomerBase
(CustomerBaseID             int NOT NULL identity primary key,
RecordSource            nvarchar(4),
RecordType              varchar(2),
SiteURN             nvarchar(128)NOT NULL,
SiteDesc                    nvarchar(60)NULL,
CustomerLink            nvarchar(120)NOT NULL,
HomeCurrencyCode            nvarchar(8)NOT NULL, 
CustomerID                  nvarchar(15)NOT NULL,
CustomerCurrencyCode        nvarchar(8)NOT NULL,
CustomerName            nvarchar(120)NULL,
CustomerShortName           nvarchar(30) NOT NULL,
Address             nvarchar(255)NULL,
City                    nvarchar(25)NULL,
PostalCode              nvarchar(10) NULL,
CountryCode             nvarchar(3) NOT NULL,
CountryName             nvarchar(60) NOT NULL,
StateCode               nvarchar(8) NULL,
StateName               nvarchar(60) NULL,
Phone                   nvarchar(30) NOT NULL,
Fax                 nvarchar(30) NULL,
TaxCode             nvarchar(15) NULL,
ProspectID              nvarchar(15) NULL,
CreateDate              datetime NOT NULL,
LastUpdateDate          datetime NULL)

3 个答案:

答案 0 :(得分:2)

语句之间应该有一个逻辑运算符。像这样:

... ss.siteURN=b.siteURN
                  ss.sitedesc=b.sitedesc AND
                  ss.FS_URL=b.FS_URL AND
                  co.HomeCurrencyCode=b.HomeCurrencyCode AND
                  c.customerid=b.customerid  ...

答案 1 :(得分:0)

在where子句中,您可能会错过AND关键字

where(ss.siteURN=b.siteURN
                AND  ss.sitedesc=b.sitedesc
                AND  ss.FS_URL=b.FS_URL

.....

答案 2 :(得分:0)

请阅读插入查询的syntex,如: 插入tablename(columname1,columnname2,..)value(value1,valkue2,..)

因此,您的查询如下所示,您需要根据表列名称更改列名:

insert into SEC_ODS.dbo.ODSCustomerBase(columnname1, columnname2, ...)    
select 
--Identity as CustomerBaseID
'E4SE' as Recordsource, 
'C' as RecordType, 
ss.siteURN,
ss.sitedesc,
ss.FS_URL + 'frmcustomer.aspx?CustomerID=' + customerid as CustomerLink,
co.HomeCurrencyCode, 
c.customerid, 
c.currencycode as customercurrencycode,
c.entityname as CustomerName, 
c.entityshortname CustomerShortName, 
c.address, 
c.city, 
c.postalcode, 
c.countrycode, 
cn.countryname, 
c.statecode,
s.statename, 
c.phone,
c.fax, 
c.taxcode, 
c.prospectid,
c.createdate, 
c.lastupdatedate 
from country cn, 
SECSite ss, 
company co, 
customer c 
left outer join state s 
on c.statecode = s.statecode
where  ss.LocalSiteFlag = 1
and cn.countrycode = c.countrycode
and not exists(select columnaname3 from SEC_ODS.dbo.ODSCustomerBase b
                where(ss.siteURN=b.siteURN  and
                      ss.sitedesc=b.sitedesc  and
                      ss.FS_URL=b.FS_URL  and
                      co.HomeCurrencyCode=b.HomeCurrencyCode  and
                      c.customerid=b.customerid  and
                      c.currencycode=b.currencycode  and
                      c.entityname=b.entityname  and
                      c.entityshortname=b.entityshortname  and
                      c.address=b.address  and
                      c.city=b.city  and
                      c.postalcode=b.postalcode  and
                      c.countrycode=b.countrycode  and
                      cn.countryname=b.countryname  and
                      c.statecode=b.statecode  and
                      s.statename=b.statename  and
                      c.phone=b.phone  and
                      c.fax=b.fax  and
                      c.taxcode=b.taxcode  and
                      c.prospectid=b.prospectid  and
                      c.createdate=b.createdate  and
                      c.lastupdatedate=b.lastupdatedate))