2013-04-09 62 views
1

我在我的insert语句中不断收到有关我的逗号的错误。任何想法,为什么这可能是。为什么我的插入语句出现错误?

以下是错误消息:

消息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 
+0

这是什么表'客户,dupaddr之间的关系,flag'? – 2013-04-09 15:43:49

+1

最后一个'SELECT CAST(Flag AS INT)'应该做什么?这是错误的部分..... – 2013-04-09 15:51:06

+0

您需要**以适当的方式连接**客户,'dupaddr'和'flag'表,以便您可以选择两个项目'cust_no'和'来自这些连接表的“flag” - 你不能在你使用的地方使用另一个* subquery * - 不起作用。 – 2013-04-09 15:56:54

回答

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是不是你想要的,你能告诉我怎么表flagcustomer相关和dupaddr

+0

标志表只保存custflagid,标志(数值)和cust_no。 dupadder是我创建的一个视图,用于获取具有不同cust_no但地址相同的所有客户的列表。 – BryansDriving 2013-04-09 16:08:43

+0

看到我更新的答案。 – 2013-04-09 16:11:27

+0

_标志表只保存custflagid,标志(数值)和cust_no。 dupadder是我创建的一个视图,用于获取具有不同cust_no但地址相同的所有客户的列表_ 对不起,此评论有误国旗表中没有cust_no列 – BryansDriving 2013-04-09 16:23:30

0

正如我评论 - 不知何故你的代码是一个有点混乱....目前尚不清楚这三个表customerdupaddrflag如何有关 - 他们应该得到适当的加入,让你可以抓住cust_noFlag从一个SELECT声明。

我看不出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??? 
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