UPDATE TableA.FK VALUES TableB.PK WHERE TableA.Column = TableB.Column - c#

I'm trying to merge tables from different databases, ServerDB and ClientDB and save to ClientDB where the client's tables hold the master's tables records plus any records the user might add. On the other hand server tables could also be updated (new records inserted).
The database has relationships. The column in TableA I want to INSERT INTO values is a ForeignKey linking to TableB's PrimaryKey which is an auto-incremental column.
I'm saving all records from both databases in a merged dataset, and due to database design restrictions, I need to clear both tables on the client before inserting the merged tables from the dataset.
I first update TableB (the one with the PrimaryKey auto-increment column), but now the values of this column have nothing to do with the ForeignKey on TableA, so I update TableA and temporary inserting in the ForeignKey column the value of the first record of TableB's PK. Now I need to update TableA foreignKey column with the correct values from TableA PK column. Theres also a third column on each table that have the same values.
What the syntax of the sql statement should be? If I don't make much sense let me know and I'll post a better description.

It was a confussing question but i think you are talking about inserting values from table 1 to table 2 where table 1 value is equal to table 2 values the sql query for this operation is
INSERT INTO emp (empno,ename)
SELECT t2.deptno,
t2.dname
FROM dept t2
LEFT JOIN emp t1
ON t2.deptno = t1.deptno
in this query table 1 (emp) will insert 2 values into columns(empno and ename) from
table 2 (dept) and join is on (deptno) which is present in both tables.
You can further ask if this was not helpful.

Thank you all for your help. After struggling for a while with the "OledbException Operation must use an updateable query" I found out a solution in a similar topic: SQL Update woes in MS Access - Operation must use an updateable query
Thats the query that did the trick:
UPDATE DISTINCTROW PlaylistsSongs
INNER JOIN PlaylistsNames ON PlaylistNames.PlaylistName = PlaylistsSongs.PlaylistName
SET PlaylistID = PlaylistNames.ID

Related

Update Join Query not Updating Record in The Table

In my attendance table I want to update a record with S_ID and C_ID from StudentCourse bridge table but its not updating.
https://imgur.com/a/7ZBItur
Its working when I use it to select and display 2 columns from the StudentCourse table and 1 column from the Attendance table but it doesn't work when I use it to update the attendance table which for now is empty
UPDATE Attendance
SET S_ID = sc.S_ID,
C_ID = sc.C_ID
FROM Attendance a
left outer join StudentCourse sc ON a.S_ID = sc.S_ID
WHERE sc.S_ID=2 and sc.C_ID=2
There are two actually four tables:
Student(S_ID(primary key))
Course(C_ID(Primary key))
StudentCourse((S_ID,C_ID(Foreign keys))Bridge table)
and attendance table with (S_ID,C_ID(Foreign keys))
What I am doing is displaying data on datagridview by joining tables and selecting S_ID,S_Name,C_ID and Pre_Abs (Attendance table column)columns from all these tables.
Now, I want to insert the info present in datagridview to the attendance table when I click on button.
I have done this already with simple insert query to attendance table by using datagrdview.rows[i].cell[2] property.
I want to know if there is any better idea to do this so that I can use JOIN instead of using datagridview property with for loop.
For now my attendance table is empty while Student, Course, and StudentCourse tables are filled with the data.
What I want is to display record(S_ID, C_ID) from studentCourse table and (Pre_Abs) from Attendance table and when I submit the attendance; I want it to store Pre_abs record against each S_ID, C_ID in the attendance table.
I don't think I can explain it any further.
You have declared alias for the table Attendance. So you should use alias reference before the column name as the same column name also available in other table. Can you please try this-
UPDATE a
SET a.S_ID = sc.S_ID,
a.C_ID = sc.C_ID
FROM Attendance a
LEFT OUTER JOIN StudentCourse sc ON a.S_ID = sc.S_ID
WHERE sc.S_ID=2 and sc.C_ID=2
I have also doubt about you where condition as you are Update table Attendance but filtering records on table StudentCourse. use of wrong filter can resultant in update all rows in table Attendance. Please be careful.
Hmmm. I do not understand the reason why your Attendance table exists in the first place. Your StudentCourse table already seems to contain info about which students are having which courses. I'll just assume the Attendance table has to deal with registering student attendance for each single lecture/lesson within a course.
Anyway, as #Psi already commented, you seem to initially want an INSERT query to create a record in the Attendance table with data based on information in the StudentCourse table. I guess that you are thinking about it too hard. ;-) You may try something like this:
INSERT INTO Attendance (S_ID, C_ID, Pre_Abs)
SELECT S_ID, C_ID, Pre_Abs
FROM StudentCourse
WHERE S_ID = 2 AND C_ID = 2
However, it is currently unclear how field Pre_Abs should be filled... You try to get it from table Attendance, but that would seem to be invalid.
Also, when creating INSERT-queries, make sure that the source data values (in the SELECT or VALUES clause) are in the same order as the target fields (in the INSERT clause). If the field/value order doesn't match, the data gets mixed up (if the query does not fail due to data type errors)!
And last but not least: if both Attendance and StudentCourse tables share the same composite key (S_ID, C_ID), you should use that full key when joining related records of those tables:
FROM
Attendance AS A
LEFT JOIN StudentCourse AS SC ON
SC.S_ID = A.S_ID AND
SC.C_ID = A.C_ID
Hope this helps a little...

Query to insert rows from CSV to MySQL

I have 2 tables employees and job in a MySQL server. I have 2 CSV files with the same schema as the two tables. Something as below.
Job table(MySQL)
Job(CSV)
Employees(MySQL)
Employees(CSV)
I need to insert the rows from CSV into the MySQL tables. Note that the Id fields in both the tables are on AUTOINCREMENT. I am aware that MySQL does not support merge query and instead has INSERT.. ON DUPLICATE UPDATE. Now the issue with using this is that the Primary keys i.e., the Ids are definitely duplicate but I need them to be inserted as new rows with new Ids. For example, the Admin role should have the Id as 3 and so on. Also, as you can see, the job table is being referenced by the employees table. So the the JobId column in the table should be updated with the respective values. I am not sure how this can be achieved.
Any help appreciated!

Update a field 'version' into a table

I have this situation:
I have two tables:
Table A
Staging_Table A
Both tables contain those common columns:
Code
Description
Into Table A I also have a column Version which identifies the last version of corresponding column Code.
My problem is how to update the column Version once a new Description is stored for the same Code (I fill up the Staging_Table with a bulk Insert from C#. I have a flow of data that change once a week).
I need to insert the new row into Table A which contain the same Code, but a different Description, without deleting the old one.
I insert the rows from Staging table to table A with MINUS operation and I have this mechanism within a stored procedure because I also fill up the staging table with a Bulk Insert from C#.
The result I need to obtain is the following:
TABLE A:
Id Code Description Version End_date
-- ----------------- ------- --------
1 8585 Red Car 1 26-mag-2015
2 8585 Red Car RRRR 2 01-giu-2015
How can I do that?
I hope the issue is clear
If I understand correctly process work like that:
1. Data is loaded to staging table Staging_table_A
2. Data is inserted from Staging_table_A itno Table_A with additional column version.
I would do:
with cnt as (select count(*) c, code from Table_A group by code)
Insert into Table_A (select sta.*, nvl(cnt.c,0) + 1 as version
from Staging_table_A sta left outer join cnt on (sta.code = cnt.code));
This is based on condition that in Table_A versions contains no duplicates.

SQL batch insert, avoid duplicates, no PK

I was given a task to insert over 1000 rows with 4 columns. The table in question does not have a PK or FK. Let's say it contains columns ID, CustomerNo, Description. The records needed to be inserted can have the same CustomerNo and Description values.
I read about importing data to a temporary table, comparing it with the real table, removing duplicates, and moving new records to the real table.
I also could have 1000 queries that check if such a record already exists and insert data if it does not. But I'm too ashamed to try that out for obvious reasons.
I'm not expecting any specific code, because I did not give any specific details. What I'm hoping for is some pseudocode or general advice for completing such tasks. I can't wait to give some upvotes!
So the idea is, you don't want to insert an entry if there's already an entry with the same ID?
If so, after you import your data into a temporary table, you can accomplish what you're looking for in the where clause of a select statement:
insert into table
select ID, CustomerNo, Description from #data_source
where (#data_source.ID not in (select table.ID from table))
I would suggest to you to load the data into a temp table or variable table. Then you can do a "Select Into" using the distinct key word which will removed the duplicated records.
you will always need to read the target table, unless you bulk load the target table into a temp table(in this point you will have two temp tables) compare both, eliminate duplicates and then insert in target table, but even this is not accurate, because you can have a new insert in the target table while you do this.

C# filtered read of several tables with multiple relations

I have a quite large database with several tables and complex relations and thousands of records. Just to simplify the problem, say I have got a MySQL database as follows:
[table1]
id
table2_id
table3_id
name_id
[table2]
id
name_id
[table3]
id
name_id
[name]
id
text
Then there are relations:
table1.name_id -> name.id
table1.table2_id -> table2.id
table1.table3_id -> table3.id
table2.name_id -> name.id
table3.name_id -> name.id
Tables contain:
50,000 records in table1
10,000 records in table2
5,000 records in table3
25,000 records in name
I need to display table1 in a DataGridView showing in columns text names.
I need a filter to be applied to every table1 request where I can include a name search keyword for each table: table1, table2, table3.
I have been trying manually fill DataSet using DataAdapters and adding DataRelations and found some problems along:
it takes ages and a lot of memory to get DataSet filled with all the data. Obviously I do not need everything at once, I would be happy if I could limit table1 to 5,000 records with a filter applied
considering DataAdapters and DataSets, I could not manage to set WHERE conditions to child rows in order to retrieve filtered table1 data - for example: I want to get a list of table1 where table2 name text = "a" and table3 text = "b"
I would not really want to build 'SELECT ... LEFT JOIN' or 'SELECT .... table1, table2, table3' queries as like I mentioned before, I deal with more than 6 tables with nested relations (eg. a related table is in relation with another one etc.)
I would not want to retrieve all table1 records and then filter them at C# side as like I said before - it takes a long time and there is no need to display all records on the screen. All records MUST be considered only in terms of filtering records then result can be limited to 5,000 items.
I understand using DataSet and DataAdapters may be wrong in this scenario. Any other ideas are very welcome and I am sure will be helpful as well. THANK YOU.
Could someone please point me in the right direction?

Categories

Resources