I have 140,000 INSERT statements I need to run. I am trying to insert the record's into a database table, and then input the created PK's into a temp table which I then return. It looks something like this:
CREATE PROCEDURE sp_MassInsertProcedure
@PassInVariable int
AS
CREATE TABLE #TempTable(OriginalID int IDENTITY(1,1), NewID int);
--These two inserts are essentially repeated 70,000 times
INSERT INTO MyDBTable (something, something, something) VALUES (1, 2, @PassInVariable);
INSERT INTO #TempTable(NewID) SELECT SCOPE_IDENTITY();
SELECT * FROM #TempTable;
DROP TABLE #TempTable;
I have a few other stored procedures just like this that have max 2,000 insert statements and this way works perfectly, but I think because of the number of statements in this particular SP it gives me "Query Finished with Errors" when I try to create the procedure, but doesn't actually create the procedure.
I then added a GO every 200 insert statements, but after the first GO executes the temp table must be released and I get "TempTable is unavailable" error (I also get the same error for the @PassInVariable after the first GO executes). The thing that is strange is that when I place the above explained SQL in a standard script (not in a stored procedure) it works.
So my question is, does anyone know how to persist a temporary table/variable in a single stored procedure that uses multiple batch executions via the GO command?
CREATE PROCEDURE sp_MassInsertProcedure
@PassInVariable int
AS
CREATE TABLE #TempTable(OriginalID int IDENTITY(1,1), NewID int);
--These inserts are essentially repeated 70,000 times
INSERT INTO MyDBTable (something, something, something) VALUES (1, 2, @PassInVariable);
INSERT INTO #TempTable(NewID) SELECT SCOPE_IDENTITY();
GO
INSERT INTO MyDBTable (something, something, something) (1, 2, @PassInVariable);
INSERT INTO #TempTable(NewID) SELECT SCOPE_IDENTITY();
SELECT * FROM #TempTable;
DROP TABLE #TempTable;