[英]select data from #Temp table after #temp table create in another query in asp.net c#
首先,我首先執行創建帶有數據的#temp表的過程,然后我要#temp與其他表列使用聯接的某些列。 在第二次查詢發生錯誤(#temp對象無效)之后執行第一個查詢
if (con.State == ConnectionState.Closed)
{ con.Open(); }
IsInTransaction = true;
trans = con.BeginTransaction();
da = new SqlDataAdapter("Execute SP_Statement_Temp", con);
da.SelectCommand.Transaction = trans;
DataTable DatTemp = new DataTable();
da.Fill(DatTemp);
SelectString = "Select Distinct #temp.IdentityID, TblMasterTypeOfIdentity.TypeOfIdentity,TblIdentity.IdentityName, '' As 'Opening Balance' , '' As 'Closing Balance' from #temp inner join TblIdentity on TblIdentity.IdentityID=#temp.IdentityID inner join TblMasterTypeOfIdentity on TblMasterTypeOfIdentity.TypeOfIdentityID=#temp.TypeOfIdentityID";
CmdString = SelectString + " " + WhereString + " " + OrderBy;
da = new SqlDataAdapter(CmdString, con);
da.SelectCommand.Transaction = trans;
DataTable datDetail = new DataTable();
da.Fill(datDetail);
trans.Commit();
IsInTransaction = false;
con.Close();
這是因為#temp
表在創建SP后立即被刪除。
存儲過程完成后,將自動刪除在存儲過程中創建的本地臨時表。 該表可以由創建表的存儲過程執行的任何嵌套存儲過程引用。 調用創建表的存儲過程的進程無法引用該表。
https://docs.microsoft.com/zh-cn/sql/t-sql/statements/create-table-transact-sql
如果要創建一個臨時表,可以跨連接使用,則可以使用雙哈希( ##
)而不是單個( #
)。 這將創建一個全局臨時變量,而不是局部臨時變量。 因此,如果您在Execute SP_Statement_Temp
內更改SQL以創建名為##temp
而不是#temp
的臨時變量,則應該能夠在SQL中使用它。
這已經被問過了,參見例如
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.