2013-04-09 59 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