返回存储过程的结果时为什么没有结果?

时间:2016-06-08 21:12:29

标签: sql-server stored-procedures temp-tables openrowset

在构建包括几个临时表和存储过程被调用然后联合的第一步中,我有第一个小步骤(根据Philip Kelley的答案here):

IF OBJECT_ID ( 'RockBottomAmalgamated', 'P' ) IS NOT NULL   
    DROP PROCEDURE RockBottomAmalgamated;  
GO
CREATE PROC [dbo].[RockBottomAmalgamated]
    @BegDate datetime,
    @EndDate datetime,
    @SortBy varchar(20)
AS
CREATE TABLE #BaseCraftworksTable
 (
   Unit    varchar(25)            not null
  ,ShortName    varchar(50)
  ,ItemCode  varchar(25)  not null
  ,Description  varchar(100)  not null
  ,Price  varchar(25)
  ,Variance  varchar(25)
  ,VarianceAverage  varchar(10)
  ,PriceWeek  varchar(50)
  ,Week  varchar(10)  not null
 )

INSERT INTO #BaseCraftworksTable (Unit, ShortName, ItemCode, Description, Price,
                                  Variance, VarianceAverage, PriceWeek, Week)
 EXECUTE sp_ViewPriceMatrix_Variance_RockBottomRollup_Craftworks
   @BegDate = @BegDate
  ,@EndDate = @EndDate
  ,@SortBy  = @SortBy

SELECT * FROM #BaseCraftworksTable

...但是调用此SP(RockBottomAmalgamated)不会返回任何内容;但调用“sp_ViewPriceMatrix_Variance_RockBottomRollup_Craftworks”会返回记录。为什么会这样,因为到目前为止这只是将从sp_ViewPriceMatrix_Variance_RockBottomRollup_Craftworks返回的一些数据填充到#BaseCraftworksTable中,然后返回其内容?

更新

尝试使用“FROM OPENROWSET()”爵士乐时使用此代码:

IF OBJECT_ID ( 'RockBottomAmalgamated', 'P' ) IS NOT NULL   
    DROP PROCEDURE RockBottomAmalgamated;  
GO
CREATE PROC [dbo].[RockBottomAmalgamated]
    @BegDate datetime,
    @EndDate datetime,
    @SortBy varchar(20)
AS
CREATE TABLE #BaseCraftworksTable
 (
   Unit    varchar(25)            
  ,ShortName    varchar(50)
  ,ItemCode  varchar(25)  
  ,Description  varchar(100)  
  ,Price  varchar(25)
  ,Variance  varchar(25)
  ,VarianceAverage  varchar(10)
  ,PriceWeek  varchar(50)
  ,Week  varchar(10)  
 )

SELECT * INTO #BaseCraftworksTable FROM OPENROWSET(
   'SQLNCLI', 'Server=PLATYPUSQL42.za.ABCData;Trusted_Connection=yes;', 
   'EXEC sp_ViewPriceMatrix_Variance_RockBottomRollup_Craftworks
       @BegDate = @BegDate
      ,@EndDate = @EndDate
      ,@SortBy  = @SortBy');

SELECT * FROM #BaseCraftworksTable

...我收到了关于“BaseCraftworksTable”已经存在的错误消息。所以我在“CREATE TABLE”之前添加了这个:

IF OBJECT_ID('#BaseCraftworksTable', 'U') IS NOT NULL
  DROP TABLE #BaseCraftworksTable; 

......但它没有帮助。

所以我注释掉了CREATE TABLE部分;这消除了之前的错误,但却取而代之的是:

Error 53: Named Pipes Provider: Could not open a connection to SQL Server [53]. 

OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "Login timeout expired".
OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.".

提供的连接值(“ PLATYPUSQL42.za.ABCData ”)正是我从LINQPad的“连接”下拉列表中选择的(选择了“SQL”)语言下拉列表);它适用于LINQPad;什么是OPENROWSET调用期望作为其第二个参数?

更新2

我显然不是SQL大师,但我惊讶于我可以完全注释掉我的CREATE TABLE语句,重新创建SP,运行它,并且没有关于不存在的表的错误消息。因此,我假设如果表尚未定义,则会根据 INSERT INTO()子句中的参数内的内容自动创建。有意义/好看的功能;但既然如此,明确定义表会有什么好处呢?

注意:我仍然没有得到任何结果;我试过这个想法可能问题在于我如何定义表中的一个或多个字段。但不,除此之外还有其他原因导致问题[s] ......

0 个答案:

没有答案