使用sql語句實現同時向主表和子表插入數據方法:sql
Oracle:oracle
-- oracle建立sequence create sequence SEQ_test minvalue 1 maxvalue 99999999999999999999999999 start with 21 increment by 1 cache 20; -- oracle 獲得新插入數據的ID並使用 DECLARE newID varchar2(50); begin insert into table1(ID,aa,bb) values(SEQ_test.nextval,'7777','8888') RETURNING ID INTO newID; COMMIT; insert into table2(ID,pid) values(SEQ_test.nextval,newID); end;
SQL Server:ide
-- SQL Server 獲得新插入數據的ID並使用 DECLARE @id int insert into table1(name,password) values (...) set @id=@@identity --取到剛插入的id insert into table2(age,sex,userid) values (...@id)