简体   繁体   中英

Adding in SQL Column that Totals Other Column Values

Trying to add a new column into pre-existing table with the following command:

ALTER TABLE randomquestions 
ADD Total AS R1_Number + R2_Number + R3_Number + R4_Number PERSISTED

I get this error:

#1064 - 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 'AS R1_Number + R2_Number + R3_Number + R4_Number PERSISTED' at line 2

Can anyone tell me where I went wrong with the syntax?

If MySQL 5.7.6+ Generated Columns :

ALTER TABLE randomquestions 
ADD Total INT AS (R1_Number + R2_Number + R3_Number + R4_Number) STORED;

Note that you may need to change datatype depending of datatype of Rx_Number .

Alter looks good missing parenthesis and column type.

ALTER TABLE randomquestions 
ADD Total int AS (R1_Number + R2_Number + R3_Number + R4_Number) PERSISTED

I tried a similar statement with the parenthesis and it worked:

use test
alter table [dbo].[tablename]
Add total2 as (price * 2) persisted 

(so I guess it's the parenthesis, it's not finding your table, or just a syntax error with the field names.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM