动态SQL - 通过执行存储过程插入到#temp表中

时间:2021-12-06 20:11:26

I'm having an issue that's very slightly differently from others that have already been answered here.

我遇到的问题与已在此处回答的其他问题略有不同。

I have a Stored Procedure that has 1 parameter and is returning different results (number of columns) based on that parameter. In this Stored Procedure, I create a table called #FreightData. The end of that Stored Procedure has something like the following:

我有一个存储过程,它有1个参数,并根据该参数返回不同的结果(列数)。在这个存储过程中,我创建了一个名为#FreightData的表。存储过程的结束具有以下内容:

IF @IncludeIDs = 0 
BEGIN 
    select [Buyer], [Contract], [Product], [Shipowner], [DespatchOrder] from  #FreightData
END
ELSE 
BEGIN
    select * from #FreightData
END

All of the above is working successfully.

以上所有都是成功的。

Then, in another Stored Procedure called [FreightProfile], I'm trying to populate a #temp table with the results from:

然后,在另一个名为[FreightProfile]的存储过程中,我试图使用以下结果填充#temp表:

EXEC [dbo].[EXCEL_FreightData] @IncludeIDs = 1

I've used the exact same CREATE TABLE definition that is in [EXCEL_FreightData] (with a different table name - #FreightProfile_results) and am trying to populated the new temp table using

我使用了[EXCEL_FreightData]中的完全相同的CREATE TABLE定义(具有不同的表名 - #FreightProfile_results)并且我正在尝试使用新的临时表填充

INSERT INTO #FreightProfile_results
EXEC [dbo].[EXCEL_FreightData] @IncludeIDs = 1

However, I'm getting the error:

但是,我收到错误:

Column name or number of supplied values does not match table definition.

列名或提供的值数与表定义不匹配。

I'm trying a solution now using OPENROWSET, so that I don't have to provide the table definition, to get around this issue, but if anyone had any suggestions on the above, it would be much appreciated!

我现在正在尝试使用OPENROWSET的解决方案,这样我就不必提供表定义来解决这个问题,但如果有人对上述内容有任何建议,我将不胜感激!

1 个解决方案

#1


0  

My bad!

我的错!

I had copied the following in the table definition, hence the problem.

我在表定义中复制了以下内容,因此出现了问题。

[ID] [int] identity (1,1) Not Null

#1


0  

My bad!

我的错!

I had copied the following in the table definition, hence the problem.

我在表定义中复制了以下内容,因此出现了问题。

[ID] [int] identity (1,1) Not Null