如何复制任意表的一行更改一列

发布于 2024-08-25 00:35:19 字数 365 浏览 3 评论 0原文

我需要复制一行更改 PK。 每个客户端安装中的表可能有所不同,因此我不能只枚举列。 我已经设法执行以下操作:

INSERT INTO table SELECT * FROM table WHERE PK='value'

但显然它失败了,因为我试图复制 PK。

然后我尝试:

INSERT INTO table SELECT 'newValue' AS PK, * FROM table WHERE PK='value'

它也失败了,因为列名不匹配。

我知道PK永远是第一栏,但我不确定它有多大用处。

那么……这可能吗?有什么想法吗?

I need to duplicate one row changing the PK.
The table can be different in each client installation, so I can't just enumerate the columns.
I've managed to do the following:

INSERT INTO table SELECT * FROM table WHERE PK='value'

but obviously it fails because I'm trying to duplicate the PK.

Then I tried:

INSERT INTO table SELECT 'newValue' AS PK, * FROM table WHERE PK='value'

It also failed, because the column names didn't match.

I know the PK will always be the first column, but I'm not sure it's of much use.

So... Is this possible? Any idea?

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

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

发布评论

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

评论(2

淡看悲欢离合 2024-09-01 00:35:19

唯一的解决方案是通过查询其列列表并排除标识列来动态构建查询(这就是为什么我假设您希望跳过 PK)。

The only solution is to build the query dynamically by querying for its list of columns and excluding identity column (which is why I'm assuming you wish to skip the PK).

好听的两个字的网名 2024-09-01 00:35:19
INSERT INTO table1 (col1_PK, col2 col3) 
SELECT newValue, col2, col3
FROM table1 
WHERE col1_PK = 'Value'
INSERT INTO table1 (col1_PK, col2 col3) 
SELECT newValue, col2, col3
FROM table1 
WHERE col1_PK = 'Value'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文