Combining INSERT SELECT with constants - sql

I'm trying to insert records into a table where one of the columns comes from another table.
The other two columns are the same for each record.
All three columns are keys.
I'm trying this embedded INSERT SELECT which I see is not allowed?
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C)
VALUES (1,(SELECT COLUMN_NAME FROM TABLE) ,2)

Your syntax is off - this is the correct syntax for this:
Insert Into Table
(Column_A, Column_B, Column_C)
Select 1, Column_Name, 2
From OtherTable

I would post a comment but I don't have enough 'status' yet.
The insert below works in PostgreSQL. What database are you using?
insert into people_experiences (qty, created_at, updated_at, person_id, experience_id) values (1, now(), now(), (select id from people where first_name='Joseph'), (select id from experiences where experience='MS'));
INSERT 0 1

We can even use this
Insert Into Table
(Select "value-1" as column, "value-2" as column, ot.* from otherTable ot)
for two column i want constant values and rest should be use as it is.
Note: Insert select will not allow virtual column.

Related

Insert Values Into SQL from a List OR Array Values

I need to insert values into a table with from another select statement with multiple results. However, it is not like I can do it one by one. Is there any method that more efficient to insert values into the table?
SELECT DISTINCT KID FROM K_Table
KID
1
2
5
7
15
...
From the First DISTINCT SELECT Statement, it will select the multiple KID results which the results might be up to 100 rows.
INSERT INTO K_Table VALUES(Listof(KID)[0],'Default','ABC',GETDATE()),
(Listof(KID)[1],'Default','ABC',GETDATE()),
(...)
Is that able to get the result from a SELECT statement and make it as array OR List for insert values purpose?
You can use insert into .. select.. directly as follows:
INSERT INTO K_Table
Select distinct KID,'Default','ABC',GETDATE()
From k_table;
But, I dont know why you want to add data to same table.
You can use INSERT with SELECT in one statement
INSERT INTO K_Table(Kid, Col1, Col2, Col3) SELECT DISTINCT Kid, 'Default', 'ABC', Getdate() FROM K_Table

insert into value in select statement mutliple values

I try to insert into table with 1 column is (select from table).
I should copy all the id to table1 with 1 column is (select from table )
This not working:
insert into table1 (id,resoucrce,rate) values ((select id from table2),0,0)
I want to do something like that insert all the id from table to another table with default values.
Use insert . . . select:
insert into table1 (id, resource, rate)
select id, 0, 0
from table2;
For copy the content of one table to another table within the same database use this :-
INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
or your query like that :-
Insert into table1 (id, resource, rate) select id, 0, 0 from table2;
You should focus "values" keyword;
values ((select id from table2),0,0)
When you use values(, , ,) you have to specify value columns. But you're trying to pass both resultset and single values together. That's why you get an error. You should only pass single values...
If its clear for you; you can easily find the correct sql syntax.

SELECT values of an INSERT INTO statement

I need to select a few columns from a table1. I need to insert only one of these columns as well as some arbitrary hard coded data and insert it into table2 while also getting the original select statement back.
Basically I would like to get the results of my INSERT INTO statement instead of the "(1 row(s) affected)" that I get in SSMS.
Is there a way to do this?
Here is a SQLFiddle:
http://sqlfiddle.com/#!3/e9beb/3
Those records will insert just fine. However, I want the results of my SELECT statement to come back to me so that I can do it all at once without multiple reads or trips. Is this possible?
You can use the OUTPUT clause:
INSERT INTO Table2
OUTPUT inserted.*
SELECT Phrase, 'This is an automatic note by the system', GETDATE(), 1
FROM Table1
Use a batch statement and store the intermediate results in a table variable:
DECLARE #intermediate TABLE (
col1 type,
col2 type,
col3 type
);
INSERT INTO #intermediate VALUES ( ... );
INSERT INTO destinationTable SELECT * FROM #intermediate;
SELECT #intermediate;
If using this from code you can have all of this in a single command-text string.
Have you tried something like this:
INSERT INTO Table1 (
SELECT Phrase, Notes, GetDate, 1
FROM Table2
UNION
SELECT Phrase, 'This is an automatic note by the system', GETDATE(), 1
UNION
)

adjusting the insert into SQL

I got this already working;
INSERT INTO TermsFinal
(old_classification, count, new_classification, old_term,new_term)
SELECT old_classification , Count(seed) AS count , new_classification, old_term, new_term FROM TermsTemp
GROUP BY old_classification
ORDER BY count DESC
There is one more field in the TermsFinal called SOURCE_TABLE which TermsTemp does not have.
I would like to populate that field too. I already got the $source_table value. I tried this but dis not work.
INSERT INTO TermsFinal
(SOURCE_TABLE,old_classification, count, new_classification, old_term,new_term)
'{$SOURCE_TABLE}', SELECT old_classification , Count(seed) AS count , new_classification, old_term, new_term FROM TermsTemp_TEMP
GROUP BY old_classification
ORDER BY count DESC
How do you add that value into the SOURCE_TABLE field of the TermsFinal while executing the insert into statement in one go?
and the other puzzling thing to me here, how come my first SQL insertinto works without the SQL keyword VALUES. This page http://www.w3schools.com/sql/sql_insert.asp teaches that VALUES part is needed!
You can put string (or any other type ) constant into select, for example
select 'string' as const_str , field1 from table1 will return 2 columns , the first will have "string" text for all rows. In your case you can do
INSERT INTO TermsFinal
(SOURCE_TABLE,old_classification, count, new_classification, old_term,new_term)
SELECT '{$SOURCE_TABLE}', old_classification , Count(seed) AS count , new_classification, old_term, new_term FROM TermsTemp_TEMP
GROUP BY old_classification
ORDER BY count DESC
There are several ways to insert data into a table
One row at a time. This is where you need the values keyword.
Insert into TableA (Col1, Col2,...) values (#Val1, #Val2,...)
You can get the id using select ##identity (at least in ms sql), if you have auto identity
on. This is useful when you need the ID for you next insert.
From select (what you're doing)
Insert into tableA (Col1, Col2)
Select Val1, Val2 from TableB
or insert a hard coded value and values from two separate tables
Insert into tableA (Col1, Col2, Col3, Col4)
Select 'hard coded value', b.Val1, b.Val2, c.Val1
from TableB b join Table c on b.TableCID=c.ID
Now you can insert more than one row at once.
Select into
This method ends up creating a new table from a query, and is great for quick backups of a table, or part of a table.
select * into TermsFinal_backup from TermsFinal where ...

Insert values from another Table in sql server 2008

I have to copy all column data to another table.I have created a new blank table.How to insert the values in it.I am avoiding writing the column name manually because it contain 35 column name in it.Sequence & name of column are same in both the table..?
If the tables have the same columns and types, just do;
INSERT INTO table2 SELECT * FROM table1;
Demo here.
use following stcript:
INSERT INTO "table1" ("column1", "column2", ...)
SELECT "column3", "column4", ...
FROM "table2"
for more information see:
http://www.1keydata.com/sql/sqlinsert.html
Create table2 with columns and datatype for each column. If the columns match up exactly on both tables, then insert into table2 from table1
Create table table2(
column1 datatype,
column2 datatype,
column3 datatype,
column35 datatype
}
INSERT INTO table2
SELECT * from table1
create table2
insert into table2
select * from table1
Please find my verion . i had same column name in both tables
INSERT INTO first_table
(column_1,
column_2,
column_3,
column_etc)
SELECT tab2.column_1 AS column_1,
10 AS column_2,
Getdate() AS column_3,
'some_text' AS column_etc
FROM second_table tab2 (nolock)
insert into dbo.FolderStatus
(
[FolderStatusId],
[code],
[title],
[last_modified]
)
select
[code],
[code],
[title],
[last_modified]
from dbo.f_file_stat