sql server数据插入,第一行结束余额应该是下一行开放余额

时间:2012-04-05 14:25:44

标签: sql-server

我以这种方式插入数据时遇到了问题。

enter image description here

如何使用存储过程以这种方式插入数据。

INSERT INTO [dbo].tbl_Transaction                
        (                
             [FK_GameID] ,            
             [SpotID] ,            
             TransactionReason ,            
             TransactionType ,            
             TransactionAmount
             --PrevAmountBalance,
            -- CurrentBalance
        )                
       SELECT tblTransaction.Row.value('@FK_GameID','BIGINT'),            
       tblTransaction.Row.value('@SpotID','SMALLINT'),            
       tblTransaction.Row.value('@TransactionReason','SMALLINT'),            
       tblTransaction.Row.value('@TransactionType','Varchar(50)'),            
       tblTransaction.Row.value('@TransactionAmount','MONEY')
       --@OpeningBalance,
       -- (@OpeningBalance-tblTransaction.Row.value('@TransactionAmount','MONEY'))
       FROM @TransactionTable.nodes('/row') AS tblTransaction(Row)   

1 个答案:

答案 0 :(得分:0)

create table #balances(
id int not null identity(1,1),
opening_balance int, 
closing_balance int, 
t_type varchar(50))

启动表(如果你没有这个,你将不得不处理NULL)

insert into #balances values (100 , 100 , 'start')

查询:

insert into #balances values(
(select top 1 closing_balance from #balances order by id desc),
300, --new value
'credit')