在存储过程中使用/输出参数调用存储过程 - 列名无效

时间:2013-04-01 18:49:47

标签: sql-server stored-procedures

我编写了一个使用输出变量的存储过程。存储过程运行正常,我知道这是事实。我可以使用下面的代码段

运行存储过程
DECLARE @return_value int,
    @coEfficientAlpha float

EXEC    @return_value = [dbo].[getTestCoEfficientAlphaByDasIdAndKeyStandard]
    @dasId = 4001,
    @keyStandardId = 960,
    @coEfficientAlpha = @coEfficientAlpha OUTPUT

SELECT  @coEfficientAlpha as N'@coEfficientAlpha'

SELECT  'Return Value' = @return_value

GO

但是当我从我的其他存储过程getTestReliabilityOnDasId中调用此proc时,我收到了

  

列名无效

错误消息。在任何一个程序中我都没有做任何奇怪的事情。有什么想法吗?

如果需要,我会发布更多代码。

编辑:

我的2个存储过程的代码如下:

好的,getTestCoEfficientAlphaByDasIdAndKeyStandard的定义是

ALTER procedure [dbo].[getTestCoEfficientAlphaByDasIdAndKeyStandard]
@dasId int,
@keyStandardId int,
@coEfficientAlpha float output
as

--declare @dasId int
--set @dasId = 4001

--declare @keyStandardId int
--set @keyStandardId = 960

-- used all over query
select qa.*, dq.dasQuestionId
into #temp
from test t 
    inner join question q on t.testId = q.testId
    inner join questionAnswer qa on q.questionId = qa.questionId
    inner join dasQuestion dq on t.dasId = dq.dasId and q.questionNumber = dq.questionNumber
    inner join keyStandardDasQuestion ksdq on dq.dasQuestionId = ksdq.dasQuestionId
where t.dasId = @dasId
    and q.questionTypeId = 2
    and ksdq.keyStandardId = @keyStandardId


-- used later in query
select tb1.DasQuestionId, sum(square(tb1.score - tb2.avgScore))/count(tb1.score) as qSd2
into #questions
from #temp tb1
    inner join (
        select questionId, sum(score)/cast(count(score) as float) as avgScore
        from #temp
        group by questionId
    )tb2 on tb1.questionId = tb2.questionId
group by tb1.dasQuestionId

-- used later in query
select studentNo, sum(score) as studentTestScore, count(score) as cntTestScore
into #testData
from #temp
group by studentNo

-- average number of questions correct on test          
declare @avgTScore float
set @avgTScore = (  
    select sum(studentTestScore) / cast(count(studentTestScore) as float) from     #testData    
    )   

-- average variance of questions right on test
declare @tSd2 float
set @tSd2 = (
        select sum(xMinusMean)/count(xMinusMean) as tSd2
        from (
            select (t.studentTestScore - @avgTScore)*(t.studentTestScore - @avgTScore) as xMinusMean
        from #testData t
        )tb1 

    )

set @coEfficientAlpha = (
    select (count(distinct dasQuestionId)/ (cast(count(distinct dasQuestionId) as     float) - 1))*(1-((select sum(qSd2) from #questions)/@tSd2)) as coEfficientAlpha
    from #temp
)

drop table #temp, #questions , #testdata

并且getTestReliabilityOnDasId的定义是

ALTER procedure [dbo].[getTestReliabilityOnDasId]
@dasId int
as


--declare @dasId int
--set @dasId = 4001

select ROW_NUMBER() OVER(order by ks.keyStandardId asc) AS rowNumber,
    da.dasId, da.title, ks.keyStandardId, ks.keyStandardText, count(dq.dasQuestionId)     as countQuestions
into #temp
from districtAssessment da
    inner join dasQuestion dq on da.dasId = dq.dasId
    inner join keyStandardDasQuestion ksdq on dq.dasQuestionId = ksdq.dasQuestionId
    inner join keyStandard ks on ksdq.keyStandardId = ks.keyStandardId
where da.dasId = @dasId
group by da.dasId, da.title, ks.keyStandardId, ks.keyStandardText


create table #KeyStandards(
    [keyStandardId] int,
    coEfficientAlpha float
)

declare @numRows int
select @numRows = max(rowNumber)
from #temp

declare @keyStandardId int, @rowNumber int
set @rowNumber = 1

DECLARE @return_value int,
        @coEfficientAlpha float,
        @numQuestions int

WHILE @rowNumber <= @numRows
BEGIN
    set @keyStandardId = (
   select keyStandardId
   from #temp
   where rowNumber = @rowNumber )

    set @numQuestions = (select countQuestions from #temp where keyStandardId = @keyStandardId   )

    if @numQuestions > 1
    begin
        EXEC    @return_value = [dbo].    [getTestCoEfficientAlphaByDasIdAndKeyStandard]
                @dasId = @dasId,
                @keyStandardId = @keyStandardId,
            @coEfficientAlpha = @coEfficientAlpha OUTPUT

    end

   insert into #keyStandards
   select @keyStandardId, case when @coEfficientAlpha is null then 'Not Enough     Questions' else @coEfficientAlpha end 

   set @rowNumber = @rowNumber + 1
END

select t.*, k.coEfficientAlpha
from #temp t
    inner join #keyStandards k on t.keyStandardId = k.keyStandardId

drop table #temp, #KeyStandards

我得到的确切错误消息是:

  

Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 33
  列名称'questionId'无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 31
  列名称'questionId'无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 31
  列名称“得分”无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 31
  列名称“得分”无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 34
  列名称'questionId'无效。
  消息207,级别16,状态1,过程getTestCoEfficientAlphaByDasIdAndKeyStandard,第35行
  列名称'dasQuestionId'无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 27
  列名称'DasQuestionId'无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 27
  列名称“得分”无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 27
  列名称“得分”无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 41
  列名称'studentNo'无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 38
  列名称'studentNo'无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 38
  列名称“得分”无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 38
  列名称“得分”无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 66
  列名称'dasQuestionId'无效。
  Msg 207,Level 16,State 1,Procedure getTestCoEfficientAlphaByDasIdAndKeyStandard,Line 66
  列名称'dasQuestionId'无效。

注意:如果我运行getTestReliabilityOnDasId,我会收到这些错误消息。如果我运行getTestCoEfficientAlphaByDasIdAndKeyStandard然后在单独的查询窗口中运行getTestReliabilityOnDasId,那么一切运行正常。它几乎就像SQL Server引擎必须预热一样......

1 个答案:

答案 0 :(得分:4)

您有命名冲突。他们都使用SELECT ... INTO创建一个名为#temp的表。

当存储过程调用另一个存储过程时,嵌套存储过程可以从父级查看临时表。

这个预先存在的#temp表将具有与子过程引用完全不同的列名集,并且不会编译。

如果用其他临时表名替换其中一个过程中对#temp的所有引用,那么你应该停止看到这些编译错误。