Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #1930

Re: WHILE LOOP AS FOR LOOP

From Erland Sommarskog <esquel@sommarskog.se>
Newsgroups comp.databases.ms-sqlserver
Subject Re: WHILE LOOP AS FOR LOOP
Date 2015-07-29 15:44 +0000
Organization Erland Sommarskog
Message-ID <XnsA4E6B4B775C33Yazorman@127.0.0.1> (permalink)
References <d5581480-cb05-4b19-8f82-995f7bfe01e3@googlegroups.com> <XnsA4E6A3D9FCE25Yazorman@127.0.0.1> <6141367c-ecc2-4a6b-aa21-1b006053a8fb@googlegroups.com>

Show all headers | View raw


Jessica González (jess.rgm@gmail.com) writes:
> thanks.. but
> 
> SET IDENTITY_INSERT table ON;
> INSERT INTO table(pk,activo,permiso,rol) 
>       SELECT IDENT_CURRENT('table')+1,'S',id, 1
>       FROM table2
> 
> 
> return error  
> 
> Violation of PRIMARY KEY constraint 'PK__table__3213E83F2B947552'.
> Cannot insert duplicate key in object 'table'. The duplicate key value
> is (6). 

Why do you fiddle with IDENTITY_INSERT ON? That is an option you rarely have 
any reason to play with. And IDENT_CURRENT you have even more rare reason to 
touch. That is not a particularly useful function.

If you have made the column an IDENTITY column, you should not assign the 
values explicitly. And vice versa: if you want to be able to specify the 
values explicitly, you sould not use IDENTITY.


-- 
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Back to comp.databases.ms-sqlserver | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

WHILE LOOP AS FOR LOOP Jessica González <jess.rgm@gmail.com> - 2015-07-29 06:58 -0700
  Re: WHILE LOOP AS FOR LOOP Erland Sommarskog <esquel@sommarskog.se> - 2015-07-29 14:04 +0000
    Re: WHILE LOOP AS FOR LOOP Jessica González <jess.rgm@gmail.com> - 2015-07-29 07:31 -0700
      Re: WHILE LOOP AS FOR LOOP Erland Sommarskog <esquel@sommarskog.se> - 2015-07-29 15:44 +0000
        Re: WHILE LOOP AS FOR LOOP rja.carnegie@gmail.com - 2015-07-29 10:03 -0700
  Re: WHILE LOOP AS FOR LOOP --CELKO-- <jcelko212@earthlink.net> - 2015-08-14 17:54 -0700

csiph-web