How to get the value after updating the table using triggers in sql - sql

How to get the value after updating the table using triggers in sql

Having a scenario like this:
CREATE TABLE #tmp (id INT, txt VARCHAR(10))
INSERT INTO #tmp ( id, txt )
VALUES ( 1, 'abc')
You can return the values using OUTPUT (https://msdn.microsoft.com/en-us/library/ms177564.aspx) during an UPDATE like this:
UPDATE #tmp SET
txt = 'xyz'
OUTPUT INSERTED.id, INSERTED.txt
You can return any column from your table - INSERTED will contain new data while DELETED will contain previous data.

In the Update SQL it self you can use OUTPUT.
In the triggers on MS SQL servers there are to temp tabels called INSERED and DELETED see her for more info: https://msdn.microsoft.com/en-us/library/ms191300.aspx
Hope it helps.

Related

How can I save query results into a new column and save it in the table with SQL Oracle Live?

This is the query:
SELECT ROUND(column_1,0) as new_column_to_save
FROM table_to_save_to
Thank you
if you wanna insert it into a new table :
INSERT INTO NEWTableName ( colName)
SELECT ROUND(column_1,0) as new_column_to_save
FROM table_to_save_to
If you want to update a column within the same table
UPDATE table_to_save_to
SET new_column_to_save = ROUND(column_1,0)
FROM table_to_save_to

Cloning a table definition to a table variable in SQL Server

Is there a way to clone the table definition from an existing table and recreate as a table variable?
DECLARE #TempTable1 TABLE (ID INT, Description VARCHAR(256))
I need to recreate a set of tables with same number of columns and definitions without repeating the DECLARE TABLE statement.
This process is available on MySQL as below.
CREATE TABLE TempTable1 LIKE TempTableMain;
Is it possible to do this is Microsoft SQL Server?
Please note that the actual scenario contains more that 60 columns in the #TempTable and need to create more than 10 instances from the original table.
I am not talking about data insertion or SELECT ion from another table as below. I need to create the table definition.
DECLARE #TempTable TABLE(ID INT, Description VARCHAR(100))
INSERT INTO #TempTable
VALUES (1, 'Test1'), (1, 'Test1');
SELECT *
INTO #TempTable2
FROM #TempTable1
SELECT * FROM #TempTable2
Create a user defined type with the columns of your table, lets say like that:
CREATE TYPE MyTableType AS TABLE (ID INT, Description VARCHAR(256));
And then declare your table variables using this type:
DECLARE #Table1 MyTableType;
DECLARE #Table2 MyTableType;
DECLARE #Table3 MyTableType;
SQL Server management studio gives you the option to create a sql script to create an already existing table.
Right click your table -> script table as -> CREATE To -> New Query Editor window
This way you dont have to write out the whole query every single time.
You could even create a stored procedure which takes as argument the name of your to be created table and run this from a while loop.
You can perform the following command:
SELECT * INTO #MyTable_tmp FROM MyTable
Then modify your MyTable, and copy your data back in. Other approaches I've seen is to create a new table calling it Mytable_Tmp (Not a temp table), which will be your new table.
Then copy your data doing any migrations you need. Then you will drop the original table and do a rename on Mytable.
When you run SELECT * INTO #MyTable FROM MyTable, SQL Server creates a new temporary table called #MyTable that matches each column and data type from your select clause. In this case we are selecting * so it will match MyTable. This only creates the columns it doesn't copy defaults, constraints indexes or anything else.
If you are using table variables, it means that you don't want to use them in long period of time, as they will be "forgotten" after every script completion.
So, easiest in my opinion is to use such construct:
IF OBJECT_ID('tempdb.dbo.#tmpTable', 'U') IS NOT NULL
DROP TABLE #tmpTable;
SELECT * INTO #tmpTable FROM MyPrimaryTable
It creates temporary table exactly like yours, if you want empty table, you can just use:
SELECT * INTO #tmpTable FROM MyPrimaryTable WHERE 1 = 0
Then, temporary table will have exact same schema as your primary table.
You can apply as many times as you need (create as many temporary tables as you need).
You could use regular tables instead of temporary tables as well.
If you want to re-create table after dropping the existing table then you can use the below query.
/*
Create brands table
*/
-- Old block of code
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[TOY].[BRANDS]') AND type in (N'U'))
DROP TABLE [TOY].[BRANDS]
GO
-- New block of code
DROP TABLE IF EXISTS [TOY].[BRANDS]
GO
-- Add new table
CREATE TABLE TOY.BRANDS
(
ID INT NOT NULL,
NAME VARCHAR(20) NULL
)
GO
-- Load the table with data
INSERT INTO TOY.BRANDS (ID, NAME) VALUES
(1, 'Ford'),
(2, 'Chevy'),
(3, 'Dodge'),
(4, 'Plymouth'),
(5, 'Oldsmobile'),
(6, 'Lincoln'),
(7, 'Mercury');
GO

Creating a table, dropping it and referencing the results

I'm creating a table using multiple subselects. So I am getting data from multiple places within one SQL Database.
What I want is to 1. Set the above as a table itself and 2. create a new table using the above results.
So I will call the above [table1].
I just want to know how to set this as a (temporary) table so i can then start to add new script which I can use to the data I have now received in my new table to display.
You might be looking for SELECT INTO clause:
Select (select Project.Number from Project where Project.ProjectID = PurchaseOrder.ProjectID) as [Project],
PurchaseOrder.Number,
(Select GoodsReceipt.ReceiptNumber from GoodReceipt where GoodsReceipt.ReferenceID = PurchaseOrder.PurchaseOrderID) as [GReceipt]
INTO TABLE1
From PurchaseOrder
You can just write SELECT YOURCOLUMNS INTO #TEMP FROM XXXXX
And then you can start a new script by writing
Note: You new script has to been in the same session when you make use of temp tables
DROP TABLE IF EXISTS DBO.TEST --This only works on SQL Server 2016
/* Drop Table */
IF OBJECT_ID('dbo.Test', 'U') IS NOT NULL
DROP TABLE dbo.Test;
Create Table dbo.TEST (
COLUMNS DATATYPE NULL
)
Insert into DBO.TEST
SELECT * FROM #TEMP
DROP TABLE #TEMP

SQL Server use SCOPE_IDENTITY() for insert into with select command

Hello I have one table whose name is
PERSO_DATA(Id,refPerso,,Col1,Col2,Col3,Col4,Col5)
and I have
PERSO_DATA_QUEUE(Id,refPersoData,refVideo)
Firstly I will insert to datas to PERSO_DATA then I will take Id of inserted data and isnsert to another table as refPersoData.
INSERT INTO PERSO_DATA(refPerso,Col1,Col2,Col3,Col4,Col5)
SELECT #refPerso,pa.Col1,pa.Col2,pa.Col3,pa.Col4,pa.Col5 FROM #PERSODATA pa
SET #refPersoData=SCOPE_IDENTITY()
When I write like that i insert all datas but can't take the Id for each data
INSERT INTO PERSO_DATA_QUEUE(refVideoQueue,refPersoData)
SELECT #refVideoQueue,#refPersoData
so code above it add only one.But I want if I have 10 data to insert PERSO_DATA I need to also insert PERSO_DATA_QUEUE table 10 data with inserted Ids.
How can I do this? I couldn't find a solution.
Thanks in advance
Try this code:
CREATE TABLE #temp ([Id] [bigint] NOT NULL);
INSERT INTO PERSO_DATA(refPerso,Col1,Col2,Col3,Col4,Col5)
OUTPUT INSERTED.Id
INTO #temp (Id)
SELECT #refPerso,pa.Col1,pa.Col2,pa.Col3,pa.Col4,pa.Col5
FROM #PERSODATA pa;
INSERT INTO PERSO_DATA_QUEUE(refVideoQueue,refPersoData)
SELECT #refVideoQueue, Id
FROM #temp;
The solution might be combining either cursor or loop and any iterator - like ROW_NUMBER() in your temp table.
Then - use SCOPE_IDENTITY() in it to get created IDENTITY value.

Insert into Table select result set from stored procedure but column count is not same

I need something like that which is of course not working.
insert into Table1
(
Id,
Value
)
select Id, value from
(
exec MySPReturning10Columns
)
I wanted to populate Table1 from result set returned by MySPReturning10Columns. Here the SP is returning 10 columns and the table has just 2 columns.
The following way works as long as table and result set from SP have same number of columns but in my case they are not same.
INSERT INTO TableWith2Columns
EXEC usp_MySPReturning2Columns;
Also, I want to avoid adding "." as linked server just to make openquery and openrowset work anyhow.
Is there a way not to have define table strucutre in temp table (all columns with datatypes and lenght)? Something like CTE.
You could use a temporary table as a go-between:
insert into #TempTable exec MySP
insert into Table1 (id, value) select id, value from #TempTable
You could solve the problem in two steps by doing the insert from the stored procedure into a temporary table, then do the insert selecting just the columns you want from the temporary table.
Information on temporary tables: http://www.sqlteam.com/article/temporary-tables
-- Well, declare a temp table or a table var, depending on the number of rows expected
-- from the SP. This table will be basically the result set of your SP.
DECLARE #spResult AS TABLE
(
ID INT,
VALUE FLOAT,
....
);
-- Get the result set of the SP into the temp table.
INSERT #spResult EXEC STORED_PROC;
-- Now you can query the SP's result set for ID and Value;
INSERT Table1 (ID, VALUE)
SELECT ID, VALUE FROM #spResult;
You dont need to create a temporary table, you can do it with single query by creating temporary view like this
with tempView as EXEC MySPReturning10Columns insert into Table1 select id, value from tempView
The temporary view disappears as soon as the statement finishes execution