[英]Incorrect syntax near the keyword 'select'
I have a poorly normalized set of tables and I am trying to correct this issue. 我的一组标准化表很差,我正在尝试纠正此问题。 The DB is MS SQL 2005.
该数据库是MS SQL 2005。
Table1 has an id
field (record ID), a mainID
field (person ID) and a sequence
field (and int that determines the sort order) 表1具有一个
id
字段(记录ID),一个mainID
字段(人员ID)和一个sequence
字段(以及确定排序顺序的int)
Table2 has its own id
field and a copy of the id for the first record by a person ( sequence
= 1). Table2有其自己的
id
字段和一个人的第一条记录的id副本( sequence
= 1)。
I've added a new field to table2 call table2_id and I would like to populate this field with the ID from table2 so that I can do away with table1.mainID
. 我在table2调用table2_id中添加了一个新字段,我想用来自table2的ID填充此字段,以便可以
table1.mainID
。 table2 only has a record for one of the records for each person and mainId
is set to the id where table1.sequence = 1
. table2仅对每个人的一条记录有一条记录,并且
mainId
设置为id,其中table1.sequence = 1
。
This is the update query I thought would do the job by I'm getting errors 这是我认为会遇到错误的更新查询
update table1 as a
set regInfoID = (select b.id
from table2 as b
where b.ref1 = (select c.id
from table1 as c
where c.mainID = a.mainID
and sequence = 1))
I believe I'm on the right track her since the following query works fine 我相信我在正确的位置上,因为以下查询效果很好
select regInfoID = (select b.id
from table2 as b
where b.ref1 = (select c.id
from table1 as c
where c.mainID = a.mainID
and sequence = 1)), a.*
from table1 as a
I think your query is equivalent to this: 我认为您的查询与此等效:
update a
set regInfoID = b.id
-- select a.*, b.id
from table2 b
inner join table1 c on c.id = b.ref1
inner join table1 a on c.mainID = a.mainID and c.sequence = 1
From this query, I think you will potentially have indeterminate results because table2 (b) is not guaranteed to be a single row result. 通过此查询,我认为您可能会有不确定的结果,因为不能保证table2(b)是单行结果。 So regInfoID will be set to one of the resulting b.id values.
因此regInfoID将被设置为结果b.id值之一。
I figured it out 我想到了
update table1
set regInfoID = (select b.id
from table2 as b
where b.ref1 = (select c.id
from table1 as c
where c.mainID = a.mainID
and sequence = 1))
from table1 as a
The error seems to have been cause by my having the alia in the update statement instead of in a from statement. 该错误似乎是由于我在update语句而不是from语句中使用了别名引起的。
Thanks for the help. 谢谢您的帮助。
Here's one way, using your SELECT statement that works, and wrapping it in a CTE: 这是一种使用有效的SELECT语句并将其包装在CTE中的方法:
with cte as (
select a.*, _regInfoID = (select b.id
from table2 as b
where b.ref1 = (select c.id
from table1 as c
where c.mainID = a.mainID
and sequence = 1))
from table1 as a
)
update cte set regInfoID = _regInfoID
I like this style, since you can preview your modifications before applying them, and it's trivial to turn the SELECT
into an UPDATE
我喜欢这种样式,因为您可以在应用修改之前预览您的修改,并且将
SELECT
转换为UPDATE
并不容易。
But the problem you had with the original query was just a syntax error. 但是您对原始查询的问题只是语法错误。 This is how it should have been written.
这是应该如何编写的。 Note the
FROM table1 AS a
: 请注意
FROM table1 AS a
:
update a
set regInfoID = (select b.id
from table2 as b
where b.ref1 = (select c.id
from table1 as c
where c.mainID = a.mainID
and sequence = 1))
from table1 as a
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.