my google-fu is failing me.
I'm trying to do the following in a more automatic way:
1) select a set of fields from 1 table
select ACCT_ID from MASTER_ACCT where CUST_NBR like '%ABC';
2) use the results of that in a multiple row insert
// for each ACCT_ID in (1)
insert into TOGGLES (FIELD1, FIELD2, FIELD3)
values('abc', '123', ACCT_ID[i]);
Is there a way to execute the 2nd statement for ACCT_ID[i] in each of the ACCT_ID results from the 1st statement?
You would use an INSERT INTO...SELECT statement:
INSERT INTO toggles (field1, field2, field3)
SELECT 'abc', '123', acct_id
FROM master_acct
WHERE cust_nbr LIKE '%ABC';
You might use the below syntax
INSERT INTO target_table[()] SELECT ... FROM ...;
find this link for more details.
Related
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 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
)
What is the query in SQL Server to insert rows with structure from one table to another table?
A couple ways
SELECT INTO
SELECT
field1,
field2,
...
INTO Table1
FROM Table2
INSERT INTO
INSERT INTO Table1
field1,
field2,
...
SELECT
field1,
field2,
...
FROM Table2
Sounds like you want something like this:
INSERT INTO DestTable (Column1,COlumn2)
SELECT Column1, Column2
FROM SourceTable
Here is the documentation for the INSERT...EXECUTE statement that will allow you to do what you need.
Is it possible to do something like this:
INSERT INTO table(col1, col2) VALUES(something_from_another_table, value);
With something_from_another_table being a SQL command? Like, is there something I can do that's equivelant to:
INSERT INTO table(col1, col2) VALUES((SELECT value FROM table2 WHERE id = 3), value);
Yes
INSERT INTO table(col1, col2)
SELECT value1, 'value2' FROM table2 WHERE id = 3
Where value1 is the value from the 'other table' and value2 is a constant that you've included in that select statement.
Try this:
INSERT INTO table(col1, col2)
SELECT table2.value1, value2 FROM table2 WHERE table2.id = 3;
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
take a look especially in the examples.
I would recommend reading full syntax of SELECT, UPDATE, DELETE and INSERT SQL commands to begin with. Then expand to subqueries and DDL.
Go slowly and work out examples.
You definately can. It should work similar as the example below;
INSERT INTO Store_Information (store_name, Sales, Date)
(SELECT store_name, Sales, Date FROM Sales_Information WHERE Year(Date) = 2010)
when you specify the keyword "Values" on the insert statement you are trying to insert just a value. the current way to do that is assigning the value of the "something_from_another_table" into a variable and then, make your insert
DECLARE #ANYVALUE AS VARCHAR(40)
SELECT #ANYVALUE = ANYFIELD FROM table2 WHERE id = 3
INSERT INTO table1 (FIELD1, FIELD2) VALUES(#ANYVALUE, VALUE2)
On this way always will insert one record. the other correct way will insert n record as the where statement can filter.
INSERT INTO Store_Information (store_name, Sales, Date)
(SELECT store_name, Sales, Date FROM Sales_Information WHERE Year(Date) = 2010)
I'm trying to do an Insert operation using iBatis.
INSERT INTO SCHEMA.TABLE
(FIELD1,
FIELD2,
FIELD3)
VALUES
(#field1#,
(SELECT
ANOTHER_FIELD
FROM
SCHEMA.TABLE
WHERE
FIELD4= #field2#),
#field2#)
The inner select query always fails and returns NULL. But if I substitute #field2# with the actual value only in the inner query, it works fine.
Why is iBatis not substituting fieldvalues in the innerqueries?
Any ideas?
The following way using a single sub-query and omitting the VALUES keyword would work with Oracle, please try with iBatis:
INSERT INTO SCHEMA.TABLE
(FIELD1,
FIELD2,
FIELD3)
(
SELECT
#field1#,
ANOTHER_FIELD,
#field2#
FROM
SCHEMA.TABLE
WHERE
FIELD4= #field2#
)
That syntax is invalid for Oracle. Try the following:
INSERT INTO SCHEMA.TABLE
(FIELD1,
FIELD2,
FIELD3)
SELECT
#field1#,
ANOTHER_FIELD,
#field2#
FROM
SCHEMA.TABLE
WHERE
FIELD4= #field2#