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.
Related
I have a table, which has 9 records with id = 1 - 9 (for example, there can be more than 20 ids).
I have one varchar value = 'premium'.
I need to insert these values to another table, after this action I should have 9 records with id from the first table and 'premium' varchar in the second table:
1, 'premium';
2, 'premium';
etc.
How to write the function for SQL?
Are you looking for insert . . . select or create table as?
insert into table2 (id, value)
select id, 'premium'
from table1;
or:
create table table2 as
select id, 'premium' as value
from table1;
Do you want this?
demo:db<>fiddle
INSERT INTO second_table (id, text_value)
SELECT id, 'premium'
FROM first_table;
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
I'm trying to insert a row into a table named table1 if only 'value' exists in table2. The newly created row returns an id and I want to use this id through a WITH query and insert it into a third table named table3.
Here is the sql statement I used.
WITH new_key AS (
INSERT INTO table1(name, value)
SELECT 'test', value
FROM table2
WHERE value = 'some_value'
RETURNING id
)
INSERT INTO table3(table1_id)
VALUES (new_key);
The INSERT query in the WITH query works and returns an id. Unfortunately the whole statement returns "new_key column does not exist".
You need to select the value from the CTE:
WITH new_key AS (
INSERT INTO table1(name, value)
SELECT 'test', value
FROM table2
WHERE value = 'some_value'
RETURNING id
)
INSERT INTO table3(table1_id)
SELECT id
FROM new_key;
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
)
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 ...