Consider the following sql
考虑以下sql
A stored proc called myProc which returns two result sets. Result set 1 returns column1, column2. Result set 2 returns column 3, column 4, column 5.
一个名为myProc的存储过程,它返回两个结果集。结果集1返回column1,column2。结果集2返回第3列第4列第5列。
The following sql will fail since the temp table only has defined 2 int columns.
以下sql将失败,因为临时表只定义了2个int列。
Create Table #temp1(
Column1 int,
Column2 int)
insert into #temp1 exec myProc
My question is is it possible to just insert the first result set into #temp1?
我的问题是可以将第一个结果集插入#temp1吗?
2 个解决方案
#1
2
Old post, but I faced the same problem and although the answers mentioned above are a bit related, the OP's question is about SP that returns multiple sets. The only solution I could find, apart from rewriting the SP to split it into smaller SPs, was to write a SQL CLR
procedure that executes the SP and returns only the required result set. The procedure gets the index of the required result set, executes a SqlCommand
to run the intial T-SQL
SP, then loops through a SqlDataReader
results until it finds the desired result set and returns the corresponding records. The following code is part of the SQL CLR
procedure:
老帖子,但是我遇到了同样的问题,尽管上面提到的答案有点相关,OP的问题是关于SP返回多个集合。除了重写SP以将其拆分为更小的SP之外,我能找到的唯一解决方案是编写执行SP的SQL CLR过程并仅返回所需的结果集。该过程获取所需结果集的索引,执行SqlCommand以运行初始T-SQL SP,然后循环通过SqlDataReader结果,直到找到所需的结果集并返回相应的记录。以下代码是SQL CLR过程的一部分:
SqlDataReader rdr = command.ExecuteReader();
int index = 0;
bool bContinue = true;
while (index < resultSetIndex.Value)
{
if (!rdr.NextResult())
{
bContinue = false;
break;
}
index++;
}
if (!bContinue)
throw new Exception("Unable to read result sets.");
.......
List<SqlMetaData> metadataList = new List<SqlMetaData>();
for (int i = 0; i < rdr.FieldCount; i++)
{
string dbTypeName = rdr.GetDataTypeName(i);
SqlMetaData metadata;
if (dbTypeName.ToLower().Contains("char"))
metadata = new SqlMetaData(rdr.GetName(i), (SqlDbType)Enum.Parse(typeof(SqlDbType), dbTypeName, true), 50);
else
metadata = new SqlMetaData(rdr.GetName(i), (SqlDbType)Enum.Parse(typeof(SqlDbType), dbTypeName, true));
metadataList.Add(metadata);
}
SqlDataRecord record = new SqlDataRecord(metadataList.ToArray());
object[] values = new object[rdr.FieldCount];
if (rdr.HasRows)
{
SqlContext.Pipe.SendResultsStart(record);
while (rdr.Read())
{
rdr.GetValues(values);
record.SetValues(values);
SqlContext.Pipe.SendResultsRow(record);
}
SqlContext.Pipe.SendResultsEnd();
}
#2
2
There's another way
还有另一种方式
SELECT * into #temp
from OPENROWSET('SQLNCLI', 'Server=(local)\\(instance);Trusted_Connection=yes;',
'EXEC (database).(schema).(sproc)')
This'll insert the first resultset into #temp
这会将第一个结果集插入到#temp中
#1
2
Old post, but I faced the same problem and although the answers mentioned above are a bit related, the OP's question is about SP that returns multiple sets. The only solution I could find, apart from rewriting the SP to split it into smaller SPs, was to write a SQL CLR
procedure that executes the SP and returns only the required result set. The procedure gets the index of the required result set, executes a SqlCommand
to run the intial T-SQL
SP, then loops through a SqlDataReader
results until it finds the desired result set and returns the corresponding records. The following code is part of the SQL CLR
procedure:
老帖子,但是我遇到了同样的问题,尽管上面提到的答案有点相关,OP的问题是关于SP返回多个集合。除了重写SP以将其拆分为更小的SP之外,我能找到的唯一解决方案是编写执行SP的SQL CLR过程并仅返回所需的结果集。该过程获取所需结果集的索引,执行SqlCommand以运行初始T-SQL SP,然后循环通过SqlDataReader结果,直到找到所需的结果集并返回相应的记录。以下代码是SQL CLR过程的一部分:
SqlDataReader rdr = command.ExecuteReader();
int index = 0;
bool bContinue = true;
while (index < resultSetIndex.Value)
{
if (!rdr.NextResult())
{
bContinue = false;
break;
}
index++;
}
if (!bContinue)
throw new Exception("Unable to read result sets.");
.......
List<SqlMetaData> metadataList = new List<SqlMetaData>();
for (int i = 0; i < rdr.FieldCount; i++)
{
string dbTypeName = rdr.GetDataTypeName(i);
SqlMetaData metadata;
if (dbTypeName.ToLower().Contains("char"))
metadata = new SqlMetaData(rdr.GetName(i), (SqlDbType)Enum.Parse(typeof(SqlDbType), dbTypeName, true), 50);
else
metadata = new SqlMetaData(rdr.GetName(i), (SqlDbType)Enum.Parse(typeof(SqlDbType), dbTypeName, true));
metadataList.Add(metadata);
}
SqlDataRecord record = new SqlDataRecord(metadataList.ToArray());
object[] values = new object[rdr.FieldCount];
if (rdr.HasRows)
{
SqlContext.Pipe.SendResultsStart(record);
while (rdr.Read())
{
rdr.GetValues(values);
record.SetValues(values);
SqlContext.Pipe.SendResultsRow(record);
}
SqlContext.Pipe.SendResultsEnd();
}
#2
2
There's another way
还有另一种方式
SELECT * into #temp
from OPENROWSET('SQLNCLI', 'Server=(local)\\(instance);Trusted_Connection=yes;',
'EXEC (database).(schema).(sproc)')
This'll insert the first resultset into #temp
这会将第一个结果集插入到#temp中