Freigeben über


A few common SQL scripts

A few command SQL script

(1) Insert multiple records of Table_A to a new Table_B

SELECT *

INTO Table_B

FROM Table_A

(2) Insert multiple records of Table_A to an existing table Table_B

INSERT INTO Table_B (Col1, Col2, Col3)

SELECT ColM, ColN, ColQ FROM Table_A

(3) Update Table_A through the data of Table_B

UPDATE Table_A

SET Col1=Table_B.Col2

FROM Table_B

Note: the above scripts can be easily extended to multiple tables, because multiple tables can be joined to one table. For example, if we want to update Table_A by the joined information of Table_B and Table_C, we can do the following

Update Table_A

Set Col1=myTable.ColM, Col2=myTable.ColP

FROM

(

SELECT Table_B.ColM, Table_C.ColP

FROM Table_B

INNER JOIN Table_C

ON Table_B.ID=Table_C.RefID

) AS myTable