为什么这个特定语法不适用于更新插入?

发布于 2024-09-27 03:40:43 字数 509 浏览 6 评论 0原文

我正在使用 SQL Server 2005,我想同步两个具有相同定义但存在于不同数据库中的表。 MERGE INTO 仅存在于 2008 年,我更喜欢一种不必在 UPDATE 中指定列的语法。因此,我偶然发现使用以下语法的各种帖子

UPDATE Destination FROM (Source INTERSECT Destination)
INSERT INTO Destination FROM (Source EXCEPT Destination)

但是当我尝试执行它,我得到:

Incorrect syntax near the keyword 'FROM'.

我怎样才能让它工作?我有多个需要同步的表,并且我不想在每个语句中指定所有列。

感谢您的任何提示!

I'm using SQL Server 2005 and I want to synchronize two tables which have the same definition but exist in different databases. MERGE INTO only exists in 2008 and I'd prefer a syntax where I don't have to specify columns in the UPDATE. So I stumbled upon various posts using the following syntax:

UPDATE Destination FROM (Source INTERSECT Destination)
INSERT INTO Destination FROM (Source EXCEPT Destination)

But when I try to execute it I get:

Incorrect syntax near the keyword 'FROM'.

How can I get this working? I have multiple tables which I need to synchronize and I don't want to specify all the columns in every statement.

Thanks for any hint!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

煮茶煮酒煮时光 2024-10-04 03:40:43

根据联机丛书,更新 命令需要 set 关键字,并且它必须位于可选的 from 关键字之前。 插入命令< /a> 没有独立的 from 关键字,from 仅作为 select 语句的一部分存在,或者作为派生表源或在公共表表达式内。

您引用的链接未显示有效的 SQL Server 2005 语法。

“我怎样才能让它工作?我有多个需要同步的表,并且我不想在每个语句中指定所有列。”

对于更新,您必须指定所有列。对于insert,如果源和目标具有相同的结构,则可以使用insert into TARGTET_TABLE_NAME select * from SOURCE_TABLE_NAME BUT,不建议用于生产代码中,如果源或目标发生更改,该语句就会中断。如果源和目标不同,则必须至少在插入的一侧指定列。

According to Books Online the update command requires the set keyword, and it must come before the optional from keyword. The insert command doesn't have a stand alone from keyword, the from only exists as part of a select statement either as a derived table source or within a common table expression.

The link you reference is not showing valid SQL Server 2005 syntax.

"How can I get this working? I have multiple tables which I need to synchronize and I don't want to specify all the columns in every statement."

For update, you must specify all the columns. For insert if the source and destination have the same struture then you can use insert into TARGTET_TABLE_NAME select * from SOURCE_TABLE_NAME BUT that is not recommended for production code, if the source or destination change, the statement would break. If source and destination differ, then you must specify columns on at least one side of the insert.

伤感在游骋 2024-10-04 03:40:43

如果这不能回答您的问题,我很抱歉,但假设这样做的全部原因是为了节省时间,您不能右键单击源表并生成 INSERT 脚本,然后右键单击目标表并生成一个空白的 SELECT 脚本,然后将两者结合起来?仅当您的环境可以接受“杀戮和填充”时,这才有效。

I'm sorry if this doesn't answer your question, but assuming the whole reason for this is in the interest of saving time, can't you just right-click the source table and generate the INSERT script, then right-click the destination table and generate a blank SELECT script, then combine the two? This will only work if a kill-and-fill is acceptable in your environment.

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