insert into orders (OrderNo,P_Id) select 12345,P_Id from persons where LastName='Hansen';
Should do it for you. Michael D. Black Senior Scientist NG Information Systems Advanced Analytics Directorate ________________________________ From: [email protected] [[email protected]] on behalf of David Westbury [[email protected]] Sent: Sunday, August 14, 2011 10:46 PM To: [email protected] Subject: EXT :[sqlite] Insert rows into a foreign key table For the two tables shown here, what sqlite INSERT statement would add a new Order for person Hansen? Is it possible to add a new order with one sql statement or does it require two statements? The only way I have found to add a new order is to first SELECT the P_Id for Hansen from the Persons table. Then the new row containing Hansen's P_Id would be INSERTed into the Orders table. But this seems like a function that sql should be able to perform in one statement. For example, why won't an embedded select statement similar to the following work? INSERT INTO ORDERS (OrderNo, P_Id) values ( 12345, select P_Id from persons where LastName = 'Hansen') thanks _______________________________________________ sqlite-users mailing list [email protected] http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users _______________________________________________ sqlite-users mailing list [email protected] http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

