temp-tables

Is it possible to have temp tables in a function?

↘锁芯ラ 提交于 2019-11-30 00:50:48
问题 Apparently, I can't use them. I'm getting an error message like: Invalid use of a side-effecting operator 'SELECT' within a function If I want to do something like this: select bleh into #temp from Blah ... inside a function. 回答1: No, per this thread where the same question was asked, you cannot, but you can use a table variable DECLARE @MyTempTableVariable TABLE (SCHEMA) INSERT INTO @MyTempTableVariable SELECT bleh FROM bleh 回答2: You can also do it with a CTE. See the template browser in

Difference between createTempview and createGlobaltempview and CreateorReplaceTempview in spark 2.1?

对着背影说爱祢 提交于 2019-11-29 22:55:31
问题 What is the difference between createTempview and createGlobaltempview and CreateorReplaceTempview in spark 2.1 ?? 回答1: Global Temporary View As per documentation, global temporary view are views that are shared among all the sessions, untill all the Spark Application terminates. createorReplaceTempview createTempView (or more appropriately createOrReplaceTempView ) has been introduced in Spark 2.0 to replace registerTempTable , which has been deprecated in 2.0. createTempView creates an in

Inserting data into a temporary table

こ雲淡風輕ζ 提交于 2019-11-29 20:02:18
After having created a temporary table and declaring the data types like so; CREATE TABLE #TempTable( ID int, Date datetime, Name char(20)) How do I then insert the relevant data which is already held on a physical table within the database? alexsuslin INSERT INTO #TempTable (ID, Date, Name) SELECT id, date, name FROM physical_table To insert all data from all columns, just use this: SELECT * INTO #TempTable FROM OriginalTable Don't forget to DROP the temporary table after you have finished with it and before you try creating it again: DROP TABLE #TempTable SELECT ID , Date , Name into #temp

sp_send_dbmail will not send query results

对着背影说爱祢 提交于 2019-11-29 19:48:34
问题 I've tried every avenue on every damn forum suggested, but to no avail! Need to send results of SQLPERF(logspace), that have been stored in a table, via sp_send_dbmail to recipient. Step 2 of job is where failure occurs. Please help! EXEC msdb.dbo.sp_send_dbmail @profile_name= 'MyDBA', @recipients= 'Mack@mydba.co.za', @subject='Log Warning', @query='SELECT * from #TempForLogSpace WHERE LogSpaceUsed >80 回答1: You can't query from a temp table using database mail. The session that you used to

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

懵懂的女人 提交于 2019-11-29 18:48:26
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; I wrestled quite a while with the proper syntax for CREATE TEMPORARY TABLE SELECT. Having figured out a few things, I wanted to share the answers

SQL Server, temporary tables with truncate vs table variable with delete

不羁岁月 提交于 2019-11-29 10:50:08
I have a stored procedure inside which I create a temporary table that typically contains between 1 and 10 rows. This table is truncated and filled many times during the stored procedure. It is truncated as this is faster than delete. Do I get any performance increase by replacing this temporary table with a table variable when I suffer a penalty for using delete (truncate does not work on table variables) Whilst table variables are mainly in memory and are generally faster than temp tables do I loose any benefit by having to delete rather than truncate? Running the followign to scripts, it

Using temp table with exec @sql in stored procedure

半腔热情 提交于 2019-11-29 09:47:37
I have a stored procedure and part of them as below: @DRange is a incoming varchar value declare @sql varchar(max) set @sql = 'select * into #tmpA from TableA where create_date >= getDate - ' + @DRange + '' and is_enabled = 1' exec (@sql) select * from #tmpA The problem is when I execute the stored procedure, an error message occurs: Cannot find the object "#tmpA" because it does not exist or you do not have permissions. Is it not possible to use temp table and execute it or did I do something wrong? #tmpA is created in a different scope, so is not visible outside of the dynamic SQL. You can

db2 equivalent of tsql temp table

青春壹個敷衍的年華 提交于 2019-11-29 07:35:12
How would I do the following TSQL query in DB2? I'm having problems creating a temp table based on the results from a query. SELECT COLUMN_1, COLUMN_2, COLUMN_3 INTO #TEMP_A FROM TABLE_A WHERE COLUMN_1 = 1 AND COLUMN_2 = 2 The error message is: "Error: SQL0104N An unexpected token "#TEMP_A" was found following "". Expected tokens may include: ":". SQLSTATE=42601" You have to declare a temp table in DB2 before you can use it: DECLARE GLOBAL TEMPORARY TABLE SESSION.YOUR_TEMP_TABLE_NAME AS ( SELECT COLUMN_1, COLUMN_2, COLUMN_3 FROM TABLE_A ) DEFINITION ONLY Then populate it: INSERT INTO SESSION

What is the difference between TEMPORARY TABLE and TABLE VARIABLE in SQL 2008?

被刻印的时光 ゝ 提交于 2019-11-29 06:41:43
What is the difference between: CREATE TABLE #temp ( [ID] INT) INSERT INTO #temp SELECT ... and DECLARE @temp TABLE ( [ID] INT) INSERT @temp SELECT ... in SQL Server 2008? Temporary tables are like ordinary tables in most characteristics, except they go into TempDB instead of the current Database, and they dissapear after limited scope, (depending on whether they are session based or global Temp Tables. But all changes to data in Temp tables is logged to the transaction log, with all the performance implications that that entails. otoh, you can also add as many indices or views, or triggers,

Creating a Primary Key on a temp table - When?

萝らか妹 提交于 2019-11-29 05:31:30
问题 I have a stored procedure that is working with a large amount of data. I have that data being inserted in to a temp table. The overall flow of events is something like CREATE #TempTable ( Col1 NUMERIC(18,0) NOT NULL, --This will not be an identity column. ,Col2 INT NOT NULL, ,Col3 BIGINT, ,Col4 VARCHAR(25) NOT NULL, --Etc... -- --Create primary key here? ) INSERT INTO #TempTable SELECT ... FROM MyTable WHERE ... INSERT INTO #TempTable SELECT ... FROM MyTable2 WHERE ... -- -- ...or create