user818700
user818700

Reputation:

Syntax error in mysql?

I'm used to MS sql so working with mySql is sure to throw some syntax error at me that is bound to slow me down. I have the following:

declare @expensesSum DOUBLE
select @expensesSum = sum(expenseAmount) from bondsaverdb.expenses
insert into bondsaverdb.expenses
select '6','Extracash',(income - @expensesSum)  from bondsaverdb.income where userName ='Dean'

The error I'm getting says:

syntax error near declare @expensesSum

Must be the way I'm declaring the variable??

Thanks in advance!

Upvotes: 1

Views: 86

Answers (2)

gbn
gbn

Reputation: 432271

MySQL is very different to SQL Server; you don't declare variables generally

SET @expensesSum = (select @expensesSum = sum(expenseAmount) from bondsaverdb.expenses);
insert into bondsaverdb.expenses
select '6','Extracash',(income - @expensesSum)  from bondsaverdb.income where userName ='Dean';

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

MySQL does not require (or even allow, apparently) you to declare variables, and as far as I know it will treat all objects, variables included, as strings -- so they cannot be typed in t his way. Anyway, this should work:

SET @expensesSum = (SELECT SUM(expenseAmount) FROM bondsaverdb.expenses);
INSERT INTO
   bondsaverdb.expenses
SELECT
   '6', 'Extracash', (income - @expensesSum)
FROM
   bondsaverdb.income
WHERE
   userName = 'Dean'

I'm also not sure what that '6' is for .. if that's an auto increment ID you should omit it from the INSERT altogether (and specify the other columns to write to) or leave it NULL.

Upvotes: 1

Related Questions