SQL Server - can't set the IDENTITY_INSERT to ON in my database

Asked By Peter Norton on 12-Feb-07 05:15 AM
I am trying to set IDENTITY_INSERT to ON with "SET IDENTITY_INSERT AJAXMaster (table name) ON". But it shows me the error "The Set SQL construct or statement is not supported."
Then it shows the message that "Query has executed successfully."

Now when I am trying to insert a field containing image into this table using this query "insert into ajaxmaster values (12, 'Peter_Norton', 'Can not set the identity to ON', 'E:\logo.jpg')", it shows me "Can not insert explicit value for identity column in table 'AJAXMaster' when IDENTITY_INSERT is set to OFF."

Can anyone help me out?

Don't provide values for identity columns

Rajesh Madhukar replied to Peter Norton on 12-Feb-07 05:24 AM

Remove Identity Values

  I think you have set first column as identity.
  You cannot provide values for those columns which has been set identity.
Therfore don't provide values or this column
instead of using
insert into ajaxmaster values (12, 'Peter_Norton', 'Can not set the identity to ON', 'E:\logo.jpg')
use following
insert into ajaxmaster values ('Peter_Norton', 'Can not set the identity to ON', 'E:\logo.jpg')
 
Rajesh Madhukar

not working

Peter Norton replied to Rajesh Madhukar on 12-Feb-07 05:37 AM
@Mr.Rajesh Madhukar
Thanks a lot for answering so fast first of all.
I tried it. It gives me another error "Incomplete VALUES list."
Let me explain what exactly i'm trying to do. I want an Image to be uploaded into my database (table). I can do that with code too but before that for checking when I'm trying this query, these errors stops the work I'm doing. Can you tell me what "exactly" should I do?

SET IDENTITY_INSERT

mv ark replied to Peter Norton on 12-Feb-07 06:11 AM
The syntax of SET IDENTITY_INSERT is -

SET IDENTITY_INSERT [ database. [ owner. ] ] { table } { ON | OFF }

The tablename has to be replaced with the actual tablename. Try this -

SET IDENTITY_INSERT AJAXMaster ON

No need of "(table name)"
SET IDENTITY_INSERT ON
K Pravin Kumar Reddy replied to Peter Norton on 12-Feb-07 07:24 AM

[CODE]
set identity_insert LodgingSummary on
insert into LodgingSummary
(
LodgingSummaryID,
AccountInfoID,
LoadTransactionCode,
NoShowIndicator,
CheckInDate,
DailyRoomRate,
TotalOtherCharges
 )
select
LodgingSummaryID,
 (select min(ai.AccountInfoID) from AccountInfo ai
    where ai.AccountInfoID not exists (select AccountInfoID from LodgingSummary l
        where l.AccountInfoID= ai.AccountInfoID)) as AccountInfoID,
LoadTransactionCode,
NoShowIndicator,
CheckInDate,
DailyRoomRate,
TotalOtherCharges,
TotalTaxAmount
from Load_LodgingSummary
set identity_insert lodgingsummary off
[/CODE]

reference

http://www.eggheadcafe.com/community/aspnet/13/10001980/select-with-insert.aspx

http://www.eggheadcafe.com/community/aspnet/9/10003027/use-set-identityinsert-o.aspx

http://www.eggheadcafe.com/forumpost.aspx?topicid=9&forumpostid=37961

SET IDENTITY_INSERT (Transact-SQL)
K Pravin Kumar Reddy replied to Peter Norton on 12-Feb-07 07:32 AM

Allows explicit values to be inserted into the identity column of a table.  var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl03e8d9886,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl03img,";

SET IDENTITY_INSERT [ database_name . [ schema_name ] . ] table { ON | OFF }
var ExpCollDivStr = ExpCollDivStr; ExpCollDivStr = ExpCollDivStr + "ctl00_LibFrame_ctl05cff7e1e,"; var ExpCollImgStr = ExpCollImgStr; ExpCollImgStr = ExpCollImgStr + "ctl00_LibFrame_ctl05img,";
database_name

Is the name of the database in which the specified table resides.

schema_name

Is the name of the schema to which the table belongs.

table

Is the name of a table with an identity column.

At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, SQL Server 2005 returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for.

If the value inserted is larger than the current identity value for the table, SQL Server automatically uses the new inserted value as the current identity value.

The setting of SET IDENTITY_INSERT is set at execute or run time and not at parse time.

check example/information here (gud one)

http://msdn2.microsoft.com/en-us/library/ms188059.aspx