Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

identity_insert error

Posted on 2013-10-24
7
Medium Priority
?
321 Views
Last Modified: 2013-10-24
This quick script:

/****** Script for SelectTopNRows command from SSMS  ******/
set identity_insert [iMIS_MCLE_15TEST].[dbo].[BM_OnlinePass_Subscription] on
insert into [iMIS_MCLE_15TEST].[dbo].[BM_OnlinePass_Subscription]
SELECT [id]
      ,[clientID]
      ,[startDate]
      ,[endDate]
      ,[period]
      ,[pricePaid]
      ,[isNewLawyer]
      ,[isSponsor]
      ,[isBaseSubscription]
      ,[optedOut]
      ,[sizeOfFirmSource]
      ,[baseAmount]
      ,[transactionalDiscountAmount]
      ,[priceToPayYear]
      ,[initialpurchdate]
      ,[monthlyamount]
      ,[lastpayment]
      ,[lastpaydate]
      ,[isPromotion]
      ,[isAutoEnrolled]
      ,[otherDiscountAmount]
      ,[isSoldThroughAdmin]
      ,[periodNextYear]
      ,[subscriptionStatus]
      ,[renewalFlag]
  FROM [iMIS_MCLE_Prod].[dbo].[BM_OnlinePass_Subscription]
  where lastpaydate >= '11/01/2013' and lastpaydate <= '11/30/2013'



generates the following error:

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

Why?
0
Comment
Question by:Jeff_Kingston
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39597123
One of these columns, probably id, is an identity field, which means you can't intentionally insert a value into it.

The only exeption is to...
SET IDENTITY_INSERT ON
-- Do your insert here, so it doesn't duplicate rows for the identity field
SET IDENTITY_INSERT OFF

Open in new window


<edit>

Hmm.. try losing the table name in the IDENTITY_INSERT statement
0
 

Author Comment

by:Jeff_Kingston
ID: 39597196
the syntax above with no table name generates the following error

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'on'.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39597220
<wild guess>
Looking at the technet help page, the table name is there, but there's also a GO after the SET IDENTITY_INSERT line(s) that are not in the above code.  Try that.
0
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.

 

Author Comment

by:Jeff_Kingston
ID: 39597371
/****** Script for SelectTopNRows command from SSMS  ******/
set identity_insert [iMIS_MCLE_15TEST].[dbo].[BM_OnlinePass_Subscription] on
go
insert into [iMIS_MCLE_15TEST].[dbo].[BM_OnlinePass_Subscription]
SELECT [ID]
        ,[clientID]
      ,[startDate]
      ,[endDate]
      ,[period]
      ,[pricePaid]
      ,[isNewLawyer]
      ,[isSponsor]
      ,[isBaseSubscription]
      ,[optedOut]
      ,[sizeOfFirmSource]
      ,[baseAmount]
      ,[transactionalDiscountAmount]
      ,[priceToPayYear]
      ,[initialpurchdate]
      ,[monthlyamount]
      ,[lastpayment]
      ,[lastpaydate]
      ,[isPromotion]
      ,[isAutoEnrolled]
      ,[otherDiscountAmount]
      ,[isSoldThroughAdmin]
      ,[periodNextYear]
      ,[subscriptionStatus]
      ,[renewalFlag]
  FROM [iMIS_MCLE_Prod].[dbo].[BM_OnlinePass_Subscription]
  where lastpaydate >= '11/01/2013' and lastpaydate <= '11/30/2013'

Still generates this error:::::(

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

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39597404
>can only be specified when a column list is used
Wonder if this means that your INSERT INTO line must explicity spell out all column names.
This is a good programming practice anyways.
insert into [iMIS_MCLE_15TEST].[dbo].[BM_OnlinePass_Subscription] ([ID] ,[clientID],[startDate], etc, etc. [renewalFlag])
SELECT ... 

Open in new window

btw, in case it's not obvious, you can lose the database and schema references if this is executed in the same database and the default schema for the user is dbo.   You can also lose the square brackets round every name, as there's no characters in these names such as space, dash, $, etc. that warrants using them.
0
 

Author Closing Comment

by:Jeff_Kingston
ID: 39597601
Duhhhhh!  Kudos for seing my blindness,  of course we need the list of fields as part of the insert statement not the select.   A simple select * from  does the job.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39597616
Don't be too pissed at yourself.  I actually got burned on this on the 70-433 SQL 2008 development exam, as the question was 'How to insert rows into a table using as little code as possible, which means not spelling out the INSERT INTO columns, despite the fact that it's considered a poor programming practice and no one I know does this.

Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

610 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