0

insert into sycle(name,password) values(select name from name_table,'name');

i am having problem in inserting additional value with the parameter received from the select subquery.

it can be done in two query, but i want to kn ow if it can be done in a single query.

1
  • Where is 'password' coming from? Commented Oct 1, 2010 at 13:33

4 Answers 4

2
insert into sycle(name,password)
select name, password from name_table

or if password is a variable:

insert into sycle(name,password)
select name, @password from name_table

if password is 'name' then:

insert into sycle(name,password)
select name, 'name' from name_table
Sign up to request clarification or add additional context in comments.

1 Comment

no i want to hardcode a value 'name' as password....its not coming from table....
1

Try:

  INSERT INTO sycle(name,password) VALUES (SELECT name, 'name' FROM name_table);

although this will take only single name from name_table. If you want to get a whole bunch of values do:

  INSERT INTO sycle(name,password) SELECT name, 'name' FROM name_table;

1 Comment

what is your rdbms? Are you using mysql or sql server?
1

This is what you're looking for:

INSERT INTO sycle
(
   name
   ,password
)
SELECT  name
        ,NULL -- password
FROM    name_table

2 Comments

its not working after using -- all the remaining query is commented
Yes, the double dash is a comment. Make sure there is a new line after the word password or just remove -- password altogether.
0

give this a try

insert into sycle select name,'name' from name_table

2 Comments

This will not work if you there are more than two columns in scycle, or if the columns are not in the desired order.
Very true but we don't really have access to the structures and I was just pointing him in the proper direction - should have made the code a bit more defensive however. You're right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.