temp-tables

Getting the avg of the top 10 students from each school

ε祈祈猫儿з 提交于 2019-11-30 17:49:18
问题 We have a school district with 38 elementary schools. The kids took a test. The averages for the schools are widely dispersed, but I want to compare the averages of JUST THE TOP 10 students from each school. Requirement: use temporary tables only. I have done this in a very work-intensive, error-prone sort of way as follows. (sch_code = e.g., 9043; -- schabbrev = e.g., "Carter"; -- totpct_stu = e.g., 61.3) DROP TEMPORARY TABLE IF EXISTS avg_top10 ; CREATE TEMPORARY TABLE avg_top10 ( sch_code

There is already an object named '##Temp' in the database

丶灬走出姿态 提交于 2019-11-30 17:37:07
I have a stored procedure on SQL Server 2000. It contains: select ... into ##Temp ... ... drop table ##Temp When I run the stored procedure with ADO a second time, it prompts: There is already an object named '##Temp' in the database. Could anyone kindly tell me what's wrong? You should re-write your stored proc to drop the temp table if it exists, then you won't ever have this issue IF (SELECT object_id('TempDB..##Temp')) IS NOT NULL BEGIN DROP TABLE ##Temp END Since you chose to use a global temporary table ##Temp , it is visible to all SQL connections at any given time. Obviously, while the

Is it possible to have temp tables in a function?

冷暖自知 提交于 2019-11-30 17:30:34
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. 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 You can also do it with a CTE. See the template browser in SSMS. IntelliSense confuses the issue and will show an error until you complete the CTE and the following insert

There is already an object named '##Temp' in the database

孤街醉人 提交于 2019-11-30 16:42:50
问题 I have a stored procedure on SQL Server 2000. It contains: select ... into ##Temp ... ... drop table ##Temp When I run the stored procedure with ADO a second time, it prompts: There is already an object named '##Temp' in the database. Could anyone kindly tell me what's wrong? 回答1: You should re-write your stored proc to drop the temp table if it exists, then you won't ever have this issue IF (SELECT object_id('TempDB..##Temp')) IS NOT NULL BEGIN DROP TABLE ##Temp END 回答2: You are using a

Difference between createTempview and createGlobaltempview and CreateorReplaceTempview in spark 2.1?

梦想的初衷 提交于 2019-11-30 15:47:33
What is the difference between createTempview and createGlobaltempview and CreateorReplaceTempview in spark 2.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 memory reference to the Dataframe in use. The lifetime for this is tied to the spark session in which the

sp_send_dbmail will not send query results

感情迁移 提交于 2019-11-30 13:51:46
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 You can't query from a temp table using database mail. The session that you used to create the temp table (step 1 of your job I assume) has been closed and a new session started when step 2

How to return temporary table from stored procedure

放肆的年华 提交于 2019-11-30 07:08:10
问题 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

Creating a Primary Key on a temp table - When?

风格不统一 提交于 2019-11-30 06:34:06
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 primary key here? My question is when is the best time to create a primary key on my #TempTable table? I

Pass index to temporary table from regular table?

十年热恋 提交于 2019-11-30 05:27:28
问题 I am creating a temp table with a query like this: CREATE TEMPORARY TABLE temp_table SELECT * FROM regular_table WHERE 1 But regular_table has FULLTEXT index on some of the fields. I try to do a FULLTEXT search on the new temporary table and I get an error telling me "Can't find FULLTEXT index matching the column list ". So obviusly the index is not copying over to the new table. Is there a way to force this? Thanks. 回答1: You could use CREATE TEMPORARY TABLE temp_table LIKE regular_table ,

How to get a table of dates between x and y in sql server 2005

大兔子大兔子 提交于 2019-11-30 02:58:53
问题 I just want a quick way (and preferably not using a while loop)of createing a table of every date between date @x and date @y so I can left outer join to some stats tables, some of which will have no records for certain days in between, allowing me to mark missing days with a 0 回答1: Strictly speaking this doesn't exactly answer your question, but its pretty neat. Assuming you can live with specifying the number of days after the start date, then using a Common Table Expression gives you: WITH