insert data from one mysql table to another - sql

i am trying to run this SQL query to copy data from one table to another but it shows zero rows returned:
INSERT INTO ticket_updates
VALUES
(
SELECT *
FROM ticket_updates2
WHERE sequence = '4715'
)

You cannot put a select statement into a values method.
Your syntax should look like this:
INSERT into ticket_updates(all,my,columns)
select all,my,columns from ticket_updates2 where sequence = '4715'

Related

Trying to insert values into a table where some values will be select statements whereas others will be hardcoded

My select into query would start like this:
insert into camrule (HCHARGECODE, htenant, dtfrom, dtto, IESTIMATETYPE, destimated, imaxpermo)
My hchargecode would be a hardcoded value of 174, my htenant would be based on a select statment (ex. select htenant from tableX), and so on. How can I hardcode the columns and have the other values from the select statments added to my camrule table?
Also, this is for multiple rows, not just a single row to be inserted.
I've tried creating a temp table with the hardcoded values, but am getting an error. I was hoping I could insert the columns from this temp table into my camrule table.
error message
Use the Values keyword to get this done...
insert into camrule (HCHARGECODE, htenant, dtfrom, dtto, IESTIMATETYPE, destimated, imaxpermo)
values(174,(select htenant from tableX), and so on...
This way allows you to select values from different tables, as opposed to just adding hard-coded columns to a Select statement from a single source table.
You can do as simple as:
insert into camrule (
HCHARGECODE, htenant, dtfrom, dtto,
IESTIMATETYPE, destimated, imaxpermo
)
select
174, htenant, dtfrom, dtto,
IESTIMATETYPE, destimated, imaxpermo
from tableX

Generate insert column based on select columns

I have a scenario, where 100's of select statements sql's are in one metadata table or some text file.
Need to insert all sql results into one specific table. (master table has col1, col2,col3 .... 200columns )
problem im facing(ORA-00947) is every select statement has different number of columns.
.. i need to generate INSERT PART.
CASE 1 : INSERT INTO (COL1,COL2,COL3) <<this select part comes from file/variable>>
CASE 2 : INSERT INTO (COL1) <<this select part comes from file/variable>>
CASE 3 : INSERT INTO (COL1) <<this select part comes from file/variable>>
have to figure out how many columns are in select part then generate INSERT part.
.
Thought of create as select but problem is some select statement has max(col) without alias so it will fail.
This is too long for a comment.
If you are storing SQL in a table, then you are constructing your query dynamically. So, update the table and list the columns that you want.
You could then construct the inserts as :
insert into master_table (<column list here>)
<select here>;
Both the select and column list would come from the table.
By far the easiest is to create a view for each SELECT statement. Then you can query the USER_TAB_COLUMNS view on the view name and get the column names.
Best regards,
Stew Ashton

INSERT INTO audit table when SELECT from table

Is that applicable to use INSERT inside SELECT statement, in order to insert some data whenever we select. the scenarion is i need to audit the select operations so whenever we run SELECT statemnt we need to insert into a nother table, i have all the code for the select operation all what i need is to find a way to add INSERT statement inside SELECT statement
EX
SELECT *, (
INSERT INTO auditTable(ID, CREATEDINFO) VALUES ( :v0, :v1)
) FROM mainTable;
As suggested in the comments, there may be better ways to do what you are asking, but for the example you have shown, you want to do something like this:
INSERT INTO auditTable(ID, CREATEDINFO)
SELECT *
FROM mainTable;
Note: I'm assuming there are only two rows in mainTable as your query suggests, otherwise you should specify which rows you are selecting rather than using *

How Select Data and Insert data to another table using same stored procedure

I want to select some data inside a stored procedure and I want to insert all selected data to another table inside the same stored procedure:
SELECT * FROM #TempTable
then insert into another table like this:
SELECT #v_Maxno = isnull(max([A_TEST].ROW_NUM) + 1, 1)
FROM [A_TEST]
INSERT INTO [dbo].[A_TEST] (ROW_NUM,A, B, C)
VALUES (#v_Maxno,TempTable.CL_A, TempTable.CL_B, TempTable.CL_C)
I have to calculate row number Manually if any thing like loop so I will manage to achieve this.
Not finding the best way to doing this ..........Using SQL Server
instead of using values keyword, directly use select.
like this
insert into Table1(RowNum,Col1,Col2)
select ROW_NUMBER() OVER(ORDER BY Cola),Cola,colb from table2

Returning inserted rows in presto

I'm am inserting rows from one table to another using presto, and would like the inserted rows to be returned. Something like this...
Something like:
insert into animals
select * from arriving_animals
returning *
It appears returning is not supported in presto. Any advice on how to return the rows that are inserted within the same statement?
Create a "temporary" table with the rows to insert:
CREATE TABLE tmp_insert AS
SELECT * FROM arriving_animals
Then insert them:
INSERT INTO animals
SELECT * FROM tmp_insert
You can now read the inserted rows and drop the table when finished.