Wednesday, November 29, 2017

Copy Data from One Table to Another Table and insert values


There are multiple ways to do this.

1) INSERT INTO SELECT
This method is used when table is already created in the database earlier and data have to be inserted into this table from another table. If columns listed in the INSERT clause and SELECT clause are same, listing them is not required.

INSERT INTO table-name (column-names)
SELECT column-names 
FROM table-name
WHERE condition


2) SELECT INTO
This method is used when table is not created earlier and it needs to be created when data from one table must be inserted into a newly created table from another table. The new table is created using the same data types as those in selected columns.

select * into dbo.CopyTo from dbo.CopyFrom


3) INSERT BY VALUE
By using a values clause to specify the data values for one row.
          INSERT INTO MyTable (Column1, Column2)
              VALUES (‘Column1Value’, Column2 Value.')

2 comments:

  1. Data is the business asset for any organisation which is audited and protected. To gain in their business, it is become very urgent for every organization to choose few good predictive data models and validates them using test data before figuring out an operationalization plan for the model to be deployed to production so that applications can consume it.
    Artificial intelligence with SQL Server

    ReplyDelete

SQL Script to list out name of tables and its records in the database

SET NOCOUNT ON  DBCC UPDATEUSAGE(0)  -- DB size. EXEC sp_spaceused -- Table row counts and sizes. CREATE TABLE #temptable  (      [name] NVA...