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....
Related
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.
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
I have two tables that need to have an append and update query run regularly. Each time I run these queries they cause the receiving table to begin numbering where the new records left off. How can I get tbl1 to increment up from 1 by ones and tbl2 to increment up from 1000000 by ones even after the update/append?
The PK I'm using it the default auto number field.
tbl1: Updated by local users in real time
tbl2: Imported copy of tbl1, remote users download it, work off of it then update tbl1 using the update and append queries below.
INSERT INTO tbl1 ( Field1, Field2 )
SELECT tbl2.Field1, tbl2.Field2
From tbl2;
UPDATE tbl2
INNER JOIN tbl1
ON tbl2.ID = tbl1.ID
SET tbl1.Field1 = IIf([tbl2].[Field1] Is Null,[tbl1].[Field1],[tbl2].[Field1]),
tbl1.Field2 = IIf([tbl2].[Field2] Is Null,[tbl1].[Field2],[tbl2].[Field2])
;
I have the remote users each assigned a PK seed number far enough apart that they will never overlap.
ALTER TABLE Table ALTER COLUMN Field COUNTER(1000000,1);
ALTER TABLE Table ALTER COLUMN Field COUNTER(2000000,1);
Sorry for the long question/post but need some help as I've been searching for several days but havent found anything that helps. Seems like it should be easy but..here goes
I have table1 in my (Access 2010) database that has exising records. I have another table2 that after I run a query, it first deletes the data in table 2, then imports new records into that table. I run the import into table 2 on a semi regular basis but have yet to copy all those records into table 1 successfully.
I need to copy only the records from table 2 to table 1 if the records don't already exist in table1. So, each time the query or vba code would run, it would be continuing to grow table 1 without duplicating existing data.
To clarify further, it's data from the Outlook GAL so each time table2 imports that data (lname,fname,phone,email) it needs to be added to table1, but only if it doesn't already exist in table 1.
I have a small start of SQL but cannot get it to work properly because I'm not sure how to add the other fields into this SQL statement properly (unfortunately I don't know a whole lot about SQL or creating an append query):
INSERT INTO [Current] ( FirstName )
SELECT DISTINCT tblglobaladdresslistimport.First
FROM tblglobaladdresslistimport LEFT JOIN [Current] ON tblglobaladdresslistimport.First = Current.FirstName
WHERE Current.FirstName Is Null;
How about this :
INSERT INTO [Current](FirstName, LastName, Phone, Email)
SELECT DISTINCT
tblglobaladdresslistimport.First
, tblglobaladdresslistimport.Last
, tblglobaladdresslistimport.Phone
, tblglobaladdresslistimport.Email
FROM
tblglobaladdresslistimport LEFT JOIN [Current]
ON tblglobaladdresslistimport.First = Current.FirstName
AND tblglobaladdresslistimport.Last = Current.LastName
AND tblglobaladdresslistimport.Phone = Current.Phone
AND tblglobaladdresslistimport.Email = Current.Email
WHERE Current.FirstName Is Null
AND Current.LastName Is Null
AND Current.Phone Is Null
AND Current.Email Is Null;
Adjust column names if I guessed it wrong. That assumed that you don't have primary key, so data in tblglobaladdresslistimport considered already exists if there is a row in Current having same value for all columns.
I have this table:
Table1:
id text
1 lala
And i want take first row and copy it, but the id 1 change to 2.
Can you help me with this problem?
A SQL table has no concept of "first" row. You can however select a row based on its characteristics. So, the following would work:
insert into Table1(id, text)
select 2, text
from Table1
where id = 1;
As another note, when creating the table, you can have the id column be auto-incremented. The syntax varies from database to database. If id were auto-incremented, then you could just do:
insert into Table1(text)
select text
from Table1
where id = 1;
And you would be confident that the new row would have a unique id.
Kate - Gordon's answer is technically correct. However, I would like to know more about why you want to do this.
If you're intent is to have the field increment with the insertion of each new row, manually setting the id column value isn't a great idea - it becomes very easy for there to be a conflict with two rows attempting to use the same id at the same time.
I would recommend using an IDENTITY field for this (MS SQL Server -- use an AUTO_INCREMENT field in MySQL). You could then do the insert as follows:
INSERT INTO Table1 (text)
SELECT text
FROM Table1
WHERE id = 1
SQL Server would automatically assign a new, unique value to the id field.