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 SQL SERVER. Show all posts
Showing posts with label SQL SERVER. 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
Thursday, June 6, 2013
How to connect CodeIgniter to MS SQL Server 2008
On this post I will try to explain step by step how to connect CodeIgniter to a MS SQL Server Database, I recently had to do it and had a few troubles to make it work, and no clear explanation, For these instructions I used XAMPP, but it can be applied to any other stack, just considering which PHP version they may have.
FOR WINDOWS XP
For Windows XP we have to use the 5.3.x PHP version, because the native SQL Client version and the PHP SQL driver compatible with windows XP wont work with higher versions, If you are using XAMPP, version 1.7.3 works perfectly. You can download old XAMPP versions from Here.
1st Step - Install the SQL Native Client from Microsoft:
For Windows XP the SQL Native Cliente 2008 version is sufficient for the version of the driver we will install on the next step, you can install 2012 version additionally, if you want to. The Client version for 2008 can be downloaded from Here.
Etiquetas:
Codeigniter,
PHP,
SQL SERVER
Wednesday, June 5, 2013
Como configurar Codeigniter para conectarse a SQL Server 2008 (esp)
Este post es para aclarar el procedimiento para habilitar la conexión a Microsoft SQL Server desde Codeigniter ya que recientemente tuve la necesidad de hacerlo y me encontré con varios problemas y ninguna explicación clara, Para estas instrucciones tomen en cuenta que utilicé XAMPP en el servidor, pero se puede aplicar a cualquier otro stack que utilicen:
PARA WINDOWS XP
Para windows XP tenemos que limitarnos a usar php version 5.3.x, mas adelante les indicare por que, por lo tanto si usáran XAMPP, la version 1.7.3 funciona perfecto.Versiones antiguas de Xampp.
1er Paso - Instalar el SQL Native Client de Microsoft:
Para Windows XP es suficiente la versión SQL Native Cliente de 2008 para la version de el driver que instalaremos en el paso 2, pero se puede instalar la del 2012 tambien si se desea. La version del cliente para 2008 pueden descargarlo Aqui
Etiquetas:
Codeigniter,
Spanish,
SQL SERVER
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)