1

我在插入语句中不断收到关于逗号的错误。关于为什么会这样的任何想法。

这是错误消息:

消息 102,级别 15,状态 1,第 3 行
',' 附近的语法不正确。

INSERT INTO...SELECT声明

insert into custflag (cust_no, flag)
  select 
      customer.cust_no 
  from 
      customer, dupaddr 
  where 
      customer.cust_no = dupaddr.cust_no, select cast(flag as int) 
                                           from flag 
                                           where flag_desc = 'Dup Customer'

这是我查询的完整代码。

SET IDENTITY_INSERT flag ON
insert into flag (flag,flag_desc,available)
values ((select Max(flag) from flag) + 1, 'Dup Customer', 1)

create view dupaddr as 
select distinct c1.cust_no, c1.firstname, c1.lastname, c1.company, c1.predir + ' ' + c1.streetno + ' ' + c1.streetnm + ' ' + c1.suffix + ' ' + c1.postdir as fff ,c1.address2
from customer c1,customer c2 
where c1.cust_no <> c2.cust_no
and c1.firstname = c2.firstname 
and c1.lastname = c2.lastname 
and c1.company = c2.company
and c1.predir + ' ' + c1.streetno + ' ' + c1.streetnm + ' ' + c1.suffix + ' ' +   c1.postdir = c2.predir + ' ' + c2.streetno + ' ' + c2.streetnm + ' ' + c2.suffix + ' ' + c2.postdir 
and c1.address2 = c2.address2



insert into custflag (cust_no,flag)
select customer.cust_no from customer, dupaddr where customer.cust_no = dupaddr.cust_no , select cast(flag as int) from flag where flag_desc = 'Dup Customer'

想通了,我将标志添加到视图中,并且能够简化插入语句。谢谢你们每一个人的帮助!

SET IDENTITY_INSERT flag ON
insert into flag (flag,flag_desc,available)
values ((select Max(flag) from flag) + 1, 'Dup Customer', 1)

create view dupaddr as 
select distinct c1.cust_no, 
c1.firstname, 
c1.lastname, 
c1.company, 
c1.predir + ' ' + c1.streetno + ' ' + c1.streetnm + ' ' + c1.suffix + ' ' +   c1.postdir as fff ,
c1.address2, 
(SELECT cast(flag as int) FROM flag  WHERE flag_desc = 'Dup Customer') as flag
from customer c1,customer c2 
where c1.cust_no <> c2.cust_no
and c1.firstname = c2.firstname 
and c1.lastname = c2.lastname 
and c1.company = c2.company
and c1.predir + ' ' + c1.streetno + ' ' + c1.streetnm + ' ' + c1.suffix + ' ' +  c1.postdir = c2.predir + ' ' + c2.streetno + ' ' + c2.streetnm + ' ' + c2.suffix + ' ' + c2.postdir 
and c1.address2 = c2.address2



insert into custflag (cust_no,flag)
select dupaddr.cust_no, dupaddr.flag from dupaddr
4

3 回答 3

0

你可以试试这个

insert into custflag (cust_no, flag)
  select 
      customer.cust_no, (select cast(flag as int) 
                                           from flag 
                                           where flag_desc = 'Dup Customer') 
  from 
      customer, dupaddr 
  where 
      customer.cust_no = dupaddr.cust_no
于 2013-04-09T16:02:54.440 回答
0

正如我所评论的 - 不知何故,你的代码有点令人困惑......不清楚这三个表customer是如何相关的dupaddr-flag它们应该正确连接,以便您可以从单个语句中获取cust_noand 。FlagSELECT

我根本看不出Flag与其他表格有什么关系——坦率地说,我不明白为什么你甚至需要dupaddr这里的表格......

所以尝试这样的事情:

insert into custflag (cust_no, flag)
  select 
      customer.cust_no,
      (select cast(flag as int) from flag where flag_desc = 'Dup Customer')
  from 
      customer
  inner join
      dupaddr on customer.cust_no = dupaddr.cust_no  -- why do you need this???

或者因为这flag似乎与任何被选择的行都没有关系,你也可以在你的声明之前抓住这个

declare @flag INT
select @flag = cast(flag as int) from flag where flag_desc = 'Dup Customer'

insert into custflag (cust_no, flag)
  select 
      customer.cust_no, @flag
  from 
      customer
  inner join
      dupaddr on customer.cust_no = dupaddr.cust_no  -- why do you need this???
于 2013-04-09T15:59:22.050 回答
0

为了让您查询工作,这是等效的代码,

INSERT  INTO custFlag(cust_no, flag)
SELECT  a.cust_no, c.flag
FROM    customer a
        INNER JOIN dupaddr b
            ON a.cust_no = b.cust_no
        INNER JOIN
        (
            SELECT  cust_no, cast(flag as int) flag
            FROM    flag 
            WHERE   flag_desc = 'Dup Customer'
        ) c ON a.cust_no = c.cust_no

但我怀疑CROSS JOIN不是你想要的,你能告诉我 tableflagcustomerand有什么关系dupaddr吗?

于 2013-04-09T15:46:52.037 回答