need to update the DB table through macros - mysql

I need to do the below update through macros,
If tbname = "PARTY" Then
Dim ssql As String
ssql = "Update PARTY Set PARTY_ID= (SELECT (max(PARTY_ID)+1) FROM PARTY) WHERE PARTY_ID ='DUMMY'
DB.Execute ssql, 64
End If
I am getting this error, while running the above statement.
Error Code: 1093. You can't specify target table 'PARTY' for update in
FROM clause 0.000 sec
Is there any other way to update the max(party_id)+1 to the row having party_id as "DUMMY"

The error you're having is in your SQL. You shouldn't be using a subselect here. The from-clause you're having is complete bull, because Update PARTY implies From PARTY
Additionally you seem to be calculating something strange, since you get an "invalid use of group function"-error. Short googling reveals this stackoverflow question, suggesting you should be using HAVING instead of WHERE
The fix is thusly simple:
ssql = "Update PARTY set PARTY_IT=(max(PARTY_ID) + 1) HAVING PARTY_ID='DUMMY'"

Related

MySqlException was unhandled on the adapter.Fill(table) when trying to add data to the datagrid

When my SQL query has a count function in the query it does not want to display the data in the DBGRID and I'm getting a "MySqlException was unhandled" error. As soon as I remove the count function it runs smoothly and displays the data on the DB grid.
Code below:
Dim cmd As New MySqlCommand("SELECT COUNT(a.client_id), a.CLIENT_ID,b.c_name, b.C_surname FROM tblinv_info a JOIN tblclientinfo b ON a.CLIENT_ID = b.CLIENT_ID WHERE extract(year from a.inv_date) in ('2018','2019') AND a.Client_id = b.Client_id GROUP BY a.client_id ORDER BY count(a.client_id) desc LIMIT 10", connection)
cmd.CommandTimeout = 500
Dim adapter As New MySqlDataAdapter(cmd)
Dim table As New DataTable()
adapter.Fill(table)
dbreport.DataSource = table
Any idea on why this could be happening? I'm running the program in Visual Studio coding with VB.NET and using a MySql database. The SQL command runs fine on the localhost database. Do I need to add data to the datagrid differently?
Error: Error code is as follows: An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
Additional information: Fatal error encountered attempting to read the resultset.
Two things jump out at me as being potentially wrong here:
You need to give your count column an alias
If you're running your MySQL in ONLY_FULL_GROUP_BY mode (you probably aren't, as you assert it works without the count, but you should), then you need to add b.c_name, b.C_surname to your GROUP BY or put them in aggregation functions (e.g. MAX(b.c_name) as c_name, MAX(b.C_surname) as c_surname) in your SELECT
On this latter point, if you don't group or aggregate and your MySQL is not in "only full groupby", then it will select values at random from the group, for c_name and c_surname, and this can lead to unexpected program effects
As a final point of advice, always avoid using functions on columns in the where clause. It would be better to say WHERE somedate >= '2018-01-01' AND somedate < 2020-01-01' than WHERE extract(YEAR from somedate) IN (2018,2019). If you use a function on a column value MySQL has to evaluate the function for every row, and it means indexes on the column cannot be used.

SSIS Mysql dataflow multiple statement source

I am running into problems with SSIS(2012), mysql(odbc) and dataflow when my sql command is more than one statement. In addition I cannot start the statement with a comment either. This is not a problem when using oledb & sql server.
SELECT id FROM id_table WHERE type = 'product' INTO #id;
SELECT name FROM products WHERE id = #id;
This isn't the most relevant example. It's just to show what I'm doing. But this will not work with Mysql(ODBC) as my source. I get the following:
Table has no columns
My work around is using a sub query:
SELECT name FROM products WHERE id = (SELECT id from id_table WHERE type = 'product');
Also the below with a comment comes up with the same error:
/* Test comment */
SELECT name FROM products WHERE id = (SELECT id from id_table WHERE type = 'product');
Again, sql server(OLEDB), haven't tried ODBC, has no problems with either case.
My first thought is to check the driver and enable the "Multiple statements" flag, which is already checked. Is there a solution to this in the settings of the driver or in SSIS?
EDIT:
Additional examples:
This works with sql server and odbc source for a dataflow:
DECLARE #offer AS VARCHAR(8)
SET #offer = 'WDWD' + CONVERT(VARCHAR,1000 + DATEPART(wk,GETDATE()))
SELECT * FROM source WHERE cd = #offer
The rough equivalent with mysql and odbc source for a dataflow does not:
SET #offer = CONCAT('WHW410', LPAD(WEEKOFYEAR(NOW()), 2, '0'));
SELECT * FROM source WHERE cd = #offer;
I verified both of these are valid sql in SSMS and sqlyog.

SQL query access pivot - error message 'field that has an invalid data type'

I'm running an SQL Query on MS Access.
the query looks like this:
TRANSFORM MIN(X_VALUE*MULTIPLE & ' ' & Y_VALUE)
SELECT A.ID
FROM ((MY_TABLE_A A
INNER JOIN MY_TABLE_B B ON B.ID = A.ID)
INNER JOIN MY_TABLE_C C ON C.FOO1_ID = A.FOO1_ID)
LEFT JOIN MY_TABLE_D D ON A.FOO2_ID = D.FOO2_ID
WHERE A.NUM ='FOO'
AND A.FOO_ID<>0
AND FOO3=1
GROUP BY A.ID PIVOT X_NAME IN('BLAH1', 'BLAH2')
when running this against local MDB file, it works.
when running this against Linked MDB (tables are linked to remote Oracle DB), I'm getting
ERROR [42000] [Microsoft][ODBC Microsoft Access Driver] The Microsoft
Access database engine could not execute the SQL statement because it
contains a field that has an invalid data type.
I've googled it, and couldn't find anything useful.
Any idea what can I do?
thanks.
The only statement in the query that even vaguely seems it would cause data type issues is the mixed types in the transform statement. Perhaps the following would work:
TRANSFORM MIN(CSTR(X_VALUE*MULTIPLE) & ' ' & CSTR(Y_VALUE))

operation must use an updateable query - access

I want to update a table by using the join in access - 2007
UPDATE TABLE1 A INNER JOIN (SELECT ACCODE, SUM(AMOUNT) AS SUM_AMOUNT
FROM TABLE2 GROUP BY ACCODE) B ON A.ACCODE = B.ACCODE
SET A.TRIAL = A.TRIAL + SUM_AMOUNT
it gives me error that
operation must use an updateable query
i have try with below query and no error is here
UPDATE TABLE1 A INNER JOIN TABLE2 B ON A.ACCODE = B.ACCODE
SET A.TRIAL = A.TRIAL + SUM_AMOUNT
please help me to find what is wrong with first query
I think the reason Access treats your query as non-updateable is due to the subquery GROUP BY. You should be able to create an updateable query by using DSum.
UPDATE TABLE1 AS a
SET a.TRIAL = a.TRIAL
+ DSum("AMOUNT", "TABLE2", "ACCODE=" & a.ACCODE)
If ACCODE is text instead of numeric data type, add quotes around the value in the DSum expression.
UPDATE TABLE1 AS a
SET a.TRIAL = a.TRIAL
+ DSum("AMOUNT", "TABLE2", "ACCODE='" & a.ACCODE & "'")
I just had this issue and it was due to permissions on the table. I made a copy of the linked table to test my update query on, and kept getting this error message. I was pretty sure my query would be ok (it was a simple update) so I ran it on the actual table and I didn't get the error. Just wanted to add this for anyone else that comes across this in the future!
this error also comes when you are accessing database which is on different PC. so give write permission from security tab of folder and also give write access on sharing permission option.
In my case this is worked
Yes Mandeep, that is the way ms-access works.
UPDATE Table1 As T1 INNER JOIN Table2 AS T2 ON T1.F1 = T2.F1
SET T1.F2 = T2.F2
This query raises 'operation must use an updateable query' erro when T2 is a query, even when you are not updating T2.

Can't get join on mysql delete query to work

I know there is more than one question out there that matches this, but I am relatively new to mysql, and I can't seem to make this work using sub quests or the USING key word, plus I find the mysql on line docs a complete mystery.
I started trying to build my DELETE query using a SELECT query as my base and was able to get all the rows that I wanted to delete:
select *
from writings_tags_link
join writing_tags on writing_tags.id = writings_tags_link.tag_id
where writing_tags.tag = 'tag one'
and then just replaced select all with DELETE so:
delete
from writings_tags_link
join writing_tags on writing_tags.id = writings_tags_link.tag_id
where writing_tags.tag = 'tag one'
I gather from both the error message and from other similar posts that you can't use 'ON' to join tables in a delete query, you have to use USING or a sub query. The query I built with USING returns a really strange error, first the query:
DELETE
FROM writings_tags_link
USING writing_tags_link INNER JOIN writing_tags
WHERE writing_tags.id = writings_tags_link.tag_id
AND writing_tags.tag ='tag one'
error:
#1109 - Unknown table 'writings_tags_link' in MULTI DELETE
This table does exist, obviously, my original select query returned the desired results. Any help / explanation would be so very appreciated!
Please keep in mind, I'm only trying to delete the data in the linking table.
Your information is incorrect about requiring the use of the USING keyword in DELETE syntax when using JOINs - the documentation provides examples in the multi-delete section:
DELETE wtl
FROM WRITINGS_TAGS_LINK wtl
JOIN WRITING_TAGS wt ON wt.id = wtl.tag_id
WHERE wt.tag = 'tag one'