Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two tables one is account table and the another is missinginfo table.

I want to insert the id column from the account table to the missing info table..


here is my query

SQL
insert into missinginfo ( mfname,mlname,id) values ('john','wilson', 'select id from account where username=abc@abc.com')


Query gives no error but the id column give wrong result i.e 0 ..

where is the problem..
Posted

SQL
insert into missinginfo ( mfname,mlname,id) values ('john','wilson', (select id from account where username='abc@abc.com'))
 
Share this answer
 
v2
Comments
[no name] 10-Jan-14 11:51am    
The correct answer. 5 from my side.
Karthik_Mahalingam 10-Jan-14 12:07pm    
Thanks a lot:)
Your insert query is wrong.
Check below tsql query.

SQL
insert into missinginfo ( mfname,mlname,id) select 'john','wilson',id from account where username='abc@abc.com'
 
Share this answer
 
Remove the single quotes from the id-sql

like this
SQL
insert into missinginfo ( mfname,mlname,id) values ('john','wilson', select id from account where username=abc@abc.com)


[EDIT]correction after reading solution #1 from KARTHIK[EDIT]
SQL
insert into missinginfo ( mfname,mlname,id) values ('john','wilson', (select id from account where username=abc@abc.com))



and not like
SQL
insert into missinginfo ( mfname,mlname,id) values ('john','wilson', 'select id from account where username=abc@abc.com')
 
Share this answer
 
v3
Use this

SQL
insert into missinginfo ( mfname,mlname,id) 
select 'john','wilson',id from account where username='abc@abc.com'


and if you want to insert multiple records then use this.
SQL
insert into missinginfo ( mfname,mlname,id) 
select 'john','wilson',id from account where username='abc@abc.com'
union all
select 'john2','wilson2',id from account where username='abc@abc.com'
 
Share this answer
 
You have wrong query there in id section in values.

Putting single quotes around your select statement makes it static string it wouldn't be executed.

Removing those single quote around your select statement will be executed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900