简体   繁体   中英

Global temp table doesn't exist any more after calling stored procedure with this.ExecuteMethodCall in linq to sql

First I created a temptable calling a stored procedure.

Then while trying to get the results from a second stored procedure from the same dbcontext I am getting an error saying the temp table doesn't exist anymore.

Here is the incomplete code.

    private void GetTempResult()
    {
        var tempTable = "##mytaemptable";
        //  var task = Task.Factory.StartNew(() =>  Services.StartPreparingTempList(clientId,tempTable));
        // execute stored procedure to create temp table 
        Services.StartPreparingTempList(clientId, tempTable); // temptable gets created successfully here .

        // execute stored procedure to get results from the above created temp table.
        var tempResults = Services.GetPartialTempList(tempTable, jtableArgs); //getting error here . As soon as this statement gets executed the temp table ceases to exist. 
    }

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetPartialTempEmailList")]
    public ISingleResult<JournalEmail> GetPartialTempEmailList([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="NVarChar(MAX)")] string tempTableName, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> startIndex, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> maxRowCount)
    {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), tempTableName, startIndex, maxRowCount);
            return ((ISingleResult<JournalEmail>)(result.ReturnValue));
    }

    [global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.StartPreparingTempList")]
    public int StartPreparingTempList([global::System.Data.Linq.Mapping.ParameterAttribute(Name="ClientID", DbType="NVarChar(150)")] string clientID, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="NVarChar(MAX)")] string tempTableName, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> maxRowCount)
    {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), clientID, tempTableName, maxRowCount);
            return ((int)(result.ReturnValue));
    }
}

Do stored procedures get executed in a new connection/session with the below statement? As soon as this statement gets executed, temptable ceases to exist and throws error : invalid object name.

IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), tempTableName, startIndex, maxRowCount);

Can somebody guide me what am I missing?

I guess you are trying to create a global temporary table and insert values values in one store procedure(lets name it as sp1) and trying to retrieve the values in another in sp2(lets name it as sp2). This problem happens in linqtosql( in IExecuteResult result ) while retrieving the second store procedure (sp2). I fixed the issue but changing the returned type of the store procedure (sp2) in the dbml file to a class with the column name as property type which I created in the dbml designer or you can create a view or a table with same return type of the global temp table and add it to the designer. You can then use the view/table as the return type of the store procedure (sp2)

In the picture illustration sp_EditMVC_reconcile_fix_get_no_medical is (sp2) which I used to retrieve the global temp table and reconcile_no_medical_temp is the class I created.

在图片说明中* sp_EditMVC_reconcile_fix_get_no_medical是(sp2)*我用来检索全局临时表,* ​​reconcile_no_medical_temp *是我创建的类。

在图片说明中* sp_EditMVC_reconcile_fix_get_no_medical是(sp2)*我用来检索全局临时表,* ​​reconcile_no_medical_temp *是我创建的类。

Solution 2:

I think its a bug in VS, so I can't find the logic in it and I don't have the time to find right now.

Add the SP2 query in SP1 and use the custom created class as a return type. Then the SP2 will work. Dont know the reason but it will fix it if the above solution fails. Sorry for my bad english. Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM