问题
According to Microsoft's definition, "Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server."
Why is it that the following work:
Create a global temp table (##SomeTempTableName) using SSMS and run a separate query using SSMS a few seconds or minutes later and the second query can see and use the temp table.
Create a global temp table using ADO and run a separate ADO query a few seconds or minutes later and the second query can see and use the temp table.
Create a global temp table using an Access pass-through query and run a separate Access pass-through query a few seconds or minutes later and the second query can see and use the temp table.
But if I create a global temp table in SSMS, ADO or via an Access pass-through query, none of them can see a temp table created by any other method? EDIT: I just created a temp table using an Access pass-through query that SSMS could see.
Note that I am not closing SSMS, closing the ADO connection or closing Access between any of these events.
回答1:
In reference to the title question...
Can Access pass-through queries see global temp tables on SQL Server created using ADO and/or SSMS?
...my testing shows that the answer is "yes". Here's what I did:
After restarting the SQL Server machine I logged in to Windows as Administrator and then logged in to SQL Server locally via SSMS (Windows Authentication). In SSMS I ran the script...
USE [Accounting]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE ##gord(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
...to create the global temp table, then I ran the script...
INSERT INTO ##gord ([Name]) VALUES ('bar')
...to give it a row. Then, with SSMS still running I launched Access and ran the pass-through query...
SELECT * FROM ##gord
...and it returned the row from the global temp table. I could keep running the pass-through query as long as SSMS was running, but as soon as I shut down SSMS the next attempt to run the Access pass-through query resulted in
ODBC--call failed
[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name '##gord'. (#208)
In both the SSMS and the Access sessions I was connecting to the SQL Server as an Administrator (Server Role: sysadmin) so I'm wondering if perhaps you may have a SQL Server permissions issue that is preventing other sessions from seeing the global temp table.
来源:https://stackoverflow.com/questions/18935685/can-access-pass-through-queries-see-global-temp-tables-on-sql-server-created-usi