Sql 为什么insert语句会出错?
我的insert语句中的逗号不断出错。你知道为什么会这样吗 以下是错误消息: Msg 102,15级,状态1,第3行 “,”附近的语法不正确 以及INSERT INTO…SELECT语句Sql 为什么insert语句会出错?,sql,sql-server,sql-server-2008,sql-insert,Sql,Sql Server,Sql Server 2008,Sql Insert,我的insert语句中的逗号不断出错。你知道为什么会这样吗 以下是错误消息: Msg 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, s
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'
我将这个标志添加到视图中,并能够简化insert语句。谢谢大家的帮助
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
为了使查询工作正常,下面是等效代码
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
但我怀疑交叉连接不是您想要的,您能告诉我table flag与customer和dupaddr的关系吗?
正如我所评论的-不知何故,您的代码有点混乱。。。。尚不清楚这三个表customer、dupaddr和flag是如何关联的——它们应该正确地连接起来,以便您可以从单个SELECT语句中获取cust_no和flag 我根本看不出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???
或者,由于该标志似乎与所选的任何行都不相关,因此您也可以在语句之前获取一次:
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???
你可以试试这个
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
这个表customer,dupaddr,flag之间的关系是什么?最后一个SELECT CASTFlag AS INT应该做什么?这是错误的部分…..您需要以适当的方式联接表customer、dupaddr和flag,以便可以从这些联接表中选择两个项目cust_no和flag-您不能在使用它的位置使用另一个子查询-不起作用。flag表仅包含custflagid、FLAGNAMERIAL值,cust_no.dupadder是我创建的一个视图,用于获取具有不同cust_no但具有相同地址的所有客户的列表。flag表只包含custflagid、flagnumeric值,而cust_no.dupadder是我创建的一个视图,用于获取具有不同cust_no但具有相同地址的所有客户的列表抱歉,此评论是错误的,标志表中没有cust_no列