0

I'm trying to work with variables inside a mysql statement and struggling.

I've got this at the moment...

 SET @customer = 'UNKNOWN';
 SET @customer = (SELECT account FROM contacts WHERE emailaddy = 'myemailaddress' LIMIT 1);

 INSERT INTO cases
 (customer, details,severity,currentowner,created,createdautomatically)
 VALUES (@customer, 'This is a test case','SYS',now(),TRUE)

I'm getting 'Error. Parameter '@customer' must be defined'.

Can anyone point me in the right direction?

EDIT : After being told I was missing the declare, I've added this and I'm still getting the same error...

 DECLARE @customer VARCHAR(200);
 SET @customer = 'UNKNOWN';
 SET @customer = (SELECT account FROM contacts WHERE emailaddr = 'myemailaddress' LIMIT 1);

 INSERT INTO cases(customer, ....) VALUES (@customer, .....)

 Error. Parameter '@customer' must be defined..

ANOTHER EDIT....

Still doesnt seem to be working correctly :(

 DECLARE @customer VARCHAR(200);
 SET @customer := 'UNKNOWN';
 SET @customer := (SELECT account FROM contacts WHERE email = 'me');
 INSERT INTO cases (customer, ...) VALUES (@customer, '...')

 Error. Parameter '@customer' must be defined..
4
  • 1
    You need to have a DECLARE @customer <datatype> somewhere before you SET it. Commented Jun 29, 2016 at 17:47
  • 2
    More over in the INSERT INTO you have 6 columns defined but passing only 5 values for the same. Hope severity's equivalent value is not passed. Commented Jun 29, 2016 at 17:50
  • Thanks Arulkumar... I've stripped alot of the real info out before posting to make sure I don't accidentally breach data protection :) Commented Jun 29, 2016 at 17:54
  • 1
    it is not an issue with declare please check my answer Commented Jun 29, 2016 at 17:55

2 Answers 2

1

the issue is with the 1st line

mysql> select "sss" into @customer;
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO funding(dONATUR) VALUES (@customer);
Query OK, 1 row affected (0.09 sec)

mysql> SELECT * FROM funding WHERE Donatur='sss';
+------+--------+---------+
| area | client | Donatur |
+------+--------+---------+
| NULL | NULL   | sss     |
+------+--------+---------+
1 row in set (0.00 sec)
Sign up to request clarification or add additional context in comments.

6 Comments

So what I'm trying to say there is 'if a result is found in contacts, make that the the value of @customer, else make the value 'UNKNOWN'.... does that make sense?
your issue is missing ":" value before = mark i have added it for you SET @customer := (SELECT account FROM contacts WHERE emailaddr = 'myemailaddress' LIMIT 1);
See my latest edit Mahesh; It's not playing ball at all :(
sorry for the inconvenient due to previous answer
no worries Mahesh - thanks for taking the time to help! I'm struggling to use your example to successfully modify my code though. should it be like this... SELECT (SELECT name FROM contacts WHERE email = 'myaddress' LIMIT 1) into @customer;
|
0

Could you please try with this query:

SELECT @customer := COALESCE(`account`, 'UNKNOWN') FROM `contacts` WHERE `emailaddr` = 'myemailaddress' LIMIT 1;

INSERT INTO `cases` (customer, ...) VALUES (@customer, '...');

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.