temp-tables

What is the difference between a temporary table vs global temporary table in Oracle?

邮差的信 提交于 2019-11-29 02:17:58
问题 I have heard these two terms "temporary table" and "global temporary table" used pretty much in similar context. What is the difference between the two? 回答1: In Oracle there isn't any difference. When you create a temporary table in an Oracle database, it is automatically global, and you are required to include the "Global" key word. The SQL standard, which defines how the term "GLOBAL TEMPORARY TABLE" is interpreted, allows for either a LOCAL or GLOBAL scope. This would allow for either a

How to return temporary table from stored procedure

隐身守侯 提交于 2019-11-29 01:30:14
CREATE PROCEDURE [test].[proc] @ConfiguredContentId int, @NumberOfGames int AS BEGIN SET NOCOUNT ON RETURN @WunNumbers TABLE (WinNumb int) INSERT INTO @WunNumbers (WinNumb) SELECT TOP (@NumberOfGames) WinningNumber FROM [Game].[Game] g JOIN [Game].[RouletteResult] AS rr ON g.[Id] = rr.[gameId] WHERE g.[ConfiguredContentId] = @ConfiguredContentId ORDER BY g.[Stoptime] DESC SELECT WinNumb, COUNT (WinNumb) AS "Count" FROM @WunNumbers wn GROUP BY wn.[WinNumb] END GO This stored procedure returns values from first select statement, but I would like to have values from second select statement to be

Can you define “literal” tables in SQL?

只愿长相守 提交于 2019-11-29 00:50:57
Is there any SQL subquery syntax that lets you define, literally, a temporary table? For example, something like SELECT MAX(count) AS max, COUNT(*) AS count FROM ( (1 AS id, 7 AS count), (2, 6), (3, 13), (4, 12), (5, 9) ) AS mytable INNER JOIN someothertable ON someothertable.id=mytable.id This would save having to do two or three queries: creating temporary table, putting data in it, then using it in a join. I am using MySQL but would be interested in other databases that could do something like that. I suppose you could do a subquery with several SELECT s combined with UNION s. SELECT a, b,

Is MySQL Temporary table a shared resource?

ε祈祈猫儿з 提交于 2019-11-28 21:07:36
I have a MySQL stored procedure that uses a temporary table. Assume that my table name is 'temp' and I use it to store some middle data. It will create at the beginning of procedure, and will drop at the end. CREATE PROCEDURE p() BEGIN CREATE TEMPORARY TABLE \`temp\`(...); INSERT INTO \`temp\` VALUES(...); DROP TEMPORARY TABLE \`temp\`; END; The problem is that this stored procedure may be used by different users concurrently, so I want to know if this can cause any problems (i.e. any conflict in inserted data in temp table). In other word is temp table a shared resource within different calls

SQL Server SELECT INTO and Blocking With Temp Tables

a 夏天 提交于 2019-11-28 18:40:49
So, recently a DBA is trying to tell us that we cannot use the syntax of SELECT X, Y, Z INTO #MyTable FROM YourTable To create temporary tables in our environment, because that syntax causes a lock on TempDB for the duration of the stored procedure executing. Now, I've found a number of things that detail how temporary tables work, scope of execution, cleanup and the like. But for the life of me, I don't see anything about blocking because of their use. We are trying to find proof that we shouldn't have to go through and do CREATE TABLE #MyTable... for all of our temporary tables, but neither

PostgreSQL temporary tables

六月ゝ 毕业季﹏ 提交于 2019-11-28 15:29:57
问题 I need to perform a query 2.5 million times. This query generates some rows which I need to AVG(column) and then use this AVG to filter the table from all values below average. I then need to INSERT these filtered results into a table. The only way to do such a thing with reasonable efficiency, seems to be by creating a TEMPORARY TABLE for each query-postmaster python-thread. I am just hoping these TEMPORARY TABLE s will not be persisted to hard drive (at all) and will remain in memory (RAM),

Create a temporary table in MySQL with an index from a select

若如初见. 提交于 2019-11-28 13:42:35
问题 I have a stored function where I use temporary tables. For performance reasons, I need an index in that table. Unfortunately, I cannot use ALTER TABLE because this causes an implicit commit. Therefore I'm looking for the syntax to add the INDEX for tempid during creation. Can anyone be of help? CREATE TEMPORARY TABLE tmpLivecheck ( tmpid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY ) SELECT * FROM tblLivecheck_copy WHERE tblLivecheck_copy.devId = did; 回答1: I wrestled quite a while with the

Table variable poor performance on insert in SQL Server Stored Procedure

≡放荡痞女 提交于 2019-11-28 11:13:48
We are experiencing performance problems using a table variable in a Stored Procedure. Here is what actually happens : DECLARE @tblTemp TABLE(iId_company INT) INSERT INTO @tblTemp(iId_company) SELECT id FROM ..... The SELECT returns 138 results, but inserting in the TABLE variable takes 1min15 but when I use a temp table with the same SELECT, woops, takes 0sec : CREATE TABLE #temp (iId_company INT) INSERT INTO #temp(iId_company) SELECT id FROM ... What could cause the behavior ? Use a temporary table. You will see much better performance. A detailed explanation for the reasoning behind this is

SQL Insert Into Temp Table in both If and Else Blocks

安稳与你 提交于 2019-11-28 11:01:52
I'm trying to populate a temp table based on the result of a condition in SQL 2005. The temp table will have the same structure either way, but will be populated using a different query depending on the condition. The simplified example script below fails in syntax checking of the ELSE block INSERT INTO with the error of: There is already an object named '#MyTestTable' in the database. DECLARE @Id int SET @Id = 1 IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable IF (@Id = 2) BEGIN SELECT 'ABC' AS Letters INTO #MyTestTable; END ELSE BEGIN SELECT 'XYZ' AS Letters INTO

Execute sp_executeSql for select…into #table but Can't Select out Temp Table Data

那年仲夏 提交于 2019-11-28 10:40:34
Was trying to select...into a temp Table #TempTable in sp_Executedsql. Not its successfully inserted or not but there Messages there written (359 row(s) affected) that mean successful inserted? Script below DECLARE @Sql NVARCHAR(MAX); SET @Sql = 'select distinct Coloum1,Coloum2 into #TempTable from SPCTable with(nolock) where Convert(varchar(10), Date_Tm, 120) Between @Date_From And @Date_To'; SET @Sql = 'DECLARE @Date_From VARCHAR(10); DECLARE @Date_To VARCHAR(10); SET @Date_From = '''+CONVERT(VARCHAR(10),DATEADD(d,DATEDIFF(d,0,GETDATE()),0)-1,120)+'''; SET @Date_To = '''+CONVERT(VARCHAR(10)