我在插入语句中一直收到有关逗号的错误。知道为什么会这样。
以下是错误消息:
Msg 102,Level 15,State 1,Line 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 :(得分: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
不是你想要的,你能告诉我表flag
与customer
和dupaddr
的关系吗?
击>
答案 1 :(得分:0)
正如我评论的那样 - 某种程度上你的代码有点令人困惑......目前尚不清楚这三个表customer
,dupaddr
和flag
是如何相关的 - 它们应该是正确的已加入,以便您可以从单个cust_no
语句中抓取Flag
和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???
答案 2 :(得分: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