Is it possible to insert into two tables at the same time?

In one statement: No. In one transaction: Yes BEGIN TRANSACTION DECLARE @DataID int; INSERT INTO DataTable (Column1 …) VALUES (….); SELECT @DataID = scope_identity(); INSERT INTO LinkTable VALUES (@ObjectID, @DataID); COMMIT The good news is that the above code is also guaranteed to be atomic, and can be sent to the server from a client … Read more

Is it possible to add index to a temp table? And what’s the difference between create #t and declare @t

#tablename is a physical table, stored in tempdb that the server will drop automatically when the connection that created it is closed, @tablename is a table stored in memory & lives for the lifetime of the batch/procedure that created it, just like a local variable. You can only add a (non PK) index to a … Read more

How can I make a SQL temp table with primary key and auto-incrementing field?

You are just missing the words “primary key” as far as I can see to meet your specified objective. For your other columns it’s best to explicitly define whether they should be NULL or NOT NULL though so you are not relying on the ANSI_NULL_DFLT_ON setting. CREATE TABLE #tmp ( ID INT IDENTITY(1, 1) primary … Read more

TSQL Define Temp Table (or table variable) Without Defining Schema?

Actually using a table VARIABLE, an in-memory table, is the optimal way to go. The #table creates a table in temp db, and ##table is global – both with disk hits. Consider the slow-down/hit experienced with the number of transactions. CREATE PROCEDURE [dbo].[GetAccounts] @AccountID BIGINT, @Result INT OUT, @ErrorMessage VARCHAR(255) OUT AS BEGIN SET NOCOUNT … Read more

How to create temp table using Create statement in SQL Server?

Same thing, Just start the table name with # or ##: CREATE TABLE #TemporaryTable — Local temporary table – starts with single # ( Col1 int, Col2 varchar(10) …. ); CREATE TABLE ##GlobalTemporaryTable — Global temporary table – note it starts with ##. ( Col1 int, Col2 varchar(10) …. ); Temporary table names start with … 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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)