2

first of all... Before someone comes here and tag it as "duplicated question" please take a better look to my question !

I have an Insert that I was using Parameters to pass the values but only one field I need to take it's value with a select, look:

INSERT INTO chamados (club, project, function, description, priority) (@club, @proj, @func, @description, @pri)";

Only the club field I need to get from a SELECT query. How Do I do that?

Obs: It's not a duplicate question. All I saw in the others threads, it was showing how to get all the values from a select, while I just need one... All the others values i'm passing by parameters/strings . . .

Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54
Ghaleon
  • 1,186
  • 5
  • 28
  • 55
  • Do you mean that you need to perform the INSERT and then SELECT the value of the field 'club' back? Does that mean the 'club' field is AUTO_INCREMENTed or populated as a result of a table trigger? – Matt Jan 21 '13 at 16:32
  • Travis's answer solves this problem: http://stackoverflow.com/questions/25969/sql-insert-into-values-select-from – cheesemacfly Jan 21 '13 at 16:33
  • No, It's not @Matt. I need the select to `get` the value so I can perform the `insert`... Hope I could be clear. Thanks guys ! for answering me. – Ghaleon Jan 21 '13 at 16:35
  • @Ghaleon ok, I think then you have your answer below. – Matt Jan 21 '13 at 16:36

1 Answers1

3

You can mix the hard-coded variables, and the value from your table in a select statement:

INSERT INTO chamados (club, project, function, description, priority) 
SELECT club, @proj, @func, @description, @pri
FROM YourTable
Michael Fredrickson
  • 36,839
  • 5
  • 92
  • 109
  • I tried... But I fail =\ INSERT INTO chamados (clube, projeto, funcionalidade, descricao, prioridade) SELECT clube FROM clubes WHERE clube = @club, @proj, @func, @descricao, @pri)"; Then I Passed all these values using `parameter` – Ghaleon Jan 21 '13 at 16:36
  • 1
    @Ghaleon Move the `@variables` into your `select` clause.. they should come before your `from`. – Michael Fredrickson Jan 21 '13 at 16:37
  • Worked just perfect !! Thanks @Michael Fredrickson ! :D – Ghaleon Jan 21 '13 at 16:43