Solved

Insert SQL statement help needed.

Posted on 2015-02-17
6
86 Views
Last Modified: 2015-02-17
Hi Experts,

I am trying to recover a record accidentally deleted in a table, from a backup table (with the same structure).

when trying to  execute the below I am getting the following error.

 set IDENTITY_INSERT  patients on
 
insert into HomeCare.dbo.Patients
select * from backup_homecare.dbo.Patients 
where ID not in
(select id from HomeCare.dbo.Patients)

 set IDENTITY_INSERT  patients off

Open in new window

Msg 8101, Level 16, State 1, Line 5
An explicit value for the identity column in table 'HomeCare.dbo.Patients' can only be specified when a column list is used and IDENTITY_INSERT is ON.

What is the correct SQL or what am I missing here?
0
Comment
Question by:bfuchs
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:N R
ID: 40615821
select * 
into HomeCare.dbo.Patients
from backup_homecare.dbo.Patients 
where ID not in
(select id from HomeCare.dbo.Patients)

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40615828
"Msg 8101, Level 16, State 1, Line 5
An explicit value for the identity column in table 'HomeCare.dbo.Patients' can only be specified when a column list is used and IDENTITY_INSERT is ON."

you have not included the column list in your query

e.g.
INSERT INTO dbo.Tool (ID, Name) VALUES (3, 'Garden shovel');

and don't use select *, use explicit column names
0
 
LVL 4

Author Comment

by:bfuchs
ID: 40615836
@Nathan,
I got the below when executing yours..
There is already an object named 'Patients' in the database.

@PortletPaul,
I missed that, however why is it like that, if both tables have the same structure shouldn't it be straight forward?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 11

Expert Comment

by:N R
ID: 40615838
You could drop the existing table if there is no new data and just bring in the backup with my query above.  That will depend if this is dev or live environment.
0
 
LVL 4

Author Closing Comment

by:bfuchs
ID: 40615843
Thanks PortletPaul!

@Nathan,
No, I am restoring a single record after a day of work, while that would cause all my new records to be deleted..
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40615847
" if both tables have the same structure shouldn't it be straight forward?"
why doesn't it think like us? I don't know :)

It is always a good idea to be explicit I'm afraid - I know it will be frustrating particularly if there are lots of fields, but usually It's possible to get a list of fields pretty easy and with a bit of copy/paste etc. whip up an insert

seems that for this particular case, when overriding an auto-incrementing field, it just insists on know what you really absolutely and explicitly want.

cheers.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question