Hi kiddie, it depends on the database. In my opinion, if you can do it using ONLY sql, then do so. No need to get caught up in procedural code if it's unnecessary. In Oracle, you can insert records into one table using a query from another table as such:
SQL> describe emp --describe the EMP table--
Name Null? Type
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> describe emp_backup --describe the EMP_BACKUP table--
Name Null? Type
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> -- here are how many records are in emp_backup...
SQL> select count(*) from emp_backup;
COUNT(*)
SQL> insert into emp_backup
2 select * from emp;
14 rows created.
SQL> select count(*) from emp_backup;
COUNT(*)
14
Obviously, you could use an insert with a column list as well, and any type of query in the second half of the INSERT INTO ... SELECT statement...
SQL> create table emp_count_track(
2 cnt number(4),
3 date_counted date
4 )
5 /
Table created.
SQL> insert into emp_count_track( cnt, date_counted )
2 select count(*), sysdate
3 from emp
4 /
1 row created.
SQL> select *
2 from emp_count_track;
CNT DATE_COUN
14 16-JAN-02
If you absolutely MUST use procedural code to do this, I may want to post the type of database and the language you're using so people can better assist. Hope that helps!
sean