For the below Query its giving the following error
Error Code: 1175. You are using safe update mode and you tried to update a table
without a WHERE that uses a KEY column
To disable safe mode, toggle the option in Preferences -> SQL Editor -> Query Editor
and reconnect.
Can anybody correct me where I have got my query wrong.
UPDATE delivery p SET p.OrderID =
(
SELECT OrderID from OrderTable o where o.DiagramID=p.DiagramID AND o.DeliveryDate=
(
Select min(o2.DeliveryDate) from OrderTable o2 where o2.DiagramId=o.DiagramID
)
)
where p.OrderID=0;
The problem is, you are working in MySQL safe mode, which disallows you to run any updates without using KEY in where clause. To disable it, use
SET SQL_SAFE_UPDATES=0;
just before running a query
Related
I'm trying to delete (and update, but if I can delete than I'll be able to update) product data from MySQL website database using SSIS, when those products have been marked in our ERP (and in the sql server database used for reporting) as discontinued. I've tried the following:
First Attempt: Saving the rows-to-be-deleted to a recordset and using a for-each loop with an execute sql task to delete them as described here.
Result: Partially works, but is extremely slow and fails after about 500 deletes each time. Makes me wonder if the MySql database has some kind of hacker-protection feature.
Second Attempt: Converting the primary key for all rows-to-be-deleted into a comma-separated string variable using FOR XML PATH : as described here (or, rather, a series of them because of the 4000 char limit).
SQL Select Code (works fine)
WITH CTE (Product_sku,rownumber) AS
(
SELECT product_sku
, row_number() over(order by product_sku)
FROM product_updates
WHERE action = 'delete'
)
SELECT
Delete1= cast(
(SELECT TOP 1
STUFF(
(SELECT ',''' + product_sku+'''' FROM CTE
WHERE cte.RowNumber BETWEEN 1 and 700
FOR XML PATH (''))
, 1, 1, '') )
AS varchar(8000))
... and nine more of these select statements into additional variables to allow for larger delete operations.
And then using this result to delete records from MySql using an Execute SQL command with the following code:
DELETE FROM datarepo.product
WHERE product_sku in (?)
Result: The package executed but failed to delete anything. When viewing the MySql query log file I saw the following, which tells me why it failed to delete anything.
DELETE FROM datarepo.product
WHERE product_sku in ('\'')
Note that this same SSIS Execute SQL statement , when using hardcoded values (like the following), deletes just fine.
DELETE FROM datarepo.product
WHERE product_sku in ('1234','5678','abcd', etc...)
I haven't been able to find anything else online. As Reza Rad said in the first linked post, it's hard to find material about using SSIS to perform operations on MySql.
I'm trying to update a table with this code:
CREATE DEFINER=`root`#`localhost` PROCEDURE `refresh_stock`(id_product int)
BEGIN
update stock s inner join replacement r on r.product = s.product
set s.quantity = if (s.quantity = 0, r.quantity, s.quantity+r.quantity), s.refresh_date = now()
where s.product = id_product and r.product=id_product and r.date_replacement > s.refresh_date;
END
Basically, I have to update a number of items in a stock every time.I buy new products. The thing is, this procedure worked two times, then it stopped working and it's giving me this error: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
I tried to pass in the parameter the stock id, but it gives me the same error above. How can I solve this problem WITHOUT disable safe update?
I am using SQL Server 2008 R2. I have created some SQL statements for some migration:
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='TableA' AND COLUMN_NAME='Status')
BEGIN
UPDATE TableA
SET Status = 'Active'
WHERE Status IS NULL
END
Now, I have dropped the column Status from database table TableA.
Again when I am executing the above block, and although I have placed a check whether that column exists, only then it should execute the UPDATE statement, it gives me error
Invalid column name 'Status'
How to get rid of this error?
Thanks
You need to put the code to run in a separate scope/batch:
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='TableA' AND COLUMN_NAME='Status')
BEGIN
EXEC('UPDATE TableA SET Status=''Active'' WHERE Status IS NULL')
END
The problem you currently have is that the system wants to compile your batch of code before it executes any part of it. It can't compile the UPDATE statement since there's a column missing, so it never even has a chance to start executing the code and considering whether the EXISTS predicate returns true or false.
Your current SQL Block might fail some times because Information_schema is view and not a table. Also, according to MSDN
Some changes have been made to the information schema views that break backward compatibility.
Hence we can't rely on information schema views.
Instead use sys.tables
IF EXISTS(SELECT 1 FROM SYS.COLUMNS
WHERE NAME = N'Status' AND OBJECT_ID = OBJECT_ID(N'TableA'))
BEGIN
UPDATE TableA SET Status='Active' WHERE Status IS NULL
END
I tried to delete the entries in table1 which also appear in table2.
code is the primary key for both table1 and table2.
And I use the following query:
delete from table1 where table1.code = any(select code from table2);
However, I got the following error msg:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor -> Query Editor and reconnect.
But my understanding is I have already used the WHERE with a KEY column.
Is there any advice on this?
You aren't using any restriction clause in your subquery.
I'm getting an error in the following OPENQUERY statement that I'm trying to execute against a MySql database from SQL Server.
UPDATE OPENQUERY(MYWPDB, 'SELECT total FROM wp_tt WHERE id = 112121') SET total = 1
The error is "Key column information is insufficient or incorrect. Too many rows were affected by update".
The statement should be updating the 'total' field to the value of '1'. It's an integer field and 'id' is the primary key on the table. I'm using SQL Server 2000.
I had the same issue with an openquery that updates iSeries. My openquery is within a cursor also.
The fix is to include the key columns in the select.
So in your case it would be something like this:
UPDATE OPENQUERY(MYWPDB, 'SELECT key1, key2, total FROM wp_tt WHERE id = 112121') SET total = 1
Turns out there's nothing wrong with the query. I was trying to execute the statement inside a cursor operation inside a stored procedure. I tested it outside the cursor operation and it processed fine.
However, since I still needed it to work within the cursor, I had to keep digging, and finally found the four-part syntax would do the trick. So the query instead became:
UPDATE MYWPDB...wp_tt SET total = 1 WHERE id = 112121