在Delphi中使用多个临时表与数据库的ADO连接会产生错误

时间:2010-07-15 19:31:54

标签: sql sql-server delphi ado

我在使用与数据库的ADO连接的临时表时遇到问题。 select into查询运行正常,但似乎他们在每次select into后丢弃表。它们没有开放的记录集,所以我不确定表格为什么会掉线,它不应该打开我能看到的第二个ADO连接。

知道我可以做些什么来防止临时表掉线?

  begin
    // Get Data


    // Build the work tables for case and claims
    // Find CasemmyyyyEOM for prior month
    //

    begin
        cmd := TStringList.Create;
        cmd.Add(
            'select casenumber, fundsavail, totalrcpts, totalobjectn, totaldisbursed, confirmdate, confirmcode into #casemasterwork from CaseMaster');

        cmd.Add(
            'select caseid, claimnumber, splitcode, prinpdtodate, intpdtodate into #claimswork from claims');
        fConnection.Execute(cmd.text);
{$IFDEF DEBUG}
        MessageLog(cmd.text, 'Query');
{$ENDIF}
        cmd.Free;

        // Sum up receipts posted after end of month
        cmd := TStringList.Create;
        cmd.Add(
            'select rcpthistcasekey, amount = sum(rcpthistamt) into #rcpts from rcptshist where ');
        If cbxRunbyDate.Checked = False then
            cmd.Add('substring(rcpthistinputid,1,4) > ''' + copy(sJrnlYM, 3,
                    4) + ''' And rcpthisttrandate > ''' + sMonthBegin + '''')
        Else
            cmd.Add(' rcpthisttrandate > ''' + sMonthEnd + ''' ');
        cmd.Add(' group by rcpthistcasekey');
{$IFDEF DEBUG}
        MessageLog(cmd.text, 'Query');
{$ENDIF}
        fConnection.Execute(cmd.text);
        cmd.Free;

        // Sum up disbursements by case posted after end of month
        cmd := TStringList.Create;
        cmd.Add(
            'select disbcasekey, amount = sum(disbprinamt + disbintamt) into #disb from disbursed where ');
        If cbxRunbyDate.Checked = False then
            cmd.Add('disbrunyrmo > ''' + sJrnlYM + '''')
        Else
            cmd.Add('DisbTrDate > ''' + sMonthEnd + '''');
        cmd.Add('group by disbcasekey');
{$IFDEF DEBUG}
        MessageLog(cmd.text, 'Query');
{$ENDIF}
        fConnection.Execute(cmd.text);
        cmd.Free;

        // Sum up disbursements by case/claim posted after end of month
        cmd := TStringList.Create;
        cmd.Add(
            'select disbcasekey, disbclaimnum, prinamt = sum(disbprinamt), intamt = sum(disbintamt) into #clmdisb from disbursed where');
        If cbxRunbyDate.Checked = False then
            cmd.Add('disbrunyrmo > ''' + sJrnlYM + '''')
        Else
            cmd.Add('DisbTrDate > ''' + sMonthEnd + '''');
        cmd.Add('group by disbcasekey, disbclaimnum');
{$IFDEF DEBUG}
        MessageLog(cmd.text, 'Query');
{$ENDIF}
        fConnection.Execute(cmd.text);
        cmd.Free;

        // back out the disbursements from the claims work table
        cmd := TStringList.Create;
        cmd.Add('update #claimswork set prinpdtodate = prinpdtodate - prinamt, intpdtodate = intpdtodate - intamt from #clmdisb');
        cmd.Add('where disbcasekey = caseid and disbclaimnum = claimnumber + splitcode');
{$IFDEF DEBUG}
        MessageLog(cmd.text, 'Query');
{$ENDIF}
        fConnection.Execute(cmd.text);
        cmd.Free;

        // back out the disbursements from the casemaster work table
        cmd := TStringList.Create;
        cmd.Add(
            'update #casemasterwork set fundsavail = fundsavail - amount, totalrcpts = totalrcpts - amount from #rcpts');
        cmd.Add('where casenumber = rcpthistcasekey');
{$IFDEF DEBUG}
        MessageLog(cmd.text, 'Query');
{$ENDIF}
        fConnection.Execute(cmd.text);
        cmd.Free;

        // back out the receipts from the casemaster work table
        cmd := TStringList.Create;
        cmd.Add(
            'update #casemasterwork set fundsavail= fundsavail + amount, totaldisbursed = totaldisbursed - amount from #disb');
        cmd.Add('where casenumber = disbcasekey');
{$IFDEF DEBUG}
        MessageLog(cmd.text, 'Query');
{$ENDIF}
        fConnection.Execute(cmd.text);
        cmd.Free;

        // Set the confirmcode based on the confirm transfer table
        Try

            cmd := TStringList.Create;
            cmd.Add('update #casemasterwork set confirmcode = ''U''');
            cmd.Add(
                'from confirmdxfer, #casemasterwork  where #casemasterwork.casenumber = confirmdxfer.casenumber');
            cmd.Add('and xferdate > ''' + sMonthEnd + '''');
            // del 11-28-2004 ebs   readded 10/25/2005 esatori
            // cmd.Add('and xferdate between ''' + sMonthBegin + ''' and ''' + sMonthEnd + ''' '); // 11-28-2004 ebs
{$IFDEF DEBUG}
            MessageLog(cmd.text, 'Query');
{$ENDIF}
            fConnection.Execute(cmd.text);
            cmd.Free;
        Except
        End;

        cmd := TStringList.Create;
        cmd.Add('select TotRcpts = sum(amount) from #rcpts');
{$IFDEF DEBUG}
        MessageLog(cmd.text, 'Query');
{$ENDIF}
        rs := fConnection.Execute(cmd.text);
        cmd.Free;
        rs.MoveFirst;
        cTotalCurRcpts := rs.Fields.Item('TotRcpts').Value;
        rs.Close;

4 个答案:

答案 0 :(得分:2)

我的Delphi知识非常非常过时 - 这是否有可能使用任何类型的连接池?您可以通过运行验证 EXEC sp_who2并查看您的应用是否有多个连接。

如果有,并且您100%确定一次只运行其中一个操作,那么将临时表从#casemasterwork切换到## casemasterwork,这将使它们成为全局临时表,从而可以从任何连接访问。 / p>

记得放弃它们!

答案 1 :(得分:2)

经过对此问题的大量研究后,我找到了答案。 ADO倾向于打开与DB的第二个隐藏连接。为避免这种情况,请确保在执行临时表之前关闭所有记录集,并确保关闭临时表逻辑中使用的任何记录集。

答案 2 :(得分:0)

而不是让“select ... into #TempTable ...”隐式创建临时表,尝试执行一条SQL语句,先显式创建表“create table #TempTable(...)”,然后引用那些插入和更新中的表格。

答案 3 :(得分:0)

您的fConnection.KeepConnection属性是否设置为True?如果没有,这是你的问题。 (假设fConnection是TADOConnection)

以上代码是否使用了实际代码?你在使用参数化查询吗?如果是这样,那就是关于它的Microsoft KB文章的另一个问题。