I am creating a trigger and understand how to do this if the columns in two different tables have the same name. Ex, I write a trigger that if a new record is added in the salary column for table A to take the salary from table A and insert that as salary in table B.
What I don't understand how to do, is populate that data into table B if that table has a different column name. So I have table A that the column name is salary and table B where the column name is pastsalary.
How do I write the trigger to be able to do that?
I don't see the difference between the two.. Your insert statment would just reflect the column you want to insert into:
insert into tableB(pastsalary) values (:new.salary);
Related
I am trying to write a BigQuery to get distinct values from one table say ABC
select distinct name from dataset.abc
1
2
3
4
now I want to insert these values to another table say XYZ, which also contains column as name and another column as company.
Note: the company column can be duplicated as I want the to have all 4 rows against every company to be inserted in table ABC.
I need a BigQuery to do this dynamically instead of updating every dataset manually every time.
P.S. Sorry if my question is not as per standards as this is the first time I am posting on Stackoverflow.
I have this newly created table in SQL Server with 3 columns ID, Name, Source.
Basically this table will be populated with data from other different tables, each specifically taking in their record IDs and record Names. I believe this can be easily achieved with an INSERT INTO SELECT statement.
I would like to find out on how to populate the Source column. This column is supposed to indicate which table the data came from. For example, Source in table A has 3 records, which I then copied the ID and Name columns from this table, and put it into my destination table.
At the same time, the 3 new records will have their Source column set, indicating it came from Table A. Then I will proceed to do the same for other tables.
You can use the constant string as follows:
INSERT INTO your_table
SELECT id, name, 'TableA' as source
FROM tableA
I have three tables:
Table A has columns name, id, nationality
Table B has a column name
Table C has a column id
I was wondering if it is possible to extract from Table A and insert its name column into Table B and id column into Table C in one single SQL query? Not in two separate queries.
I know it is possible in Oracle.
I am using Teradata, which supports all SQL queries.
It is not possible to do in a single query. One table at a time only. Use a Transaction or a Stored statement to query the data and then two more queries to insert the data in each table. This does save you making the query for both inserts but you cant do an INSERT on two tables.
This is an extension of Insert values into table B based on column from table A?
From the above question, let's say in both the User_Permissions and Users table there's also 2 more columns recorded for audit purposes: a version column and a transaction_version column. When inserting the new row (which is based on a row from the Users table) into the User_Permissions table I need to take the value of the 2 columns in the Users table, increment it by 1 and then insert it into the User_Permissions table.
Is there an easy SQL query to do this? I suspect it'd have to do with another inline select but am unsure of the syntax.
You could use Triggers after insert to perform the needed updates
I have a SSIS package that pulls data from reports and loads into SQL-Server. I need to write another package that tests the "Age" column of that table. IF the age is greater than 20 then I need to INSERT that record into another table while keeping the same record in the same table.
What is the best approach to do this while keeping the logic withing the SSIS objects?
Assuming both tables have same schema,
Insert into targetTable
Select *
From sourceTable
Where Age>20