insert into #db_ledger (glcd,gldt,glfl,glblno,glblsrno,glamnt,glnarr) values ( @LEDcode, gldt=@fromdate, glfl = "OPENING", glblno='', glblsrno='', GLAMNT = (SELECT SUM(GLAMNT) from genled where glcd = @LEDcode and gldt<fromdate GROUP by glCD), "Opening Balance")Setting the balance is a matter of summing all the records that are less than the date on the current record. You can use a collorated query
UPDATE #db_ledger SET balance = (SELECT SUM(glamnt) FROM #db_ledger d WHERE d.gldt < #db_ledger.gldt)-Mike