我在存储过程中有一个查询,它工作正常。现在我想添加一个列显示错误。
我的存储过程代码是:
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
它工作正常但是当我添加一个新列t.test
时,它会显示错误
Msg 207,Level 16,State 1,Procedure test,Line 23
列名称'test'无效。
如果有人有想法请与我分享
答案 0 :(得分:1)
我不确定你的桌子是什么样的 看来你正在为你的存储过程添加测试,但它没有添加到你的数据库表中
通过查看错误消息,我可以这么说。希望它有所帮助
答案 1 :(得分:0)
不确定您要执行的操作,但猜测,如果您尝试向存储过程的 输出 添加列,则不在表中存储过程正在从中读取数据,然后您必须将一个文字表达式放入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....
答案 2 :(得分:0)
您收到此错误,因为此查询中不存在列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
但是,如果您要向该查询添加名为test
的列,那么它将成功。如果需要,它可以是'Some literal value' AS test
之类的字符串文字。