Vamos a analizar diferentes escenarios en los cuales necesitamos copiar/clonar filas de una tabla a esa misma tabla, Solo cambiando el valor de un campo, Que en este caso sera el campo Id, Usando Microsoft SQL Server.
Supongamos que tenemos el AntId (Id original Id de el que queremos copiar las filas, en este caso este Id puede estar en varias filas o solo en una) y el NuevoId ( El Id nuevo que queremos poner en las filas que vamos a copiar/clonar)
Caso 1 - conocemos el nombre de la tabla y de todos sus campos:
El primer caso es de una tabla de la cual sabemos de antemano su nombre y su estructura, así que podemos escribir una consulta como la siguiente:
DECLARE @OldId int
DECLARE @NuevoId int
SET @AntId =13456 -- ejem.
SET @NuevoId =45687 -- ejem.
INSERT INTO MiTabla(Id,ColumnaA,ColumnaB,ColumnaC)
SELECT @NuevoId ,ColumnaA,ColumnaB,ColumnaC
FROM MyTable WHERE Id=@AntId
Developer 'Extraordinaire'. .Net C#, PHP, JavaScript, SQL and IT enthusiastic. Also happily married father of four and avid book reader. This is my bilingual blog, please enjoy.
Showing posts with label TSQL. Show all posts
Showing posts with label TSQL. Show all posts
Wednesday, January 14, 2015
Copiar filas en la misma tabla solo cambiando el Id (TSQL) (esp)
Etiquetas:
Spanish,
SQL SERVER,
TSQL
Copying Rows On The Same Table and updating Only some fields (TSQL)
We will analyze different scenarios on which we need to copy or clone rows from one table to the same table, only changing the value of one know field, which in this case is the Id field, Using Microsoft SQL Server.
Supposing we are provided the OldId (original Id from were we want to copy the rows, in this case this Id can be on several rows or just one) and a NewId ( the Id we want to put on the row we are going to copy/clone)
If we know the columns and table names before hand:
The first scenario would be a table on which we know before hand the name of the table and all the fields involved, so we can easily write the following query.
DECLARE @OldId int
DECLARE @NewId int
SET @OldId =13456 -- i.e
SET @NewId =45687 -- i.e
INSERT INTO MyTable(Id,ColumnA,ColumnB,ColumnC)
SELECT @NewId,ColumnA,ColumnB,ColumnC
FROM MyTable WHERE Id=@OldId
Supposing we are provided the OldId (original Id from were we want to copy the rows, in this case this Id can be on several rows or just one) and a NewId ( the Id we want to put on the row we are going to copy/clone)
If we know the columns and table names before hand:
The first scenario would be a table on which we know before hand the name of the table and all the fields involved, so we can easily write the following query.
DECLARE @OldId int
DECLARE @NewId int
SET @OldId =13456 -- i.e
SET @NewId =45687 -- i.e
INSERT INTO MyTable(Id,ColumnA,ColumnB,ColumnC)
SELECT @NewId,ColumnA,ColumnB,ColumnC
FROM MyTable WHERE Id=@OldId
Wednesday, May 29, 2013
Reduce the size SQL Server 2008 Log file
This is a very common problem, the log file on SQL Server may grow excessively and it may even fill a small server. On previous versions of SQL Server the problem was a little harder to manage, but since version 2008 reducing the size of this file can be achieve with this little script, 100% guaranteed:
Use my_db GO Alter Database my_db Set Recovery Simple GO Alter Database mi_db Set Recovery Full GO DBCC SHRINKFILE ('my_db_log', 1) GO
Where "my_db" is the name of our database and "my_db_log" is the name of the log of your database, If you dont know it you can check it on the properties of the database.
And that, my Friend, It's all!
Monday, May 27, 2013
Como reducir el registro de SQL Server 2008 (esp)
Este es un problema muy comun, el registro de SQL Server crece en exceso y puede llegar a llenar un servidor pequeño. En versiones Anteriores el problema era mas complejo, pero desde la version 2008 el reducir este archivo se puede lograr con el siguiente script 100% garantizado:
Use mi_db GO Alter Database mi_db Set Recovery Simple GO Alter Database mi_db Set Recovery Full GO DBCC SHRINKFILE ('mi_db_Registro', 1) GO
Donde "mi_db" es el nombre de tu base de datos y "mi_db-registro" es el nombre del regsitro de tu base de datos, puedes verificarlo en las propiedades de la base de datos.
Y eso, amigo mío, ¡es todo!
Etiquetas:
Spanish,
SQL SERVER,
TSQL
Subscribe to:
Posts (Atom)