INSERT INTO audit table when SELECT from table - sql

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 *

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

Select from inherited table

I have this inheritance in my database and I need to use a SELECT query and INSERT query to it.
I can't seem to pull this off.
It's about the Item and it's inheritances.
This could be helpful?
INSERT INTO table2
SELECT *
FROM table1
WHERE condition;
I don't know if i've understood your question but, you want to insert some values into a table selecting from an another table.

Insert result set from UNPIVOT into table

I am trying to find the syntax for inserting the results from an UNPIVOT statement into an existing table in the database?
simplest answer which works for any SELECT including UNPIVOT would be...
INSERT INTO MyTable
SELECT statement
However, this does require that your destination tables columns match your SELECT statement columns.
Although you can get around this limitation with...
INSERT INTO MyTable (column1, column2....)
SELECT statement

INSERT to a table with a sub query

Can I do this in SQL 2005?
SELECT 'C'+inserted.exhid AS ExhId,inserted.exhname AS ExhName,inserted.exhid AS RefID INTO mytable FROM inserted
WHERE inserted.altname IS NOT NULL
It won't work if the table exists, but will create the table if it is non-existent. How do I get it to insert into an existing table?
like this
INSERT INTO mytable
SELECT 'C'+inserted.exhid AS ExhId,inserted.exhname AS ExhName,
inserted.exhid AS RefID FROM inserted
WHERE inserted.altname IS NOT NULL
you also don't need the aliases in this case
SQLMenace's answer is correct. But to add to it, I would suggest that it is a good practice to explicitly list your columns that you are inserting into in the event the table structure/column order ever changes, that way your proc stands a better change of working consistently.
INSERT INTO mytable (
ExhId,
ExhName,
RefID)
SELECT 'C'+inserted.exhid,
inserted.exhname,
inserted.exhid
FROM inserted
WHERE inserted.altname IS NOT NULL
To insert into an existing table, use INSERT INTO instead of `SELECT INTO