I have 3 tables
Table_Ahas a bunch of rowsTable_Bwhere rows are going to be inserted with data fromTable_ATable_Cholds a number (integer) calledcode_number
I have a stored procedure (sp_getNextCode) that selects the current code_number from Table_C, creates and returns a varchar code string with this number (like yyyyMMdd + cast(code_number as varchar) or something) and updates the Table_C code_number with the next value (code_number+1)
So far so good.
Now I want to insert a number of rows from Table_A to Table_B WITHOUT THE USE OF CURSOR
using a
INSERT INTO TABLE_B
SELECT .... FROM TABLE_A
Again so far so good
The problem is that one of the values in the above insert statement has to be the output of the stored procedure sp_getNextCode.
- I cannot use the stored procedure in the statement
- I cannot create a function with the same code as
sp_getNextCodeas the functions cannot haveINSERT/UPDATE/DELETE
I don't have the option of SQL Server 2012 (which has a sequence) only SQL Server 2008
Is there any way to achieve this or the only way is with cursors (I REALLY want to avoid the cursor cause im talking about thousands of rows that need to be inserted and it takes too long )
sp_prefix for your stored procedures, since that prefix has been reserved by Microsoft for its own use. Try to avoid it whenever you can