从 SQL 查询创建表
这是一个令人烦恼的问题,我不知道如何解决它。我正在使用 Microsoft SQL Server 2008。
所以我有两个表,我需要更新它们。他们共享一个共同的密钥,比如 id。我想用一些东西更新 Table1
,然后更新分别在 Table1
中修改的 Table2
行。
问题是我不太知道修改了哪些行,因为我使用 ORDER BY NEWID()
随机选择它们,所以我可能无法使用 JOIN
Table2
以任何方式。我正在尝试保存在 Table1
查询中修改的必要详细信息,并将它们传递给 Table2
这就是我想要做的
CREATE TABLE IDS (id int not null, secondid int)
SELECT [Table1].[id], [Table1].[secondid]
INTO IDS
FROM
(
UPDATE [Table1]
SET [secondid]=100
FROM [Table1] t
WHERE t.[id] IN
(SELECT TOP 100 PERCENT t.[id] FROM [Table1]
WHERE (SOME_CONDITION)
ORDER BY NEWID()
)
)
UPDATE [Table2]
SET some_column=i.secondid
FROM [Table2] JOIN IDS i ON i.id = [Table2].[id]
但我得到
关键字“UPDATE”附近的语法不正确。
所以问题是:如何解决语法错误或者这是更好的方法吗?
注意:第一个 FROM
括号中的查询在这个新要求之前运行良好,所以我怀疑其中存在问题。或者也许?
编辑:按照 skk 建议更改第二个 UPDATE
仍然会导致相同的错误(恰好位于包含 UPDATE
的下面一行):
UPDATE [Table2]
SET some_column=i.secondid
FROM [Task] JOIN IDS i on i.[id]=[Table2].[id]
WHERE i.id=some_value
This is one annoying issue and I can't figure out how to solve it. I'm Using Microsoft SQL Server 2008.
So I have two tables and I need to update both of them. They share a common key, say id. I want to update Table1
with some stuff and then update the Table2
rows which were respectively modified in Table1
.
The issue is that I don't quite know which rows were modified, because I'm picking them randomly with ORDER BY NEWID()
so I probably cannot use a JOIN
on Table2
in any way. I am trying to save the necessary details which were modified in my query for Table1
and pass them to Table2
This is what I'm trying to do
CREATE TABLE IDS (id int not null, secondid int)
SELECT [Table1].[id], [Table1].[secondid]
INTO IDS
FROM
(
UPDATE [Table1]
SET [secondid]=100
FROM [Table1] t
WHERE t.[id] IN
(SELECT TOP 100 PERCENT t.[id] FROM [Table1]
WHERE (SOME_CONDITION)
ORDER BY NEWID()
)
)
UPDATE [Table2]
SET some_column=i.secondid
FROM [Table2] JOIN IDS i ON i.id = [Table2].[id]
But I get
Incorrect syntax near the keyword 'UPDATE'.
So the question is: how can I solve the syntax error or is it a better way to do this?
Note: the query enclosed between the parentheses of the first FROM
worked well before this new requirement, so I doubt there's a problem in there. Or maybe?
EDIT: Changing the second UPDATE
as skk suggested still leads to the same error (on exactly the below line which contains UPDATE
):
UPDATE [Table2]
SET some_column=i.secondid
FROM [Task] JOIN IDS i on i.[id]=[Table2].[id]
WHERE i.id=some_value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SQL Server 不是手动创建新表,而是使用 OUTPUT 子句来帮忙解决这个问题
Instead of creating a new table manually, SQL server has the OUTPUT clause to help with this
它会抱怨,因为您没有为第一个查询中使用的派生表添加别名,紧接在
UPDATE [Table2]
之前。如果添加别名,您将收到不同的错误:
这又回到了@Adam Wenger 的回答。
不确定我完全理解你想要做什么,但是以下 sql 将执行(替换
SOME_CONDITION
后):It's complaining because you aren't aliasing the derived table used in the first query, immediately preceding
UPDATE [Table2]
.If you add an alias, you'll get a different error:
Which leads back to @Adam Wenger's answer.
Not sure I completely understand what you are trying to do, but the following sql will execute (after replacing
SOME_CONDITION
):Update 语法如下
,但您也在其中使用了 FROM 关键字。
编辑:
您更改代码如下,然后重试
The Update syntax is as follows
but you have used FROM keyword also in that.
EDIT:
You change the code like follows and try again