The following problem is simplified -
I have 3 tables, table1, mapping_table and table2.
table 1 will include 3 columns - first name , last name and date.
table 2 will include 4 columns - id (that gets it value from a sequence, first name , last_name_in_germen and date).
mappingTable that will include 2 columns (last name and ast_name_in_germen).
In addition -
date is nullable in table1 but have to has some value (like the date of today) is table2.
The problems are -
The new table (table2) will have columns that exist in the original one (first_name), columns that will need to make some basic transformation like mapping (last_name) and adding a default value (date) and of course to use the sequence (id).
I was thinking about using a procedure with a loop but I don't know how to insert a row to the new table.
This sound like a standard INSERT-SELECT with a join ?
insert into table2
select my_sequence.nextval,
table1.first_name,
mappingTable.last_name_in_germen,
table1.date
from table1,
mappingTable
where table1.last_name = mappingTable.last_name
Related
I have 2 tables name Table A and Table B
"Table A has 10 columns named ta1, ta2, ta3...ta10"
"Table b has 2 columns name id (PK,AI) and tb1"
My requirement is to combine "ta3" and "ta4" from table A initially,
or it will check for the "tb1" column whether any same data is available, if available do nothing else insert the new data.(if a new row is added in the ta3 or ta4, it should be added to the 2nd table column named tb1)
I have checked other functions in the for combining the data from ta3 and ta4 by using the "unnest" function, but if I use that, I'm getting stuck in the insert, as it will insert on each query rather than checking whether any data is there or not.
any solution on this legends.
simply I just want to combine 2 column from 1st table and store it in a column on 2nd table, but in 2nd table's column it should be only distinct values taken from both of those 1st table columns. this should automatically check for the values in the column of 2nd table and compare that with the new one and if there is change the insert else neglect. I hope my question is clear.
Table1
Table 2
new table-1
Expected table-2
SQL
i need to write a procedure that converts an old number into a new one.
I have 2 tables:
1. conversion table - with 2 columns: old number and new number.
2. Table with all old numbers.
How do I make a loop that passes each record in Table 2 and updates the new number according to Table 1?
An UPDATE statement will change all of the old values in Table2 that have matching new values in Table1.
UPDATE t2
SET OldValue = t1.NewValue
FROM
Table2 AS t2
JOIN
Table1 AS t1
ON t2.OldValue = t1.OldValue;
As noted in the comments, this is a set-based approach, not a loop.
So I have 2 Tables in one Database (Table1 and Table2). What I want to do is to get the last generated ID (which is primary key) from the first table (Table1) and add it to another table (Table2).
For example. The Last generated ID from Table1, column NRRENDOR is 25 (I have deleted the rows that's why it shows 22, it is primary key). If I add a row to Table1 it will generate number 26 on column NRRENDOR (First picture). But when number 26 is added to column NRRENDOR from Table 1, I want it to be added to Table2, column NRD too (Second Picture).
You should use a sql-query like the one below after inserting the new element to the first table.
Insert into table2 (id) values(Select top 1 ID from table1 order by id desc)
This should work in sql-server 2008 and newer.
From MySQL Reference Manual, 20.6.14.3 How to Get the Unique ID for the Last Inserted Row
INSERT INTO NRRENDOR (auto, field) VALUES(NULL, 'value');
generate ID by inserting NULL
INSERT INTO NRD (id, field) VALUES(LAST_INSERT_ID(), 'value');
use ID in second table.
Or you can get the last insert id manually executing the following query immediately after the INSERT into NRRENDOR
SELECT last_insert_id()
and use it later in your second INSERT query for NRD.
I have 2 table A and table B; table B is linked to table A through a foreign key.
TABLE A has a structure somewhat like this
PK Id
DeliveryChannelValue
DeliverychannelId
Date time
Table B has this structure
PK Id UniqueIdentifiers
Date time
FK tableA id
Now in a stored procedure, I get unique identifiers as comma separated value, so based on the number of items in that list, I have to create the same number of rows in table A and in table B.
If the number of items in comma separated value is 3, then there will be 3 rows to be inserted into table A and 3 rows into table B. I am trying to avoid a cursor.
Please suggest efficient way to do this.
You can use this CodeProject project split function to separate the values, and then use a known DateTime stamp to keep the tables in sync. This assumes these values aren't constantly updating which could cause a DateTime duplication issue: if that's the case, you'll need to use a add a GUID value in place of the YOURDATE field, below:
DECLARE #DATESTAMP DATETIME = GETDATE()
INSERT INTO TABLE_A (ID, YOURDATE)
SELECT item, #DATESTAMP
FROM dbo.[FN_SPLIT](#yourinputstring)
GO
INSERT INTO TABLE_B(YOURDATE, TABLE_A_ID)
SELECT #DATESTAMP, ID
FROM TABLE_A
WHERE YOURDATE = #DATESTAMP
GO
I have doubt to mount a query to update and / or add data in a table.
I have a table1 that will be supplied with the following rules:
The table1 have the same fields of table2 + status field to show if the register is active ("A") or inactive ("I").
For each record in table1, check if there are new records in table2
If the record exists in table2 and the record doens't exist in table1, includes the information in table 1 - all the fields of that record from table2 + character "A" (active) in the status field.
If the record exists in table2 and the record exists in table1 too, I have to compare id_register from table2 with the id_register from table1
If any data in this row is different, I have to insert a new row in table1 - all fields from the table2 + character "A" (active) in the status field -, AND the line that was different in table1 will be update with a character "I" (inactive) in status field;
If the record was deleted in table2, this row will receive character "I" (inactive) in table1 status field. (Deleted Record = I have to verify the records that exists in table1 and doens't exists in table2)
The records that have no changed will not be updated.
The tables:
table1
id_register
name
address
phone
email
date
value
status
table2
id_register
name
address
phone
email
date
value
Thank you!
It looks to me like you are trying to log changes to your tables. This is quite doable using triggers but I would suggest against that for a couple reasons. First, your most recent record will always be your active one in table 2, so using a timestamp avoids the update.
Secondly rather than building a solution yourself, I would recommend a module I have used in the past, namely tablelog. It will be much simpler than trying to re-implement it all yourself. Also more eyeballs and all of that....