【发布时间】:2015-04-13 08:01:22
【问题描述】:
我创建了这个程序:
CREATE Procedure CashBook (@startDate DateTime, @endDate DateTime)
AS
BEGIN
Declare @runningTable TABLE(TransDate DateTime, Debit Money, Credit Money, Balance Money)
Declare @closingBalance Money, @runningBalance Money, @openingBalance Money
--Get the opening Balance on the date you want to start at
SELECT
@openingBalance = SUM(coalesce(credit, 0) - coalesce(debit, 0))
FROM
fms.dbo.Transactions
WHERE
DataSource IN (4, 3) AND TransDate < @startDate;
--Now do the rest
INSERT INTO @runningTable (TransDate, Credit, Debit, Balance)
VALUES (@startDate, NULL, NULL, @openingBalance);
SELECT @runningBalance = @openingBalance;
INSERT INTO @runningTable (TransDate, Credit, Debit, Balance)
SELECT
TransDate, Credit, Debit,
(coalesce(credit, 0) - coalesce(debit, 0)) AS Balance
FROM
fms.dbo.Transactions
WHERE
TransDate BETWEEN @startDate AND @endDate;
--Calculate the Running Balance
SELECT
@closingBalance = SUM(coalesce(credit, 0) - coalesce(debit, 0))
FROM
fms.dbo.Transactions
WHERE
DataSource IN (4, 3) AND TransDate < @endDate
--Now do the rest
INSERT INTO @runningTable (TransDate, Credit, Debit, Balance)
VALUES (@endDate, NULL, NULL, @closingBalance)
--Calculate the Running Balance
SELECT * FROM @runningTable
END
当我在 Management Studio 中执行它时,通过调用
cashbook '2014-02-01', '2014-02-01'
我收到此错误:
消息 208,级别 16,状态 1,过程 CashBook,第 8 行
对象名称“事务”无效。
表Transactions存在
编辑
大多数评论者都在询问sp是否在同一个dm中fms,请看下图
【问题讨论】:
-
你能进入创建SP的同一个数据库并运行
SELECT * FROM fms.dbo.Transactions -
fms.dbo.Transactions这个名字对吗? -
@AmeyaDeshpande 我在管理工作室本地破坏数据库是
fms表是Transactions -
@Nick.McDermaid 是的,它有效,请参阅我上面的评论
-
您可以使用单个查询插入数据吗?使用此查询
INSERT INTO @runningTable (TransDate,Credit,Debit, Balance) VALUES (@startDate, NULL, NULL, @openingBalance);
标签: sql sql-server-2008 stored-procedures