SQL: How to copy a row in the same table updating one of the fields WITHOUT listing all fields

StackOverflow https://stackoverflow.com/questions/4584060

  •  14-10-2019
  •  | 
  •  

Question

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

Was it helpful?

Solution 2

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... :/

OTHER TIPS

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top