Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

how do i set identity on in SQL?

i am using the following command:
insert into [wf10taa_test].[dbo].[cutdetail] select * FROM [wf10taa].[dbo].[cutdetail] where cutno = '9701373'

and i am receiving the followig error when i run it:
An explicit value for the identity column in table 'wf10taa_test.dbo.cutdetail' can only be specified when a column list is used and IDENTITY_INSERT is ON.

what i have to do to allow this line to run? and after i finish do i need to set off? how?

Thanks
0
gvilbis
Asked:
gvilbis
5 Solutions
 
JestersGrindCommented:
SET IDENTITY_INSERT tablename ON
GO

Do the insert.

SET IDENTITY_INSERT tablename OFF
GO

Greg

0
 
James MurrellProduct SpecialistCommented:
for further info on JestersGrinds comment http://msdn.microsoft.com/en-us/library/ms188059.aspx
0
 
MightyMirzaCommented:
SET IDENTITY_INSERT [wf10taa_test].[dbo].[cutdetail] ON
GO

insert into [wf10taa_test].[dbo].[cutdetail] (ColumnName1,ColumnName2,..)
select * FROM [wf10taa].[dbo].[cutdetail] where cutno = '9701373'

SET IDENTITY_INSERT [wf10taa_test].[dbo].[cutdetail] OFF
GO

--the above mentioned code will help you do the insert, and yes you have to set it OFF after the insert (which the above code will take care).
--You were not adding columns after the table name(the table in which the rows has to be inserted)
--Make sure you put in your column names in the above query.

Fahad Mirza


0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
gvilbisAuthor Commented:
i did the following but still receiving the same error:
SET IDENTITY_INSERT [wf10taa_test].[dbo].[cutdetail] ON
go
insert into [wf10taa_test].[dbo].[cutdetail] select * FROM [wf10taa].[dbo].[cutdetail] where cutno = '9701373'
SET IDENTITY_INSERT [wf10taa_test].[dbo].[cutdetail] OFF
go


error:
An explicit value for the identity column in table 'wf10taa_test.dbo.cutdetail' can only be specified when a column list is used and IDENTITY_INSERT is ON.

i must to put all the columns names? but i have many columns, is there a shortage way to do it instead to type all the columns names? it will take a long time?

Thanks
0
 
Kevin CrossChief Technology OfficerCommented:
If this is a test environment, you could (1) restore production to development or (2) remove the identity restriction on column totally from tables in test if you will be replicating the data from production always.
0
 
Anthony PerkinsCommented:
If you are not explicitly inserting the IDENTITY value than all you need to do is explicitly declare all the columns, as in (there is not need for the SET IDENTITY_INSERT stuff ...) :

INSERT INTO [wf10taa_test].[dbo].[cutdetail] (Col1, Col2, Col3, ...)
SElECT Col1, Col2, Col3, ...
FROM [wf10taa].[dbo].[cutdetail]
WHERE cutno = '9701373'

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now