本地临时表sql server中的“级别”范围

时间:2015-05-08 09:51:33

标签: sql-server scope temp-tables

我正在读一本提到

的书
  

本地临时表在创建它们的级别中可见,跨批次以及在调用堆栈的所有内层中。因此,如果在代码中的特定级别创建临时表,然后执行动态批处理或存储过程,则内部批处理可以访问临时表。

  

即使在同一级别的批次中,表变量也不可见

从其他来源,据我所知,本地临时表对于创建它的会话是可见的,并在会话关闭时被销毁。

那么什么是“级别”和“调用堆栈的内部级别”呢?

1 个答案:

答案 0 :(得分:2)

Picture a stored procedure called com.$company.DBDataSetException: java.sql.SQLException: Connection refused: connect at com.$company.DBDatabase.setConnection(DBDatabase.java:43) at em.cabbench.$client.ERPInterface$client.connectToConnx(ERPInterface$client.java:137) at em.cabbench.$client.ERPInterface$client.getERPRawMaterialPrice(ERPInterface$client.java:167) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at em.cabbench.RequestTypes.call(RequestTypes.java:1120) at em.cabbench.CabBenchSrv.processRequest(CabBenchSrv.java:988) at em.cabbench.CabBenchSrv.doGet(CabBenchSrv.java:828) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) Caused by: java.sql.SQLException: Connection refused: connect at com.Connx.jdbc.TCJdbc.TCJdbcDriver.connect(TCJdbcDriver.java:251) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.$company.DBDatabase.setConnection(DBDatabase.java:40) ... 23 more Couldn't connect to CONNX java.lang.NullPointerException at com.$company.DBDatabase.closeConnection(DBDatabase.java:66) at em.cabbench.$client.ERPInterface$client.getERPRawMaterialPrice(ERPInterface$client.java:171) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at em.cabbench.RequestTypes.call(RequestTypes.java:1120) at em.cabbench.CabBenchSrv.processRequest(CabBenchSrv.java:988) at em.cabbench.CabBenchSrv.doGet(CabBenchSrv.java:828) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source) than, in turn, calls two stored procedures, server specs: PHP 5.2.4 or greater MySQL 5.0 or greater PHP cURL extension PHP Zlib extension Unix-like OS (Windows is not supported) PHP 5.3, 5.4, 5.5 yes IonCube Loader 4.6.1 or above yes Mysql 5.0 or above and its support in PHP yes FFmpeg 1.0 or above with libx264, libavfilter and AAC codec (libfaac, libfdk_aac or native ffmpeg aac codec) yes cURL and its support in PHP yes Apache + mod_rewrite + (MultiViews option disabled) yes, not sure about multi views, please ask how this is done. Zlib library yes XML extension yes GD2 with true type font (required!) yes Ability to exec PHP from CLI using exec() command yes PHP register_globals off yes PHP magic_quotes_gpc off yes PHP safe_mode off yes PHP file_uploads on yes PHP allow_url_fopen on yes and then A1.

Each of these stored procedures creates a temporary table with the same name, prefixed (of course) by B (so, C creates #) as their first action, before running any further code.

You execute the following code in query analyser:

A

Code in #A can work with tables CREATE TABLE #F (ID INT NOT NULL) EXEC A GO EXEC A and A.

Code in #F can work with tables #A, B and #F

Code in #A can work with tables #B, C and #F

Despite #A and #C being at the same "level" (they were both called from B), C cannot access the temp table that A created (it was destroyed when C exited).


1

B