Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert combine (value and select)

There are ways of inserting data into table:

insert into MyTable (ColA, ColB, ColC) values ('A', 'B', 'C')

insert into MyTable (ColA, ColB, ColC) select  colAA, colBB, colCC from MyTable2

Is there way to use insert into MyTable (ColA, ColB, ColC) select colAA, colBB, colCC from MyTable2 but instead of inserting for example colAA value into colA I woulkd like to insert there always 1.

THanks for help

like image 846
gruber Avatar asked Feb 14 '11 06:02

gruber


People also ask

Can we use insert and select together?

You can use a select-statement within an INSERT statement to insert zero, one, or more rows into a table from the result table of the select-statement. The select-statement embedded in the INSERT statement is no different from the select-statement you use to retrieve data.

Which is faster insert into or select into?

INTO' creates the destination table, it exclusively owns that table and is quicker compared to the 'INSERT … SELECT'. Because the 'INSERT … SELECT' inserts data into an existing table, it is slower and requires more resources due to the higher number of logical reads and greater transaction log usage.

What is the difference between insert into to select?

INSERT INTO SELECT inserts into an existing table. SELECT INTO creates a new table and puts the data in it.


1 Answers

Just add a constant into the SELECT list

INSERT INTO MyTable
            (ColA,
             ColB,
             ColC)
SELECT 1,
       colBB,
       colCC
FROM   MyTable2  
like image 121
Martin Smith Avatar answered Nov 16 '22 02:11

Martin Smith