SQL Server - Insert Into Select


La sintaxis es: 

  1. INSERT INTO table-name (column-names)
  2. SELECT column-names
  3. FROM table-name
  4. WHERE condition

SQL INSERT SELECT INTO


Problema: Copiar todos los Canadian suppliers en la tabla Customer


La sentencia sql:

  1. INSERT INTO Customer (FirstName, LastName, City, Country, Phone)
  2. SELECT LEFT(ContactName, CHARINDEX(' ',ContactName) - 1) AS FirstName,
  3. SUBSTRING(ContactName, CHARINDEX(' ',ContactName) + 1, 100) AS LastName,
  4. City, Country, Phone
  5. FROM Supplier
  6. WHERE Country = 'Canada'

Resultado:



Artículo Anterior Artículo Siguiente