I have a long-lived connection, on which the application creates a temp table and uses it to fetch some dynamic runtime data. To my understanding it should be possible to reference this temp table as long as it is done on the same connection. It is possible indeed when I do a bunch of raw queries, but it is not possible with sprocs. I use ADO.NET.
Am I missing something obvious here?
Works
CREATE TABLE #CustomerNames (CustomerName nvarchar(200) PRIMARY KEY)
DECLARE @CustomerName nvarchar(200)
SET @CustomerName ='Joe Baker'
INSERT INTO #CustomerNames (CustomerName) VALUES (@CustomerNames)
Doesn’t work
EXEC customerNames_createTempTable
EXEC customerNames_addCustomerName 'Joe Baker'
where sprocs encapsulate the queries
EDIT: the solution is to create a temp table outside of a sproc using a query and then do all the manipulations with the table on the same connection using sprocs. This way the temp table doesn’t go out of scope.
It’s been a while since I worked with SQL Server, but as far as I know, temporary tables created within stored procedure only exist for the duration of the procedure execution. In other words, they are dropped when the procedure is finished.