Accessing TSQL created #temp tables from CLR stored procedure. Is it possible?

为君一笑 提交于 2019-12-07 06:33:25

问题


  1. I have a TSQL Stored Procedure tsql__sp__A which does two things:

(a) Creates a temp table #tempTable that has SELECT data from a complex SELECT query.

(b) Calls a CLR managed Stored Procedure clr__sp__B for each row that does computation on row parameters.

Question: Is it possible to access #tempTable from CLR procedure clr__sp__B using the same connection context? (No, I don't want to move or create another #tempTable inside managed procedure)

Thanks.


回答1:


Thank you Boj.

However I found that when you use with a "context connections=true" it opens up all the SET

Read Bol Article

//The context connection lets you execute SQL statements in the same context that your code was invoked in the first place//

using (SqlConnection connection = new SqlConnection("context connection=true"))
{
    connection.Open();
    // access #temp table
}



回答2:


We can define two types of temp tables in SQL.

  • local
  • global

About local temp tables:

When table is preceded by single ‘#’ sign, it is defined as local temporary table and its scope is limited to session in which it is created.

And about global temp tables:

In contrast of local temporary tables, global temporary tables are visible across entire instance.

So may you should try using "##" to create a global temp table. (If there is a difference between "connection context" and "session")



来源:https://stackoverflow.com/questions/738421/accessing-tsql-created-temp-tables-from-clr-stored-procedure-is-it-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!