如何检索在存储过程和 SqlCommand 中创建的#temptable 数据?
How to retrieve #temptable data that was created inside a stored procedure and SqlCommand?
我在这里阅读了很多关于如何重用 SqlCommand
的答案,但其中 none 是这个问题的答案。基本上我有创建 #temptable
.
的存储过程
像这样:
CREATE PROCEDURE [dbo].[ProjectPriorInterestIncome]
@selectedDate Date,
@EndDay Date
AS
BEGIN
CREATE TABLE #LoanPriorProjected
(
Colums here....
)
END
在我的 .Net 中,我有一个执行存储过程的 SqlCommand。像这样:
Using cmd As New SqlCommand("ProjectPriorInterestIncome", SQLConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@SelectedDate", frmDefault.dtDate.Value)
cmd.Parameters.AddWithValue("@EndDay", Format(GetLastDayofMonth(frmDefault.dtDate.Value), "12/31/yyyy"))
cmd.ExecuteNonQuery()
'Executing a select query from #temptable'
cmd.CommandText = "SELECT * FROM #LoanPriorProjected"
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
End Using
现在,当我尝试从 #LoanPriorProjected
table 执行 select 查询时,它说
Invalid object name '#LoanPriorProjected'.
临时 table 是...临时的 - 它们存在于一个连接中。所以当执行过程时,table被创建,然后命令完成,临时table消失了。
您可以使用双哈希 ##temptable
.
使 "more global" 成为临时 table
更好的解释参考this:
The classic temporary table comes in two flavors, the Global, or shareable, temporary table, prefixed by ‘##’, and the local temporary table, whose name is prefixed with ‘#’.The local temporary tables are less like normal tables than the Global temporary tables: You cannot create views on them, or associate triggers with them. It is a bit tricky to work out which process, session or procedure created them. We’ll give you a bit of help with that later. Most importantly, they are more secure than a global temporary table as only the owning process can see it.
如果你想使用临时 table 数据只是 select 临时 table 数据在过程中,即select * from #LoanPriorProjected
您的程序将 return 结果为 table 然后使用数据 table 显示此 table 数据。
我在这里阅读了很多关于如何重用 SqlCommand
的答案,但其中 none 是这个问题的答案。基本上我有创建 #temptable
.
像这样:
CREATE PROCEDURE [dbo].[ProjectPriorInterestIncome]
@selectedDate Date,
@EndDay Date
AS
BEGIN
CREATE TABLE #LoanPriorProjected
(
Colums here....
)
END
在我的 .Net 中,我有一个执行存储过程的 SqlCommand。像这样:
Using cmd As New SqlCommand("ProjectPriorInterestIncome", SQLConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@SelectedDate", frmDefault.dtDate.Value)
cmd.Parameters.AddWithValue("@EndDay", Format(GetLastDayofMonth(frmDefault.dtDate.Value), "12/31/yyyy"))
cmd.ExecuteNonQuery()
'Executing a select query from #temptable'
cmd.CommandText = "SELECT * FROM #LoanPriorProjected"
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
End Using
现在,当我尝试从 #LoanPriorProjected
table 执行 select 查询时,它说
Invalid object name '#LoanPriorProjected'.
临时 table 是...临时的 - 它们存在于一个连接中。所以当执行过程时,table被创建,然后命令完成,临时table消失了。
您可以使用双哈希 ##temptable
.
更好的解释参考this:
The classic temporary table comes in two flavors, the Global, or shareable, temporary table, prefixed by ‘##’, and the local temporary table, whose name is prefixed with ‘#’.The local temporary tables are less like normal tables than the Global temporary tables: You cannot create views on them, or associate triggers with them. It is a bit tricky to work out which process, session or procedure created them. We’ll give you a bit of help with that later. Most importantly, they are more secure than a global temporary table as only the owning process can see it.
如果你想使用临时 table 数据只是 select 临时 table 数据在过程中,即select * from #LoanPriorProjected
您的程序将 return 结果为 table 然后使用数据 table 显示此 table 数据。