How to fire two triggers for two tables at different time to insert some values from both the tables to third table? - sql

I have 3 tables in SQL server with following fields.
Table 1- id, name, age.
Table 2- id,email, Address.
Table 3- id, name, email.
I wish to use two triggers like, when I insert values on Table 1, id and name should insert in Table 3. When I insert values in Table 2, Email should insert in Table 3 and it should insert at id and name position means it should not show NULL values. Name,id and email should insert in one row.

You should make the View for inserting data from two tables to a single table

You should fix the data model so that it is normalized. You wouldn't need a trigger to replicate data into Table 3 if the name and email columns weren't in 2 tables.

Related

Trying to insert into Table A and link Table B and C but add to all if not exist

I have 4 tables:
Table A:
LogID (unique identifier),
UserID (bigint),
LogDate (date/time),
LogEventID (int),
IPID (varchar(36)),
UserAgentID (varchar(36))
Table B:
IPID (unique identifier),
IPAddress (varchar(255))
Table C:
UserAgentID (unique identifier),
UserAgent (varchar(255))
Table D:
LogEventID (int),
LogEvent (varchar(255))
I am trying to write the to Table A but need to check Table B, Table C and Table D contain data so I can link them. If they don’t contain any data, I would need to create some. Some of the tables may contain data, sometimes none of them may.
Pretty much everything, really struggling
first, you do a insert into table B, C, D WHERE NOT EXISTS
example
INSERT INTO TableB (IPID, IPAddress)
SELECT #IPPD, #IPAddress
WHERE NOT EXISTS
(
SELECT *
FROM TableB x
WHERE x.IPID = #IPID
)
then you insert into table A
INSERT INTO TableA ( . . . )
SELECT . . .
SQL Server doesn't let you modify multiple tables in a single statement, so you cannot do this with a single statement.
What can you do?
You can wrap the multiple statements in a single transaction, if your goal is to modify the database only once.
You can write the multiple statements in stored procedure.
What you really probably want is a view with insert triggers on the view. You can define a view that is the join of the tables with the values from the reference tables. An insert trigger can then check if the values exist and replace them with the appropriate ids. Or, insert into the appropriate table.
The third option does exactly what you want. I find that it is a bit of trouble to maintain triggers, so for an application, I would prefer wrapping the logic in a stored procedure.

Inserting data from another table - Oracle SQL

I need to insert data into a media table. The data must have the media id (which is a sequence), format (DVD, VHS) and the movie title id which is a sequence that exists another table named movies. How do I pull the data for the title_id from the movies table into the media table? I'm not quite sure where to start but I've listed the code I have so far for the first 2 columns.
INSERT INTO m_media
(media_id, format, title_id)
VALUES (media_id_seq.NEXTVAL, 'DVD', );
Instead of inserting separate values, use SELECT statement which will fetch data from the movies table. Something like this:
insert into m_media (media_id, format, title_id)
select media_id_seq.nextval,
'DVD',
m.title_id
from movies m
where ... --> condition, if you want to restrict rows returned by that SELECT

Insert into table sum of different columns with same schema

I´ve the following tables
I want to insert the sum of the columns [01],][02],[03]...etc. Into another table with the same schema, even if in a specific table exist only 1 record.
INSERT INTO TABLE1 VALUES AS SELECT * FROM
TABLE;
ALTER TABLE TABLE1
ADD COLUMN SUM_VALUES NUMBER(20)
DEFAULT( SUM(01,02,...N))
Is this what you are looking for or provide extra detail

How to add more rows to an existing DB Table

I'm currently updating an existing DB table.
The Table has 14924 rows, I'm trying to insert new data which is requiring 15000 rows.
When running my Query, I'm getting this error message:
There are fewer columns in the INSERT statement than values specified
in the VALUES clause. The number of values in the VALUES clause must
match the number of columns specified in the INSERT statement.
Is there a way to add the additional 76 rows as needed?
I'm using MSSMS (Microsoft SQL Server Management Studio)
Query I'm running:
Insert INTO [survey].[dbo].[uid_table] (UID)
VALUES ('F32975648JX2','F32975681JX2',..+14998 more)
Should I clear the Column first by setting to NULL
What I'm trying to do is add all the VALUES to the UID column
My Columns are currently set as is:
UID | Email | Name | Title | Company | Address1 | Address2 | DateCreated |
All columns I have set to NULL except for UID, which already contains Values like above. Just need to replace the old values with the new ones. BUt getting error stated above
For inserting more than one value into a column you need to make the Insert statement in this format
Insert INTO [survey].[dbo].[uid_table] (UID)
VALUES ('F32975648JX2'),
('F32975681JX2'),
..+14998 more)
Also note that, The maximum number of rows that can be constructed by inserting rows directly in the VALUES list is 1000. So you have to break the INSERT statement into 1000 rows per INSERT
To insert more than 1000 rows, use one of the following methods
Create multiple INSERT statements
Use a derived table
Bulk import the data by using the bcp utility or the BULK INSERT
statement
Derived table approach
Insert INTO [survey].[dbo].[uid_table] (UID)
select 'F32975648JX2'
Union All
Select 'F32975681JX2',
Union All
..+14998 more)
your problem is in your INSERT statment
An example is
INSERT INTO table (col1, col2, col3,...)
VALUES(valCol1, valcol2, valcol3...)
Ensure that the number of columns (col1, col2, col3...) is the same number that VALUES (valCol1, valcol2, valcol3...) 3 columns and 3 values in this case

using insert into to append to existing table in a remote database

my goal is to select items from a table and append those items into another table located on a remote database on the same server. All columns in both tables match up and are identical. In this case,
I have the tsql:
INSERT INTO db1.dbo.tblitems
SELECT *
FROM db2.dbo.tblitems i2
WHERE i2 = 'import'
i get an error saying:
An explicit value for the identity column in table 'db1.dbo.tblitems' can only be specified when a column list is used and IDENTITY_INSERT is ON.
any ideas why this doesn't work?
thanks in advance
Sounds like there is an identity column in the table. An identity column is a column that is made up of values generated by the database. For example:
create table #TestTable (id int identity, name varchar(50))
insert into #TestTable select 1, 'Will Smith'
This gives the identity column error. You can avoid that in two ways: the first is not to insert the identity column, like:
insert into #TestTable (name) select 'Will Smith'
The second is to use set identity_insert (requires admin privileges):
set identity_insert #TestTable on
insert into #TestTable (id, name) select 1, 'Will Smith'
set identity_insert #TestTable off
In both cases, you have to specify the column list.
I agree with Andomar but a further consideration...
Have you considered the effects of merging these two data sets?
Say I had two identical tables in two databases with this data:
Id Name
1 Bill
2 Bob
3 Bert
Id Name
3 Jenny
4 Joan
5 Jackie
Option 1 of Andomar's would give the girls new IDs. If that ID has been used as a primary key in the table and other tables referenced it as a foreign key then this will break the referential integrity (you will have records pointing to the wrong place).
Option 2 would fall over if there is a unique index on the ID column, which quite likely if it is being used as a key. This is because the two ID values for Bert and Jenny are not unique.
So while Andomar is right in that it will fix the identity insert problem, it doesn't address the issue of why there were identity columns in the first place.
p.s. if this is an issue ask for a solution in a new question.
This might be an issue of permissions. As the server the query is running on cannot determine if the connected user has the permission to insert the data into the destination server/table, it just might not be possible.