Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.67/5 (5 votes)
See more:
I have three tables
SQL
Fir( FirID,USerID,FirNO,Date,Details)

Login( UserID, UserName, Password......)

Accused( Accusedid,FirID,name,Fathername,Address.............)

I am trying to insert values in accused table... and i am getting FirID value table from Fir table.. and i want to user the where clause and then i want to get the user name from login table.

I am trying query
SQL
insert into Accused(FirID,Name,FatherName,Gender,Phone,Address,Alias,NIC,Age) 
select FirID,'farooq','farooq','farooq' ,'farooq' ,'farooq','farooq','farooq','farooq' 
FROM Fir WHERE UserName in (Select UserName from Login where UserName='Aman')
Posted
Updated 14-Mar-12 3:43am
v5
Comments
Herman<T>.Instance 14-Mar-12 9:37am    
what problem do you run into?
codegeekalpha 14-Mar-12 9:38am    
invalid colum name UserName
Herman<T>.Instance 14-Mar-12 9:41am    
it is named 'Name' if I see your login table declaration at the top
codegeekalpha 14-Mar-12 9:42am    
i want to get userName from login table.. ahh sorry but its user name in my origional database
Prasad_Kulkarni 14-Mar-12 9:43am    
hi, farooqspecials can u pls format your question using 'Improve question' so we can give you exact solution.

you forgot to join the Login table so:
SQL
insert into Accused(FirID,Name,FatherName,Gender,Phone,Address,Alias,NIC,Age)
select FirID,'farooq','farooq','farooq' ,'farooq' ,'farooq','farooq','farooq','farooq'
FROM Fir as f
inner join Login as l on l.UserID = f.UserID
WHERE l.UserName ='Aman'
 
Share this answer
 
Comments
codegeekalpha 14-Mar-12 9:52am    
thanks digimanus
Try this:
SQL
SELECT FirID,'farooq','farooq','farooq' ,'farooq' ,'farooq','farooq','farooq','farooq' 
FROM Fir WHERE Name in (Select Name FROM Login WHERE Name='Aman')
 
Share this answer
 
Comments
codegeekalpha 14-Mar-12 9:45am    
what is that??
Prasad_Kulkarni 14-Mar-12 9:47am    
AS per your question & comment (invalid colum name UserName)
that's only best answer i can give.
Do you still getting same error??
There is no UserName column.
So the where clause in Fir table doesn't know where to find it(UserName
column does not exists for the sql interpreter).
You must use a column from the tables you specified in the from clause(right now there is only the Fir table there).

Try using Name instead of UserName in the first Where condition or add a table that contains the UserName column.
 
Share this answer
 
v3

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