I have some sql that selects from multiple tables and does an insert into a single "report" table that I use for generating a crystal report and an electronic file for a client.
So it's something like:
INSERT INTO ReportTable(fields....)
SELECT fields....
FROM Table1 join Table2... etc.
WHERE ...
The report and electronic file need to include record numbers for each item in my report table, starting at 1 and going up. This record number has no relation to the actual data and is basically used to make it easier to match up records in the report to the records in the electronic file.
I could just make sure that I sort the records the same way in the report and in the electronic file generation, then create the record numbers on the fly, but to make sure they match between the report and file, I'd rather include them in the report table.
I could use a cursor to loop through the records after the insert and add this record number value, but I'd rather not if there is a better way to do it.
Is there a way to add some kind of auto-incrementing field to my insert statement?
So it's something like:
INSERT INTO ReportTable(fields....)
SELECT fields....
FROM Table1 join Table2... etc.
WHERE ...
The report and electronic file need to include record numbers for each item in my report table, starting at 1 and going up. This record number has no relation to the actual data and is basically used to make it easier to match up records in the report to the records in the electronic file.
I could just make sure that I sort the records the same way in the report and in the electronic file generation, then create the record numbers on the fly, but to make sure they match between the report and file, I'd rather include them in the report table.
I could use a cursor to loop through the records after the insert and add this record number value, but I'd rather not if there is a better way to do it.
Is there a way to add some kind of auto-incrementing field to my insert statement?