I am generating the sql statement below based on some coldfusion logic, but it is erroring and I can't seem to find the cause, I have tried making many different modifications to it and nothing seems to be helping.
UPDATE MAIN_RECORDS
SET JONUM = NULL,
SET CUSTNAME = 'Super Sweet Name',
SET CONTACTDT = 02/28/2011,
SET ENGRECDT = 03/07/2011,
SET HOW_WR_DT = 03/07/2011,
SET COMM_DT = 03/29/2011,
SET FACAVALDT = NULL,
SET FAX_SUPDT = 03/07/2011,
SET LINENUM = 'CLPRO L6',
SET POLENUM = 'CLPRO 125 T T3',
SET REASON = '03/07/11 NO VAC FAC THIS IS THE WRONG INFORMATION IT WAS ON HERE TWICE',
SET REC_TYPE = 'H',
SET ORDER_TYPE = 'P',
SET CANCEL_ORDER = 'Y',
SET State_abbr = 'IL',
SET dbfk_state = 17,
SET xx_streetnumber = '2626',
SET xx_street = 'Fake St',
SET xx_city = 'NEWTON',
SET xx_class_of_service_ind = 'R',
SET xx_cellphone_ind = '1',
SET xx_assigned_phone = '3045653897',
SET xx_exchange_name = 'NEWTON',
SET XX_new_ref_code = '60',
SET xx_new_service_type = '11',
SET ORD_COMDT = 03/11/2011,
SET delivery_date = NULL
WHERE ordernum = '08824112' AND exchnum = '304565'
Currently the error that management studio is giving me is:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'SET'.
You only need 1 SET statement, instead of the multiple ones you have.
Also, your dates need to have single quotes around them.
e.g.:
UPDATE MAIN_RECORDS
SET JONUM = NULL,
CUSTNAME = 'Super Sweet Name',
CONTACTDT = '02/28/2011',
ENGRECDT = '03/07/2011',
HOW_WR_DT = '03/07/2011', .....
Look at the UPDATE statement. The syntax in the post is all wrong :)
The relevant portion:
SET
{ column_name = { expression | DEFAULT | NULL }
| #variable = expression
| #variable = column = expression } [ ,...n ]
Note that SET can only be specified once. The ,...n signifies the previous consuct (that in the {}) can be specified an additional n times, separated with a comma: the SET keyword itself, however, is outside that construct.
Happy coding.
Well generally the command syntax for this would follow this logic
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
so only one SET not the multiple that you have
Only one SET is needed in update Keyword to update n number of columns - example:
Update Employee
set City = Chennai,Country ='India',Employee name = 'Vignesh'
where Employee Id = 1X234
Related
I have tried:
SET #my_var = 'one,two,three';
SET #to_replace = 'two,';
SET #my_check = 1;
SET #my_var = SELECT IF(
ISNULL(#my_check),
#my_var,
REPLACE(#my_var, #to_replace,'')
);
I have tried also SET #my_var = IF( ... Or SELECT REPLACE instead REPLACE in condition
But got syntax error
What I expect is:
If #my_check = null so #my_var = 'one,two,three' (still unchanged)
Else replace two, with '' but using #to_replace variable
Expected result: #my_var = 'one,three'
set #my_var = if(isnull(#my_check), #my_var, replace(#my_var, #to_replace, ''));
Works well in MySQL v8.
So, I created an INSERT statement on MySQL that added a certain row to a table. The invoice_id was set to DEFAULT. Now, I need to find out what that auto-generated id was to continue. How do I go about that?
I've tried using DEFAULT and AUTO to see if they would do the trick. They did not.
INSERT into ap.terms
VALUES (terms_id = 6, terms_description = 'Net due 120 days', terms_due_days = 120);
UPDATE ap.terms
SET terms_description = 'Net due 125 days', terms_due_days = 125
WHERE terms_id = 6;
DELETE FROM ap.terms
WHERE terms_id = 6;
INSERT into ap.invoices
VALUES (AUTO_INCREMENT, 32, 'AX-014-027', '2014-08-01', '434.58', '0.00', '0.00', 2, '2014-08-31', NULL);
INSERT into ap.invoice_line_items
VALUES (invoice_sequence = 1, account_number = 160, line_item_amount = '180.23', line_item_description = 'Hard drive'), (invoice_sequence = 2, account_number = 527, line_item_amount = '254.35', line_item_description = 'Exchange Server update')
SET invoice_id = last_insert_id(1,1);
UPDATE ap.invoices
SET credit_total = invoice_total*0.1, invoice_total = (payment_total + credit_total)
WHERE invoice_number = 'AX-014-027';
UPDATE ap.vendors
SET default_account_number = 403
WHERE vendor_id = 44;
UPDATE ap.invoices
SET terms_id = 2
WHERE default_terms_id = 2;
DELETE FROM ap.invoice_line_items
WHERE invoice_id = last_insert_id(1,1);
DELETE FROM ap.invoices
WHERE invoice_id = last_insert_id(1,1);
So, line 16, where it says "SET invoice_id = last_insert_id(1,1)" is where my Error is. How do I fix it?
Error code 1062, you have an error in your sql syntax. Check the manual that corresponds.
Your table structure is incorrect. Your primary key should be set to auto increment, default none. You should use LAST_INSERT_ID() to retrieve the last row ID inserted into the database.
I'm getting an error about my query, and i'm not understanding what the problem might be. The error i get is
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range = '55', atkspeed = '0.95', m_damage = '0', p_damage = '38', mprotection = ' at line 1
While the code i'm using is this one
$id = mysql_real_escape_string($_POST["id"]);
$value0 = mysql_real_escape_string($_POST["value0"]);
$value1 = mysql_real_escape_string($_POST["value1"]);
$value2 = mysql_real_escape_string($_POST["value2"]);
$value3 = mysql_real_escape_string($_POST["value3"]);
$value4 = mysql_real_escape_string($_POST["value4"]);
$value5 = mysql_real_escape_string($_POST["value5"]);
$value6 = mysql_real_escape_string($_POST["value6"]);
$value7 = mysql_real_escape_string($_POST["value7"]);
$value8 = mysql_real_escape_string($_POST["value8"]);
$value9 = mysql_real_escape_string($_POST["value9"]);
$value10 = mysql_real_escape_string($_POST["value10"]);
$query="UPDATE char_stats SET vita = '$value0', mana = '$value1', speed = '$value2', range = '$value3', atkspeed = '$value4', m_damage = '$value5', p_damage = '$value6', mprotection = '$value7', pprotection = '$value8', hp5 = '$value9', mp5 = '$value10' WHERE id_char_stats='$id'";
I'm using also other very similar queries so i don't get what the problem might be. I was thinking about the underscore on char_stats so i tried using
char\_stats
for escape, but it's not working anyway.
Thanks in advance
create table t11
(
id int not null,
`range` int not null,
speed int not null
);
update t11 set range='11', speed=1; -- blows up
update t11 set `range`='11', speed=1; -- fine
update t11 set `range`=11, speed=1; -- fine
Moral of the store: back-tick range. Even the create table blows up without it.
see mysql keywords and reserved words here. Range is one of them.
So your query would become:
$query="UPDATE char_stats SET vita = '$value0', mana = '$value1', speed = '$value2', `range` = '$value3', atkspeed = '$value4', m_damage = '$value5', p_damage = '$value6', mprotection = '$value7', pprotection = '$value8', hp5 = '$value9', mp5 = '$value10' WHERE id_char_stats='$id'";
after reading all the "INSERT INTO" posts as well as the documentation, I am still unsure whether what I want is feasible or not.
I want to change the UPDATE below into an INSERT INTO, because I have many 100-thousands of them (speed issues):
UPDATE city c
SET
c.g17h = '3196504',
c.g17q = '2593487',
c.g17k = '0',
c.g17w = '0',
c.g17s = '0'
WHERE
p17t = 30 AND p17l = '30';
or
UPDATE city c
SET
c.g1h = '0',
c.g1q = '0',
c.g1k = '0',
c.g1w = '0',
c.g1s = '0'
WHERE
p1t = 1
AND p1l = '1';
However, my best solutions do not work:
INSERT INTO city (g17h, g17q, g17k, g17w, g17s)
SELECT
'3196504',
'2593487',
'0',
'0',
'0'
FROM valuestoretab
WHERE
p17t = 30
AND p17l = '30';
This is of course because my WHERE condition can only be satisfied in the first table (city) but not in the second one which is just a table of values, whereas the city table is a data set where each id has particular values for p17t, p17l and so on.
For clarification:
The first table (city) looks like
cityid1, ownerid1, islandid1, p17t, p17l
cityid2, ownerid2, islandid2, p17t, p17l
with different values for each row for p17t and p17l.
So, my questions would be:
1. Is it at all possible to write a INSERT-Query with a WHERE condition for the table that is being inserted into?
2. If no, do I have to stick to my UPDATE or is there another (fast!) solution?
Thanks to the community!
litotes
To give a quick answer insert with a select statement is possible
Insert INTO MyTable (Val1, Val2)
Select
SomeValue,
AnotherValue
From MyOtherTable
Where Date = Getdate()
On the other hand, you can also update many records like in the following example:
Update t1
Set
t1.Val1 = t2.SomeValue,
t1.Val2 = t2.AnotherValue
From MyTable t1
Inner join MyOtherTable t2 ON t1.PK = t2.FK
where t2.Date = getdate()
EDIT:
When i read the following query, I presume p17t and p17l are from the valuestoretab.
UPDATE city c
SET
c.g17h = '3196504',
c.g17q = '2593487',
c.g17k = '0',
c.g17w = '0',
c.g17s = '0'
WHERE
p17t = 30 AND p17l = '30';
==> changed this into:
UPDATE c
SET
c.g17h = '3196504',
c.g17q = '2593487',
c.g17k = '0',
c.g17w = '0',
c.g17s = '0'
FROM City c, ValueStoreTab v
WHERE
c.SomeCol = v.SomeCol -- Here, your relation must exist!
AND v.p17t = 30 AND v.p17l = '30';
I'm getting the following MySQL error:
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'SET type =
'movie', SET category = 'New', SET
music = 'Pop', SET' at line 1
Heres my query:
UPDATE music_content
SET title = 'Classic',
SET type = 'movie',
SET category = 'New',
SET music = 'Pop',
SET audience = 'Everyone'
WHERE id = '6'
Not sure what I'm doing wrong? - all the columns and tables exist and all the data is escaped (using mysql_real_escape_string()). Furthermore I have a valid/connected MySQL connection.
MySQL Version: 5.1.41.
The UPDATE syntax uses only one SET even when multiple columns are being updated.
So try:
UPDATE music_content
SET title = 'Classic',
type = 'movie',
category = 'New',
music = 'Pop',
audience = 'Everyone'
WHERE id = '6'
You only need to have "SET" once:
UPDATE music_content SET title = 'Classic', type = 'movie', category = 'New', music = 'Pop', audience = 'Everyone' WHERE id = '6'
You should only have one SET, like so:
PDATE music_content SET title = 'Classic', type = 'movie', category = 'New', music = 'Pop', audience = 'Everyone' WHERE id = '6'
I think you only need one SET. Remove the others and see if it works.
You have an abundance of SET's in your statement. Drop all but the fist one. For more information, please see the UPDATE Syntax documentation.