I'm writing a method to retrieve the dataset structure as returned by a specified stored procedure.
The code in C# (under development so a little dirty) is as follows:
public static TableInfoList DeriveTableInfo(string linkedServer, string query)
{
string temporaryTable = string.Format("generatorTemp{0}", new Random(DateTime.Now.Millisecond).Next(0, 1000).ToString());
string sql = string.Format("SELECT * INTO {0} FROM OPENQUERY ({1}, \'{2}\')", temporaryTable, linkedServer, query);
new DataAccess().ExecuteCommandText(sql);
TableInfoList derived = new TableInfoList("dbo", temporaryTable);
sql = string.Format("DROP TABLE {0}", temporaryTable);
new DataAccess().ExecuteCommandText(sql);
return derived;
}
With the basic approach being summarisable as:
1) Create a temp table
2) Use OPENQUERY to run the sproc into the table
3) Derive all the necessary info from the temporary table (done by the TableInfoList constructor with a call to sys.sp_columns) and drop it
This works perfectly until I attempt to invoke a sproc which employs a temporary table. The table created by the sproc is not recognised as it is out of scope and I get an error message to the effect of "#WhateverTheTempTableIsCalled is not a valid object."
Given that the method is intended for use in various analysis and code generating tasks based on a vast number of pre-existing sprocs, altering the target sproc is not an option.
Does anyone know of a workround with OPENQUERY or perhaps an alternative to using it?