I want to create a temporary table that's local to the SqlConnection, using a statement of the form "select into #tempTable", because I do not have to specify column names and types, as I would with a "create table #tempTable" statement;
I want the temp table to be visible from subsequent SqlCommands running on the same already-open SqlConnection, and only on that connection. I don't want to use global temporary tables, because I don't want to have to deal with name conflicts when the query is run concurrently.
I discovered a local temp table is visible to subsequent SqlCommands only if it's created with a simple, lone "create table #tempTable" statement with no SQL parameters. On the other hand, if it's created with a "select into" statement with parameters (or strung together with other statements in a single SqlCommand), then that causes the whole thing to be executed in a procedure, which limits the scope of the temp table to that procedure.
In general, it seems impossible to use temp tables across SqlCommands when those SqlCommands are anything other than simple "create table" statements with no parameters, because of how the SqlCommand works internally.
Creating the temp table separately would work (i.e. a simple "create table #tempTable (col type, col type, etc.)" statement with no SQL parameters), but that involves explicitly specifying all the column types (which is too much maintenance, IMO), whereas with a "select into" statement, I don't have to specify the column types because they're matched to the source columns automatically.
I want a temporary table created with "select into" to persist as long as the connection is open. Is there a way to scope the table to the open connection?
As it stands now, it's really unpredictable behavior, because depending on how the SqlCommand is constructed, it may or may not wrap itself in a stored procedure, so a temporary table created within a SqlCommand may or may not be visible to subsequent SqlCommands run on that same open connection.