Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can I write a where condition in inserting records like this?
SQL
CREATE PROCEDURE sp_insert_userdetails
(
)
BEGIN
INSERT INTO user_details(`name`,email,`phone no`,`photo`)
VALUES(`name`,`email`,`phone no`,`photo`)
WHERE user_details.city_id=city.id AND user_details.area_id=area.id 
AND user_details.type_id=usertype.id;
)
END;

Actually there are 3 tables city, area and usertype. I have to insert into userdetails table(`name`,email,`phone no`,`photo`) using 3 foreign keys i.e. city_id, area_id and type_id referencing 3 tables.

Anyone having any idea please tell me.

Thanks in advance,
chintamani
Posted
Updated 9-Nov-11 20:44pm
v2

If you want to insert data using 3 tables than you can do something like:

SQL
INSERT INTO user_details(`name`,email,`phone no`,`photo`)
SELECT `name`,`email`,`phone no`,`photo`
FROM user_details
INNER JOIN city ON user_details.city_id=city.id
INNER JOIN area ON user_details.area_id=area.id
INNER JOIN usertype ON user_details.type_id=usertype.id;
 
Share this answer
 
Comments
RaisKazi 10-Nov-11 2:57am    
5ed. Probably OP is looking for this. :)
chinta123 10-Nov-11 3:15am    
is it compulsory to write select statement,actually i dont have to select i want only insert.
No first you should learn basics of "T-SQL".

SQL INSERT INTO Statement

This may help you.

SQL UPDATE Statement
 
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