Why insert new have must command line "SET IDENTITY_INSERT TableName ON"save in SQL ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Why insert new have must command line "SET IDENTITY_INSERT TableName ON"save in SQL ?

  1. #1
    Join Date
    Sep 2007
    Posts
    210

    Why insert new have must command line "SET IDENTITY_INSERT TableName ON"save in SQL ?

    I have a few for example insert new records, when the store must have "SET IDENTITY_INSERT TabeleName ON"; and "SET IDENTITY_INSERT TabeleName OFF"; other examples do not need 2 lines ON and OFF, if I wanted to give my 2 lines to adjust what ? you see the code below:

    Code:
     
    strSQL = "SET IDENTITY_INSERT TableName ON ";
    strSQL = strSQL + "INSERT INTO TableName(USERNAME";
    strSQL = ...
    strSQL = strSQL + "SET IDENTITY_INSERT TableName OFF";
    ...
    SqlCommand cmd = new SqlCommand(strSQL,cnn);
      try
      {
         cmd.ExecuteNonQuery();
         cnn.Close();
      }
    ....

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,471

    Re: Why insert new have must command line "SET IDENTITY_INSERT TableName ON"save in S

    It depends on how the table is set up. If you have an identity column, you don't need to insert values into that column during the insert.

    If you are trying to insert values, then you need to set the identity insert.

  3. #3
    Join Date
    Sep 2007
    Posts
    210

    Re: Why insert new have must command line "SET IDENTITY_INSERT TableName ON"save in S

    I ran a few examples have must command lines ON and OFF new stored it in SQL Server but a few example other no need 2 lines ON OFF but still be stored in SQL Server, I guess on SQL Server to tune in item no need to use on line 2 (ON OFF), because the examples I ran along one machine, data file attch into SQL Server and i don't moving data from 1 server to another and you have to match ID values.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center