我想用列选择更新用户变量。我得到了这样的错误
SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations
我需要这样的报告:这里是信用卡和借记卡表。余额是用户列
表值
预期结果
我试过这个代码
Declare @Openingbalance decimal(18,8) set @Openingbalance=100 select CreditAmount, DebitAmount, (@Openingbalance + CreditAmount-DebitAmount) as 'Balance' from ACT_Transaction
此结果显示错误。因为未在选择语句中更新期初余额
答案 0 :(得分:2)
这适用于SQL 2008及更高版本
Declare @Openingbalance decimal(18,8) = 100
select CreditAmount,
DebitAmount,
(@Openingbalance + CreditAmount-DebitAmount) as 'Balance'
from ACT_Transaction
答案 1 :(得分:0)
Declare @Openingbalance decimal(18,8)
set @Openingbalance=100
select @Openingbalance as 'Openingbalance', CreditAmount
DebitAmount, (@Openingbalance + CreditAmount-DebitAmount) as 'Balance' from ACT_Transaction
答案 2 :(得分:0)
从技术上讲,您的查询是正确的,但您没有提到您希望将期初余额显示为单独的列或平衡列?
你可以使用子查询做这样的事情
声明@optBal数字(10) 设置@ optBal = 100
选择fldDate, fldTrxType, fldAmount, (选择总和(转换(金钱,当cramt然后-1的情况,否则1结束)* cramt + @optBal) 来自tblTrx 其中fldDate< = trx.fldDate)为Balance 来自tblTrx trx 按fldDate排序