SQL:如何复制同一个表中的行,更新其中一个字段而不列出所有字段

发布于 2024-10-10 02:44:55 字数 630 浏览 6 评论 0原文

你好 我搜索了这个问题的答案,但在这里没有找到任何答案。 我使用的是 Access 2010。 基本上,我有一个包含报告的表格,并且报告有修订号。 我找到了关于如何复制字段并仅更新其中一个字段的答案,但它看起来有点像这样:

INSERT INTO reports (fieldA, fieldB, fieldC, revision, fieldD, fieldE)
SELECT  fieldA, fieldB, fieldC, 2, fieldD, fieldE
FROM reports
WHERE <somecondition to select which report to copy>

事情是我有很多字段,所以我想要看起来更像这样的东西:

INSERT INTO reports 
SELECT  *, revision=2
FROM reports
WHERE <somecondition to select which report to copy>

我知道代码是不正确;这只是为了描述我想要的。就像这样,一种方法不使用巨大的 SQL 行列出所有字段,而只列出我想要更改的字段。 (我想在同一个表中保留以前修订的副本)

预先感谢任何可以提供帮助的人:)

Hello
I searched for an answer to this question but didn't find any here.
I'm using Access 2010.
Basically, I've got a table with reports, and reports have a revision number.
I found an answer about how to copy fields and update only one of them, but it looks somewhat like this:

INSERT INTO reports (fieldA, fieldB, fieldC, revision, fieldD, fieldE)
SELECT  fieldA, fieldB, fieldC, 2, fieldD, fieldE
FROM reports
WHERE <somecondition to select which report to copy>

Thing is I have a load of fields, so I'd like something that would look more like this:

INSERT INTO reports 
SELECT  *, revision=2
FROM reports
WHERE <somecondition to select which report to copy>

I know that code is incorrect; it's just to describe what I would like. As in, a way to not have a huge SQL line listing all the fields, but only the one I want to change.
(I want to keep a copy of previous revisions in the same table)

Thanks in advance to whoever can help :)

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

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

发布评论

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

评论(2

爱,才寂寞 2024-10-17 02:44:55

我非常确定您无法在 MS Access 或任何其他类型的 SQL 中执行此操作。正如您可能已经知道的那样,您可以做的是使用存储过程(在 Access 中称为存储查询),它将您的修订号和要复制的报告 ID(或一些其他 WHERE 条件)作为参数。这样,您仍然需要指定所有字段,但只需在数据库中而不是在代码中执行一次。示例如下:

http://www.stardeveloper.com/ articles/display.html?article=2001050101&page=1

呵呵!

I'm pretty sure you can't do this in MS Access or indeed any other flavor of SQL. What you can do, as you probably already know, is use a stored procedure (called a Stored Query in Access) which takes your revision number and the id of the report to copy (or some other WHERE conditions) as arguments. This way, you still have to specify all the fields, but you do it only once and in your database instead of in your code. An example is here:

http://www.stardeveloper.com/articles/display.html?article=2001050101&page=1

HTH!

゛时过境迁 2024-10-17 02:44:55

我在这个问题中发现了一个有趣的替代方案,使用临时表
具有唯一索引的 SQL 克隆记录

DROP TABLE #tmp_MyTable

SELECT * INTO #tmp_MyTable
FROM MyTable
WHERE MyIndentID = 165

ALTER TABLE #tmp_MyTable
DROP Column MyIndentID

INSERT INTO MyTable
SELECT * 
FROM #tmp_MyTable

我可以做同样的事情,删除主键并更新修订版,然后将其复制回我的表。

这不是我正在寻找的解决方案,而是同时的替代方法。

编辑:

尝试此解决方案但没有成功:
VBA 告诉我一些类似于“SELECT INTO 指令不允许具有多个值的字段(运行时错误 3838)”的内容,

我有一个 OLE 字段和一个附件字段,我怀疑这是错误的原因。但我需要保留它们......:/

I found an interesting alternative in this question, using a temporary table
SQL clone record with a unique index

DROP TABLE #tmp_MyTable

SELECT * INTO #tmp_MyTable
FROM MyTable
WHERE MyIndentID = 165

ALTER TABLE #tmp_MyTable
DROP Column MyIndentID

INSERT INTO MyTable
SELECT * 
FROM #tmp_MyTable

I can do the same, dropping the primary key and updating the revision, then copying it back to my table.

Not the solution I'm looking for, but an alternative way in the meantime.

EDIT:

Tried this solution without success:
VBA tells me something along the lines of "fields with multiple values are not allowed with SELECT INTO instructions (runtime error 3838)"

I have both an OLE field and an attachment field, which I suspect to be the cause of the error. But I need to keep them... :/

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