Questions tagged [output-clause]
20 questions
34
votes
2 answers
Using source columns in OUTPUT INTO clause of an INSERT statement (SQL Server)
I am writing a batch processing insert statement and would like to use a temp table to keep track of inserted ID's instead of looping through the items myself and calling SCOPE_IDENTITY() for each inserted row.
The data that needs to be inserted has…
Louis Somers
- 593
- 3
- 8
- 16
24
votes
1 answer
Insert with OUTPUT correlated to sub query table
I am modifying the structure of a database. The content of several columns of the table FinancialInstitution has to be transferred into the table Person. FinancialInstitution is linked to Person with a foreign key. Each FinancialInstitution needs…
Yugo Amaryl
- 433
- 2
- 6
- 9
12
votes
2 answers
Direct the OUTPUT of an UPDATE statement to a local variable
I would like to do this :
DECLARE @Id INT;
UPDATE Logins
SET SomeField = 'some value'
OUTPUT @Id = Id
WHERE EmailAddress = @EmailAddress -- this is a parameter of the sproc
Is this even possible? I know I can declare a local table variable and…
Andrei Rînea
- 778
- 8
- 14
11
votes
2 answers
Trigger on cascading update target doesn't fire when the OUTPUT clause is used
I have 2 tables with the cascade delete rule - [dbo].[Invoices] and [dbo].[InvoiceRows]:
CREATE TABLE [dbo].[Invoices]
(
[InvoiceId] [int] IDENTITY(1,1) NOT NULL,
--other columns
CONSTRAINT [PK_Invoices]
PRIMARY KEY CLUSTERED…
mrigrek74
- 113
- 3
8
votes
3 answers
Can the OUTPUT clause create a table?
I'm doing an update like this:
UPDATE dbo.Table1
SET BirthDate = b.BirthDate
FROM Table1 a
JOIN Table2 b
ON a.ID = b.ID
And I want to use the OUTPUT clause to back up my changes.
UPDATE dbo.Table1
SET BirthDate = b.BirthDate
OUTPUT
…
Kenneth Fisher
- 24,307
- 13
- 63
- 116
8
votes
2 answers
OUTPUT clause returning 0 for newly inserted identity value due to INSTEAD OF trigger
Consider the following minimal, complete, and verifiable example code (see dbfiddle here):
CREATE TABLE [dbo].[test]
(
[i] bigint NOT NULL
identity(1,1)
PRIMARY KEY CLUSTERED
, [d] varchar(10) NOT NULL
);
GO
With an…
Hannah Vernon
- 70,928
- 22
- 177
- 323
7
votes
1 answer
Getting Identity values to use as FK in an INSTEAD OF trigger
I have a series of updateable views we are exposing to end users as the interface for a back end process.
One of these views references two tables and requires an INSTEAD OF trigger for UPDATE and INSERTs.
The structure of the tables is (greatly…
JNK
- 18,064
- 6
- 63
- 98
7
votes
1 answer
OUTPUT clause with window functions
Is there an undocumented restriction in the OUTPUT clause, or is this a bug?
Given the following table:
CREATE TABLE t1 (SomeId int, flag bit, value int);
I would like to use a calculated value in an UPDATE statement, and then use OUTPUT to output…
Charlieface
- 17,078
- 22
- 44
3
votes
1 answer
Could 'Maximum Server Memory' be too low for large batched delete with captured output?
I'm trying to improve the performance of a SQL server database backup program that will create a new database to hold one years worth of report records, delete report records out of the main database, and capture the delete output and use it to…
Zack
- 133
- 5
3
votes
1 answer
Using the result set of a select query as the input of another select
I need to make three select queries over three different tables, using the outputs of each select query, the catch is each one gives multiple results. Here is how I do it.
Select "Title", "Num" from "Table" where "Id" in (
Select "Id" from…
EvsizTospaa
- 33
- 1
- 1
- 3
2
votes
2 answers
Can I use OUTPUT from an UPDATE in an INSERT?
I want to add a record to my app's "SystemSettings" table and set the PK value using the value from an UPDATE. The PK value comes from the "TS_LASTIDS" table which contains the maximum PK value for each table in this application (MicroFocus SBM). …
mnemotronic
- 205
- 3
- 8
2
votes
1 answer
SQL Server - Update with Output not working in Parallel environment
I know that the output parameter does not guarantees parallelism logic.
And the logic I want to implement is failing in a multi thread environment.
To test this, just open 2 windows and execute them side by side as fast as you can. Then compare the…
Dryadwoods
- 211
- 2
- 10
2
votes
1 answer
How do you retrieve the identity value of a row inserted from the OUTPUT of an UPDATE statement?
How do you retrieve the identity value for an inserted row when that row is inserted from the OUTPUT of an UPDATE statement? Neither @@IDENTITY nor SCOPE_IDENTITY() appears to be set properly.
Consider this code:
DECLARE @UpdateTable table…
Riley Major
- 1,965
- 4
- 19
- 37
2
votes
1 answer
How do I load data from a single table to multiple tables & keep referential integrity
Using SQL Server 2008R2.
I have a single table with a bunch of columns. I have built a new database with multiple tables to copy the data too. How do I copy the data, and still keep the relationships?
For example, I have a table with columns such…
BattlFrog
- 183
- 3
- 9
0
votes
2 answers
why sp_ExecuteSql output parameter returns null from linked server when procedure shows correct value in results panel
I am trying to get SQL version from a linked server and save it in a variable, but the query runs fine and showed me the result but when I review the output variable is Null.
DECLARE @cmd NVARCHAR(MAX);
DECLARE @servername NVARCHAR(MAX);
DECLARE…