Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » SQL Server »

Setear campo identity

Estas en el tema de Setear campo identity en el foro de SQL Server en Foros del Web. HOla amigos... tengo un problema, tengo una tabla temporal en la que la clave primaria es un identity, y despues de hacer inserts elimino todos ...
  #1 (permalink)  
Antiguo 26/02/2007, 08:21
Avatar de Developer9
(Desactivado)
 
Fecha de Ingreso: abril-2005
Ubicación: Mi Ecuador del alma
Mensajes: 4.196
Antigüedad: 19 años
Puntos: 47
Pregunta Setear campo identity

HOla amigos... tengo un problema, tengo una tabla temporal en la que la clave primaria es un identity, y despues de hacer inserts elimino todos los registros de la tabla para insertar otra vez pero no se como hacer para que el campo identity vuelva a generarse desde 1
  #2 (permalink)  
Antiguo 26/02/2007, 09:00
Avatar de Andres95
Colaborador
 
Fecha de Ingreso: diciembre-2004
Mensajes: 1.802
Antigüedad: 19 años, 4 meses
Puntos: 38
Re: Setear campo identity

Truncando la tabla (Truncate table #temporal) para eliminar los registros
o bien utilizando la instruccion dbcc checkindent(tabla, reseed, valor a fijar)
__________________
La sencillez y naturalidad son el supremo y último fin de la cultura...
--
MCTS : SQL Server 2008, .NET Framework 3.5, ASP.NET Applications.
  #3 (permalink)  
Antiguo 26/02/2007, 09:18
Avatar de Developer9
(Desactivado)
 
Fecha de Ingreso: abril-2005
Ubicación: Mi Ecuador del alma
Mensajes: 4.196
Antigüedad: 19 años
Puntos: 47
Re: Setear campo identity

Hola Andresillo, gracias por contestar

Si hago Truncate table #temporal exactamente que es lo que estoy haciendo? eliminando los registros?

cual es la diferencia con ejecutar un delete from #temporal?
  #4 (permalink)  
Antiguo 26/02/2007, 09:22
Avatar de Andres95
Colaborador
 
Fecha de Ingreso: diciembre-2004
Mensajes: 1.802
Antigüedad: 19 años, 4 meses
Puntos: 38
Re: Setear campo identity

De Books online de SQL Server...
Código:
TRUNCATE TABLE
Removes all rows from a table without logging the individual row deletes. 


Remarks

TRUNCATE TABLE is functionally identical to DELETE statement with no WHERE clause: both remove all rows in the table. But TRUNCATE TABLE is faster and uses fewer system and transaction log resources than DELETE. 

The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table's data, and only the page deallocations are recorded in the transaction log.

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes and so on remain. The counter used by an identity for new rows is reset to the seed for the column. If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.

You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint; instead, use DELETE statement without a WHERE clause. Because TRUNCATE TABLE is not logged, it cannot activate a trigger. 

TRUNCATE TABLE may not be used on tables participating in an indexed view.
__________________
La sencillez y naturalidad son el supremo y último fin de la cultura...
--
MCTS : SQL Server 2008, .NET Framework 3.5, ASP.NET Applications.
  #5 (permalink)  
Antiguo 26/02/2007, 09:35
Avatar de Developer9
(Desactivado)
 
Fecha de Ingreso: abril-2005
Ubicación: Mi Ecuador del alma
Mensajes: 4.196
Antigüedad: 19 años
Puntos: 47
Re: Setear campo identity

je je, gracias, aunque en estos momentos no estoy en condiciones como para traducir y aprender algo más sobre sql server

Una pregu mas, que va en el segundo parametro de

dbcc checkindent(tabla, reseed, valor a fijar)
  #6 (permalink)  
Antiguo 26/02/2007, 10:33
Avatar de Mithrandir
Colaborador
 
Fecha de Ingreso: abril-2003
Mensajes: 12.106
Antigüedad: 21 años
Puntos: 25
Re: Setear campo identity

TRUNCATE resetea el IDENTITY, DELETE no

A tu última pregunta, generalmente va 1, a menos que quieras comenzar en otro número.
__________________
"El hombre, en su orgullo, creó a Dios a su imagen y semejanza."
Friedrich Nietzsche
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:49.