|
In order to batch process some inserts, I need to be able to use a function within the Insert statement, but I need the function to update a table that maintains the next available value (in this case, a barcode): INSERT Into SomeTable (name, docType, barcode) SELECT Name, DocType, fn_getNextBarcode FROM SomeOtherTable WHERE Criteria = DesiredCriteria where the function would update the barcode table so that the barcode is now increased by 1, then select and return that incremented barcode value. Yes, things would be easy if the vendor had auto incremented the barcode field, but they didn't, and we can't change it. Please help.
(comments are locked)
|
|
Don't forget to wrap up the code in a transaction to ensure consistent behaviour.
(comments are locked)
|
|
You can't update a base table from a function, unfortunately. This is going to prove a bit trickier. It is certainly possible to do this but a simpler alternative would get a temporary table of the barcodes first, and then assign them within the select statement.--all wrapped within a transaction.
(comments are locked)
|
|
Since your select statement might return more than one record, the fact that
is actually a very good news as otherwise, it would allow you to update the base table as many times as there are records affected by the insert istead of doing it only once. This would be horrible. Here is the primitive sceleton of how you can do it. It should work if you have SQL Server 2005 or better. Oleg
(comments are locked)
|
|
Hi, I am not fully understand about your questions. May be that is what you want -
(comments are locked)
|


a function will return a value or a table to be used to update a value, but you will not be able to put code inside the function itself that will manipulate data.