Here is the code snippet
in the application this is my first step
"select rm1.docnumbr DOCNUMBR,rm1.custnmbr CUSTNMBR,rm1.trxsorce TRXSORCE,rm2.docdate DOCDATE,rm2.postdate POSTDATE,rm2.glpostdt GLPOSTDT,PSTUSRID,ORTRXAMT,rm2.DEX_ROW_ID H_DEX_ROW_ID,rm1.DEX_ROW_ID D_DEX_ROW_ID, rm1.dstindx,rm1.debitamt,rm1.crdtamnt,(select rtrim(actnumbr_1) + '-'+rtrim(actnumbr_2) + '-'+rtrim(actnumbr_3) + '-' + rtrim(actnumbr_4) + '-' + rtrim(actnumbr_5) + '-' + rtrim(actnumbr_6) + '-' + rtrim(actnumbr_7) from gl00100 where actindx = rm1.dstindx) 'Account',seqnumbr from rm10101 rm1,rm20101 rm2 where rm2.docdate between convert(datetime,'" + dtFrom.ToShortDateString() + "',103) and convert(datetime,'" + dtTo.ToShortDateString() + "',103 and rm1.docnumbr = rm2.docnumbr and rm1.custnmbr = rm2.custnmbr and rm1.rmdtypal = rm2.rmdtypal and rm1.dstindx in(" + strAcc + ")"
In the above query strAcc,dtFrom,dtTo are user inputs.
the results of the above query are taken into a dataset.
a loop for the above resultset storing the data into a new dataset which is used in bulk copy into a new table as follows.
DataTable DT = new DataTable();
DT = ds.Tables["SRC"];
#region bulkcopy
string strDB = conn.Database.ToString();
SqlBulkCopy bk = new SqlBulkCopy(conn);
bk.DestinationTableName = strDB + ".dbo.gl_sl_cmpr";
bk.WriteToServer(DT);
This code is getting executed with no issue.
Now I have to get records from general ledger and and update the above table gl_sl_cmpr if no updation is done then I have to insert a new row. For this I am writing the below code
SELECT JRNENTRY,trxdate,trxsorce,USWHPSTD,ortrxsrc,orgntsrc,orctrnum,debitamt,crdtamnt,Dex_row_id,actindx,(select rtrim(actnumbr_1) + '-'+rtrim(actnumbr_2) + '-'+rtrim(actnumbr_3) + '-' + rtrim(actnumbr_4) + '-' + rtrim(actnumbr_5) + '-' + rtrim(actnumbr_6) + '-' + rtrim(actnumbr_7) from gl00100 g1 where g1.actindx = g.actindx) 'account', origseqnum FROM GL20000 g WHERE trxdate between convert(datetime,'" + dtMinGLPostDt.ToShortDateString() + "',103) and convert(datetime,'" + dtMaxGLPostDt.ToShortDateString() + "',103) and actindx in (" + strAcc + ") and series <> 2 and correspondingunit <> 1
the result of this query is taken into a dataset and following loop is executed.
for (int i = 0; i < ds.Tables["InGL"].Rows.Count; i++)
{
iJrnEntry = Convert.ToInt32(ds.Tables["InGL"].Rows[i]["JRNENTRY"]);
dtTrxDate = Convert.ToDateTime(ds.Tables["InGL"].Rows[i]["trxdate"]);
strTrxSorce = ds.Tables["InGL"].Rows[i]["trxsorce"].ToString();
strActIndex = ds.Tables["InGL"].Rows[i]["account"].ToString();
strGLUser = ds.Tables["InGL"].Rows[i]["USWHPSTD"].ToString();
strOrgnTrxSorce = ds.Tables["InGL"].Rows[i]["orgntsrc"].ToString();
strOrgnDocNo = ds.Tables["InGL"].Rows[i]["orctrnum"].ToString();
strOrTrxSrc = ds.Tables["InGL"].Rows[i]["ortrxsrc"].ToString();
dclGLDr = Convert.ToDecimal(ds.Tables["InGL"].Rows[i]["debitamt"]);
dclGLCr = Convert.ToDecimal(ds.Tables["InGL"].Rows[i]["crdtamnt"]);
dexrowid = Convert.ToInt32(ds.Tables["InGL"].Rows[i]["Dex_row_id"]);
iGLACT = Convert.ToInt32(ds.Tables["InGL"].Rows[i]["actindx"]);
iGlSeqNo = Convert.ToInt32(ds.Tables["InGL"].Rows[i]["origseqnum"]);
strOrgnDocNo = strOrgnDocNo.Replace("'", "''");
strSource = strOrgnTrxSorce.Trim();
if (strOrgnTrxSorce.Trim() == "")
strSource = strOrTrxSrc;
if(iGlSeqNo !=0)
cmd.CommandText = "update gl_sl_cmpr set JRNENTRY= " + iJrnEntry + ",GLTRXDATE=COnvert(datetime,'" + dtTrxDate.ToShortDateString() + "',103),SOURCE='" + strTrxSorce + "',GLACTINDEX='" + strActIndex + "',USWHPSTD='" + strGLUser + "',ORTRXSRC='" + strOrgnTrxSorce + "',ORGCTRNUM='" + strOrgnDocNo + "',GLDR=" + dclGLDr + ",GLCR =" + dclGLCr + ",gldexrowid=" + dexrowid + " ,glact = " + iGLACT + ", status = 1 where sopno='" + strOrgnDocNo + "' and actindex ='" + strActIndex + "' and trxsorce='" + strSource + "' and seqnumbr=" + iGlSeqNo;
else
cmd.CommandText = "update gl_sl_cmpr set JRNENTRY= " + iJrnEntry + ",GLTRXDATE=COnvert(datetime,'" + dtTrxDate.ToShortDateString() + "',103),SOURCE='" + strTrxSorce + "',GLACTINDEX='" + strActIndex + "',USWHPSTD='" + strGLUser + "',ORTRXSRC='" + strOrgnTrxSorce + "',ORGCTRNUM='" + strOrgnDocNo + "',GLDR=" + dclGLDr + ",GLCR =" + dclGLCr + ",gldexrowid=" + dexrowid + " ,glact = " + iGLACT + ", status = 1 where sopno='" + strOrgnDocNo + "' and actindex ='" + strActIndex + "' and trxsorce='" + strSource + "'";
int cnt = cmd.ExecuteNonQuery();
if (cnt == 0)
{
cmd.CommandText = " insert into gl_sl_cmpr(jrnentry,gltrxdate,SOURCE,GLACTINDEX,USWHPSTD,ORTRXSRC,ORGCTRNUM,GLDR,GLCR,gldexrowid,glact,status) values (" + iJrnEntry + ",COnvert(datetime,'" + dtTrxDate.ToShortDateString() + "',103),'" + strTrxSorce + "','" + strActIndex + "','" + strGLUser + "','" + strOrgnTrxSorce + "','" + strOrgnDocNo + "'," + dclGLDr + "," + dclGLCr + "," + dexrowid + "," + iGLACT + ",2)";
cmd.ExecuteNonQuery();
}
iCount++;
}
After this the below stored procedure is called to set the status
the stored procedure is as below
CREATE PROCEDURE [dbo].[spSetStatus] --@strAcc nvarchar(1000)
AS
BEGIN
declare @SopNo nvarchar(31)
declare @SLAct int
Declare @trxsorce nvarchar(31)
declare @OnAccount decimal
declare @SLDr decimal
declare @SLCr decimal
declare @GLDr decimal
declare @GLCr decimal
declare @jrnEntry int
declare @SLDrSum decimal
declare @SLCrSum decimal
declare @GLDrSum decimal
declare @GLCrSum decimal
declare @Status int
declare cur_status_update cursor for select sopno,act,trxsorce,onaccount,SLDr,SLCr,GLDr,GLCr,jrnentry from gl_sl_cmpr where status not in (2,4)
open cur_status_update
fetch next from cur_status_update into
@SopNo,@SLAct,@trxsorce,@OnAccount,@SLDr,@SLCr,@GLDr,@GLCr,@jrnentry
while @@fetch_status = 0
begin
Set @Status = 1
select @SLDrSum = sum(SLDr),@SLCrSum = sum(SLCr), @GLDrSum = sum(GLDr), @GLCrSum = sum(GLCr) from gl_sl_cmpr where sopno=@SopNo --and convert(nvarchar,act) in (@strAcc)
if (@Onaccount = @SLDrSum or @Onaccount = @SLCrSum )
Set @Status = 1
else
Set @Status = 5
if (@SLDrSum = @GLDrSum and @SLCrSum = @GLCrSum )
Set @Status = 1
else
Set @Status = 6
update gl_sl_cmpr set status = @Status where sopno = @SopNo and act = @SLAct and trxsorce = @trxsorce
fetch next from cur_status_update into
@SopNo,@SLAct,@trxsorce,@OnAccount,@SLDr,@SLCr,@GLDr,@GLCr,@jrnentry
end
close cur_status_update
deallocate cur_status_update
END
Can u suggest the changes to improve the performance.
|