简体   繁体   English

存储过程中的列无效

[英]Invalid column in stored procedure

I have a query in a stored procedure, it works fine. 我在存储过程中有一个查询,它工作正常。 now I want to add a column the it show error. 现在我想添加一列显示错误。

My stored procedure code is: 我的存储过程代码为:

ALTER PROCEDURE dbo.test
      @SDate DATETIME =Null
    , @EDate DATETIME=Null
    ,@period int=Null        
AS BEGIN         
    SET NOCOUNT ON;

    if @period = 1 
    Begin 
       SELECT 
              t.TotalQuote
            , t.QuoteAmount        
            ,t.avgProbQ
            , t2.TotalOrders
            , t2.OrderAmount       
             ,t3.totalSales
            ,t3.Prob
        FROM (SELECT a = 1) a
        CROSS JOIN (
            SELECT 
                  TotalQuote = COUNT(quoteid)
                , QuoteAmount = SUM(totalamount)
                ,avgProbQ=SUM(CloseProbability)/COUNT(CloseProbability)
            FROM dbo.QuoteBase join dbo.OpportunityBase on dbo.QuoteBase.opportunityid=dbo.OpportunityBase.opportunityid   
            WHERE
              Month(dbo.QuoteBase.CreatedOn)=Month(getdate()) And YEAR(dbo.QuoteBase.CreatedOn)=YEAR(GETDATE())
        ) t
        CROSS JOIN (
            SELECT 
                  TotalOrders = COUNT(salesorderid)
                , OrderAmount = SUM(totalamount) 
            FROM dbo.SalesOrderBase join dbo.OpportunityBase on dbo.SalesOrderBase.Opportunityid=dbo.OpportunityBase.Opportunityid 
         Where Month(dbo.SalesOrderBase.CreatedOn)=Month(getdate()) And YEAR(dbo.SalesOrderBase.CreatedOn)=YEAR(GETDATE())
        ) t2
        CROSS Join(
        SELECT
        TotalSales=COUNT(dbo.OpportunityBase.opportunityid)
        ,Prob=SUM(CloseProbability)/COUNT(CloseProbability)

        FROM dbo.OpportunityBase join dbo.SalesorderBase on dbo.SalesOrderBase.Opportunityid=dbo.OpportunityBase.Opportunityid
            WHERE Month(dbo.OpportunityBase.CreatedOn)=Month(getdate()) And YEAR(dbo.OpportunityBase.CreatedOn)=YEAR(GETDATE())
            And dbo.SalesorderBase.StateCode=4
            )t3
    END

It works fine but when I add a new column like t.test , then it shows error 它工作正常,但是当我添加新列如t.test ,则显示错误

Msg 207, Level 16, State 1, Procedure test, Line 23 消息207,第16级,状态1,程序测试,第23行
Invalid column name 'test'. 无效的列名“ test”。

If anyone has an idea please share with me 如果有人有想法请与我分享

I am not sure what is your table looked like it seems you are adding test to your stored procedure but its not added in your database table 我不确定您的表是什么样子,似乎您正在向存储过程中添加测试,但未将其添加到数据库表中

This is what I can say by looking the error message. 这是我可以通过查看错误消息说的话。 Hope it helps 希望能帮助到你

Not sure what you are trying to do, but guessing, if you are trying to add a column to the output of stored procedure, that is not in the table that the stored procedure is reading data from, then you have to put a literal expression into the select clause, with a defined column name like below: This example uses a string literal, but it can be any datatype... 不确定要执行的操作,但是猜测,如果要在存储过程的输出中添加一列,而不是存储过程从中读取数据的表中的列,则必须放置一个文字表达式到select子句中,并具有如下定义的列名:此示例使用字符串文字,但可以是任何数据类型...

 SELECT 'A String literal to be added to output' As  NewColumnName,
      t.TotalQuote
    , t.QuoteAmount        
    ,t.avgProbQ
    , t2.TotalOrders
    , t2.OrderAmount       
     ,t3.totalSales
    ,t3.Prob
  etc.... 

You're getting this error because the column test does not exist in this query: 您收到此错误,因为此查询中不存在列test

CROSS JOIN (
    SELECT 
          TotalQuote = COUNT(quoteid)
        , QuoteAmount = SUM(totalamount)
        ,avgProbQ=SUM(CloseProbability)/COUNT(CloseProbability)
    FROM dbo.QuoteBase join dbo.OpportunityBase on dbo.QuoteBase.opportunityid=dbo.OpportunityBase.opportunityid   

    WHERE
      Month(dbo.QuoteBase.CreatedOn)=Month(getdate()) And YEAR(dbo.QuoteBase.CreatedOn)=YEAR(GETDATE())
) t

but, if you were to add to that query a column named test then it would succeed. 但是,如果您要向该查询添加名为test的列,则它将成功。 It could be a string literal like 'Some literal value' AS test if necessary. 如有必要,它可以是字符串文字,例如'Some literal value' AS test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM