1

I have to insert a variable value into a table. What is the right syntax? I tried with

set @variable= @variable1+@variable2
INSERT INTO table (column) VALUES(@variable);
INSERT INTO table (column) VALUES('variable');

but field is not populated. I checked that the variable exists.

5
  • how you are setting @variable1 and variable2 ? Commented Jul 16, 2015 at 10:34
  • 1
    depends on the columns type - you have the correct syntax for inserting string literals and numerics. but need to use set @variable=@variable1+@variable2 also these other variables need to be set previously
    – amdixon
    Commented Jul 16, 2015 at 10:36
  • Column type is float. I missed to to write set... but in the code is present Commented Jul 16, 2015 at 10:39
  • The problem was the variable type. I changed float to real and it works. Thanks! Commented Jul 16, 2015 at 10:47
  • 1
    Be aware, your edit invalidated a few answers. In the future, please avoid making these sort of changes to your questions.
    – gunr2171
    Commented Jul 16, 2015 at 15:57

5 Answers 5

2
SET @variable = @variable1 + @variable2;
INSERT INTO table (column) VALUES (@variable);
4
  • 2
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – gunr2171
    Commented Jul 16, 2015 at 15:46
  • The problem was that the variable was not set. Rigth now the user has edit the question and add this set and the problem was another one, so this is no longer a correct answer to this edited question. Commented Jul 16, 2015 at 15:50
  • Hum, that is unfortunate. Still I would recommend in the future adding at least a sentence explaining the fix. We are in the process of closing the question
    – gunr2171
    Commented Jul 16, 2015 at 15:52
  • I'll do in the future. Thanks for the advice ;) Commented Jul 16, 2015 at 15:59
2

Please check and ensure both variables shouldn't be NULL in this case.

I tried this and run successfully:

set @variable1 = 100;
set @variable2 = 100;

SET @variable = @variable1 + @variable2;
INSERT INTO  etl_1(name) VALUES (@variable);

Here etl_1 is my Table name, replace it with yours..

whats the error at your end?

0

You can try this:-

INSERT INTO table (column) 
SELECT @variable= @variable1+@variable2
0

Try

SET @variable = @variable1+@variable2
-2

Seems like you have missed a spave between VALUES and (

    @variable= @variable1+@variable2;
    INSERT INTO table (column) VALUES (@variable);
    

Let me know ;)

4
  • I dont think space can cuse such error. Commented Jul 16, 2015 at 10:35
  • It's right @AnkitBajpai Commented Jul 16, 2015 at 10:41
  • What's right?? My comment or this query?? Commented Jul 16, 2015 at 12:02
  • Your comment. Regarding the query I have to use INSERT INTO...VALUES Commented Jul 16, 2015 at 12:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.