tags:

views:

27

answers:

4

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  A: 
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
adam0101
no i want to hardcode a value 'name' as password....its not coming from table....
sanjay pandey
@sanjay pandey, see my update.
adam0101
+1  A: 

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;
Larry Lustig
it's not working tried
sanjay pandey
what is your rdbms? Are you using mysql or sql server?
Sachin Shanbhag
the 2nd query is working perfecly...........thanks a lot for this help
sanjay pandey
+1  A: 

This is what you're looking for:

INSERT INTO sycle
(
   name
   ,password
)
SELECT  name
        ,NULL -- password
FROM    name_table
Brad
its not working after using -- all the remaining query is commented
sanjay pandey
Yes, the double dash is a comment. Make sure there is a new line after the word password or just remove `-- password` altogether.
Brad
A: 

give this a try

insert into sycle select name,'name' from name_table

bigtang
This will not work if you there are more than two columns in scycle, or if the columns are not in the desired order.
Larry Lustig
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.
bigtang