Update rows on to tables in mysql - c#

I need to update the rows of two tables (song & words).
How can I do it using the same query?
Here's my first update:
string query2 = "Update myproject.song set house_number = '" + first.Length + "';";

MySQL permits you to multiple tables as shown here:
MySQL update syntax documentation
UPDATE song,artist SET song.title=a,artist.song_count=b
WHERE artist.id=song.artist_id;
In this example, song, and artist are the two tables with rows, title and song_count respectively. The WHERE clause ensures that this is being done based on your criteria

You wouldn't update two tables at the same time. Try updating song, then using the primary key of song to update the correct row in words.

Related

Access DB - Combine and remove duplicates

I have two access databases, (ProdDB.mdb) and (AllProdOrders.mdb).
In ProdDB.mdb, I have two tables that have the same structure: Data and Archive
In AllProdOrders.mdb, I have one table: Outputs
This is what I want to do:
What I want to do is create an SQL query that will combine the two tables, Data and Archive into one table and remove any duplicates by checking against three columns: Prod Ord, SO nr and Item No. If these three values are the same for any entry, that is a duplicate and thus shouldn't be included.
After this, I want to left join the AllProdOrders.mdb table: Outputs and add a column to the end of my table. This is done by checking the Order Status in that table against the SO nr.
I have already done the left join portion and my query works properly, all I really need to add is combining the two tables and filtering out any duplicates:
This is my code so far...
string sql = "SELECT [Data].[SO nr], [Data].[Value (GBP)], [Data].[Ship Date (Cust)], [Data].[Line] " +
#"FROM [Archive]" +
"UNION " +
#"SELECT [Data].[SO nr], [Data].[Value(GBP)], [Data].[Ship Date(Cust)], [Data].[Line], status.[Order Status] " +
#"FROM [Data] LEFT JOIN [;database=I:\Departments\Production\AllProdOrders.mdb].[Outputs] AS status " +
"ON [Data].[Prod Ord] = status.[No] "
I'm going to use this combination of tables in my query with an OleDBDataReader to get the total (Value (GBP) of certain orders.
I'm getting a number of columns doesn't match error but I'm not sure how to rectify this as the extra column is the Order Status column that is added to the end of the query and comes from the second database.
As you say, you have an extra column in the second part of the union query. The columns must always match in a union-query. If you add ", NULL as [Order Status]" in the end of the first line I believe the error goes away.

Data is not getting inserted into SQL Server database in orderly manner from datatable vb.net

I am developing one migration utility to transfer data from one table to another using vb.net because I need to make some changes in data and then insert it into another table, while doing this I came to a point where I am inserting data into sql server db from data table using loop through data table, but when I go to SQL Server and check that data is there or not, its not in order by Srno, this is the first time i am seeing this kind of thing, Before inserting the data I am displaying the data in DataGridView and it is showing completely fine in it but as soon as i insert the data from data table to SQL Server DB it does not get saved in order as that is displaying in DataGridView
Note: I tried to loop through DataGridView as well but it is doing the same thing.
Please check the following code for understanding, I've 4500 records need to be migrated
If finalDT.Rows.Count > 1 Then
For Each q In finalDT.Rows
'MessageBox.Show(q.item("ID").ToString + q.item("ODate").ToString + q.item("WDate").ToString)
id = q.item("ID")
odate = q.item("Odate")
wdate = q.item("WDate")
intime = q.item("WDatetime")
outtime = q.item("ODatetime")
qry = "insert into weight3 values ('" & id & "','" & odate & "','" & wdate & "','" & intime & "','" & outtime & "')"
updateWt(qry) - Insert QRY Goes for operation
Next
End If
Please check this image to see how the data is getting inserted.
Click Here
Rest of the details are as follow,
Database schema
Srno int
Odate datetime
Wdatetime datetime
intime nvarchar(50)
outtime nvarchar(50)
Data table
finalDT.Columns.Add("ID", GetType(Integer))
finalDT.Columns.Add("ODate", GetType(String))
finalDT.Columns.Add("WDate", GetType(String))
finalDT.Columns.Add("WDateTime", GetType(String))
finalDT.Columns.Add("ODateTime", GetType(String))
What you're trying to do is basically impossible, since a relational database table does NOT have any inherent ordering - there's no way to insert data "in an ordered fashion" since the table itself is not ordered by design.
The only way to get ordering in a relational database is by SELECTing from your database table and specifying an explicit ORDER BY clause while doing so - in this case, you get back your data in the orderly fashion you've defined.
But there's no way in a relational database to "order the table" by any key or combination of keys or anything - the database table itself is UNORDERED - and that's by design.

Sqlite query multiple columns and group by them

This is my table:
I am basically using the following table to pass a select statement and viewing it as a pie graph.The pie graph excludes fields that are null or 0. So First i used the following query to select the total number by its respective group number and it worked fine. This is the working query.
query = "Select groupNumber as \"Group Number\", totalNumber as \"Total Number\" From " + table;
However i found it hard to read since both columns are numbers and therefore figured it will be better to pass a query where i can select the total numbers by its respective groupName. This is a problem though because if u look at the table you will notice that groupNames can be repeated.
Therefore i would like to do a select satement where i can query the total number by the groupnumber and groupname.
Is this possible if so how? Also i can't modify the data as i receive it in such format from a established connection.

Detect changes between records in two tables

I have 3 tables:
Staging: that gets employee records inserted every month.
Master: table has contains all previously entered records from staging, unique records.
Changes: keeps track of all changes - has no primary key.
The tables have 10 columns. In the staging table, every month we have about 2,500,000 records. Using a cursor I am able to insert new records from staging into the master table.
When it comes to update, I am using an inner join to get the records from staging that already exist in the master table.
To find out if any of the employee info has changed, do I have to query something line this:
WHERE Staging.FirstName <> Master.FirstName
OR Staging.LastName <> Master.LastName
OR ...
And so on for 10 columns, or is there an easier way?
If the two tables really are identical, you could create a persisted computed column in each table that represents a checksum of the entire row (see http://technet.microsoft.com/en-us/library/ms189788.aspx), create an index on that, and then use that for your joins.
Using a Cursor for millions of rows does not sound like fun.
Maybe you should look at EXCEPT/MERGE
WITH NewAndChanged AS (
SELECT Stage.Id
,Stage.Col1
,Stage.Col2
FROM Stage
EXCEPT
SELECT Master.Id
,Master.Col1
,Master.Col2
FROM Master
)
MERGE Master
USING NewAndChanged
ON Master.Id = NewAndChanged.Id
WHEN MATCHED
THEN UPDATE SET
Col1 = NewAndChanged.Col1
,Col2 = NewAndChanged.Col2
WHEN NOT MATCHED
THEN INSERT (
Id
,Col1
,Col2
)
VALUES (
NewAndChanged.Id
,NewAndChanged.Col1
,NewAndChanged.Col2
)

Inserting unique id for file uploaded to database using SqlBulkCopy

There is a table with the following columns:
ProdID
Specification
Value
I've got a csv file with a schema that only has the last two columns - Specification and Value. In other words they are my csv headers. Now I need to associate the values of this file against one unique id.
The significance of that id is that it is a reference to the document; its not important here, but, for now assume it is reqired by the system.
Now while I use the SqlBulkCopy class to insert is there a way that I can still insert some value for ProdID such that each row inserted will have the same ProdID for that file I am trying to bulk copy...?
That depends. if ProdID is an identity field in the database, the database will fill it for you when you perform the bulk copy.
If it's not, you will need to fill it yourself and pass the three columns to SqlBulkCopy, instead of just the two.
You'd have to do something on the lines of the following code:
string cmdTxt = "SELECT " + ProdID + ", * FROM [sample.csv]";
It doesn't matter where you place the ProdID field in the select clause above; in the beginning or end, you'll get the result set with ProdID as the first field...

Categories