0

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?

5
  • 1
    Is this MySQL or SQL Server? Commented May 17, 2016 at 15:16
  • 2
    Please provide error message and DDL of your table Commented May 17, 2016 at 15:16
  • 1
    Please decide #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server Microsoft SQL Server or MySQL. Commented May 17, 2016 at 15:23
  • 1
    You're saying you get an error telling you to read MySQL documentation in SQL Server? Commented May 17, 2016 at 15:24
  • I think you must provide datatype after column name. Commented May 17, 2016 at 15:30

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But error message says otherwise ?,please update question
0

Alter looks good missing parenthesis and column type.

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

Comments

0

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.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.