计算每笔记录余额,请问这条存储过程哪有问题呢?
编译出错,提示:没有找到存储过程'OBJECT_ID'
然后再点编译提示:
消息 102,级别 15,状态 1,过程 sp_GetBalance,第 8 行
'#temptable' 附近有语法错误。
消息 137,级别 15,状态 2,第 8 行
必须声明标量变量 "@userid"。
消息 137,级别 15,状态 2,第 10 行
必须声明标量变量 "@fdate"。
代码如下:
ALTER PROCEDURE [dbo].[sp_GetBalance]
@userid nvarchar(50),
@fdate datetime,
@tdate datetime
AS
BEGIN
if OBJECT_ID('[#temptable]') is not null drop table [#temptable]
go
create table [#temptable]([id] bigint,[userid] nvarchar(50),[receivable] decimal(18, 2),[paid] decimal(18, 2),[balance] decimal(18, 2),[indate] datetime)
insert [#temptable]
SELECT a.id,a.userid, a.receivable, a.paid,
(SELECT SUM(receivable) - SUM(paid) AS Expr1
FROM dbo.FundDetail_out
WHERE (id <= a.id)) AS balance, a.indate
FROM dbo.FundDetail_out a
where userid = @userid
select * from [#temptable] where indate between @fdate and @tdate
END
[解决办法]