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

Using a global temporary table in this scenario could cause problems as the table would exist between sessions and may result in some problems using the calling code asynchronously. A local temporary table can be used if it defined before calling sp_executesql e.g. CREATE TABLE #tempTable(id int); execute sp_executesql N’INSERT INTO #tempTable SELECT myId FROM … Read more

EF can’t infer return schema from Stored Procedure selecting from a #temp table

CREATE PROCEDURE [MySPROC] AS BEGIN –supplying a data contract IF 1 = 2 BEGIN SELECT cast(null as bigint) as MyPrimaryKey, cast(null as int) as OtherColumn WHERE 1 = 2 END CREATE TABLE #tempSubset( [MyPrimaryKey] [bigint] NOT NULL, [OtherColumn] [int] NOT NULL) INSERT INTO #tempSubset (MyPrimaryKey, OtherColumn) SELECT SomePrimaryKey, SomeColumn FROM SomeHugeTable WHERE LimitingCondition = true … Read more

Creating temporary tables in SQL

You probably want CREATE TABLE AS – also works for TEMPORARY (TEMP) tables: CREATE TEMP TABLE temp1 AS SELECT dataid , register_type , timestamp_localtime , read_value_avg FROM rawdata.egauge WHERE register_type LIKE ‘%gen%’ ORDER BY dataid, timestamp_localtime; This creates a temporary table and copies data into it. A static snapshot of the data, mind you. It’s … Read more

Check if a temporary table exists and delete if it exists before creating a temporary table

I cannot reproduce the error. Perhaps I’m not understanding the problem. The following works fine for me in SQL Server 2005, with the extra “foo” column appearing in the second select result: IF OBJECT_ID(‘tempdb..#Results’) IS NOT NULL DROP TABLE #Results GO CREATE TABLE #Results ( Company CHAR(3), StepId TINYINT, FieldId TINYINT ) GO select company, … Read more