从 SQL 查询创建表

发布于 2024-12-16 11:56:37 字数 1216 浏览 1 评论 0原文

这是一个令人烦恼的问题,我不知道如何解决它。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除→记忆 2024-12-23 11:56:37

SQL Server 不是手动创建新表,而是使用 OUTPUT 子句来帮忙解决这个问题

Instead of creating a new table manually, SQL server has the OUTPUT clause to help with this

不爱素颜 2024-12-23 11:56:37

它会抱怨,因为您没有为第一个查询中使用的派生表添加别名,紧接在 UPDATE [Table2] 之前。

如果添加别名,您将收到不同的错误:

嵌套的 INSERT、UPDATE、DELETE 或 MERGE 语句必须具有 OUTPUT 子句。

这又回到了@Adam Wenger 的回答。


不确定我完全理解你想要做什么,但是以下 sql 将执行(替换 SOME_CONDITION 后):

CREATE TABLE IDS (id int not null, secondid int)

UPDATE t SET [secondid] = 100
OUTPUT inserted.[id], inserted.[secondid] into [IDS]
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]

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:

A nested INSERT, UPDATE, DELETE, or MERGE statement must have an OUTPUT clause.

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):

CREATE TABLE IDS (id int not null, secondid int)

UPDATE t SET [secondid] = 100
OUTPUT inserted.[id], inserted.[secondid] into [IDS]
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]
总攻大人 2024-12-23 11:56:37

Update 语法如下

    UPDATE TableName SET ColumnName = Value WHERE {Condition}

,但您也在其中使用了 FROM 关键字。

编辑:

您更改代码如下,然后重试

UPDATE [Table2]  SET some_column=IDS.secondid WHERE  IDS.[id] = [Table2].[id] and 
IDS.id=some_value  

The Update syntax is as follows

    UPDATE TableName SET ColumnName = Value WHERE {Condition}

but you have used FROM keyword also in that.

EDIT:

You change the code like follows and try again

UPDATE [Table2]  SET some_column=IDS.secondid WHERE  IDS.[id] = [Table2].[id] and 
IDS.id=some_value  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文