I have a number formate as follows 1500 and after an insert of a record, i want the number to go up to 1501.
here is my table:
CREATE TABLE QuoteTable2
(
QuoteID int IDENTITY (1,1) NOT NULL
, QNumber varchar(1500),
);
here is my trigger:
create TRIGGER tr_no ON quotetable2
AFTER INSERT
AS
BEGIN
UPDATE QuoteTable2
SET QNumber = inserted.QNumber +1
FROM inserted
WHERE quotetable2.QuoteID = inserted.QuoteID;
END
GO
my results are always the same number as below:
QuoteID QNumber
1 1501
1 1501
how can i modify the trigger to increment by 1 each time? in the example the quoteID and Qnumber are both the same - sorry for the formatting
You are updating the value from the inserted value, which is 1500, so it is always 1501.
You need to get the value from the source table QuoteTable2 not from the inserted:
UPDATE q
SET q.QNumber = q.QNumber +1
FROM QuoteTable2 as q
INNER JOIN inserted AS i ON q.QuoteID = i.QuoteID;
I suggest to declare that column as a IDENTITY column instead so that it will automatically be incremented.
you can try the next code:
ALTER TRIGGER tr_no ON quotetable2
AFTER INSERT
AS
BEGIN
DECLARE #COUNT INT = (SELECT COUNT(1) FROM quotetable2)
UPDATE QuoteTable2
SET QNumber = inserted.QNumber + #COUNT -1
FROM inserted
WHERE quotetable2.QuoteID = inserted.QuoteID;
END
GO
i count the rows on the table and use the value to get the increment, regards
Related
Table is EmployeeTest
Column EmployeeCode has a Unique Index where it can allow NULL but cannot be duplicate. It is varchar (16)
I want to update the EffectiveDate column
I want to create a trigger for whenever I insert or update a row in the table, if EmployeeCode is NULL, then do not update EffectiveDate.
If a row is created or updated and EmployeeCode is not null, I want the update trigger to set EffectiveDate to GETDATE()
I want it to update EffectiveDate just for the row being edited or inserted.
I cannot get this to work. I keep getting "incorrect syntax near the word 'EmployeeCode'
Can anyone assist me with this?
After Update, Insert
AS BEGIN
IF ##ROWCOUNT = 0 RETURN
SET NOCOUNT ON;
IF COLUMN EmployeeCode = NULL RETURN;
UPDATE EmployeeTest
Set EffectiveDate = GETDATE()
FROM EmployeeTest D
join inserted i on D.id = i.id
END
You can simply update only the rows that have a non-null value:
update t set EffectiveDate = Getdate()
from inserted i join EmployeeTest t on t.id = i.id
where i.EmployeeCode is not null;
And remove the If column line.
I'm using databases to connect with PowerApps and PowerBI and I had a question about triggers.
I have a table (Table A) that contains three columns: ID, TotalQty & Date. I would like to create a trigger based on the three main row actions: Insert, Delete & Update.
Example: New row is inserted into Table A from PowerApps (ID = 1000000 & TotalQty = 3 & Date = Today)
This should fire the trigger three times to insert a row into Table B (with rows ID, QrderQty, and Date):
ID = 1000000, OrderQty = 1 of 3, Date = Today
ID = 1000000, OrderQty = 2 of 3, Date = Today
ID = 1000000, OrderQty = 3 of 3, Date = Today
Similarly, if the date column is updated on Table A for this row, I need the three corresponding rows to update their respective date values as well. Or if the row in Table A is deleted, I need the three rows to be removed.
Could anybody give me an example query of this?
You need to create a Trigger first this link contains some tutorial on how to create a trigger.
Then according to your question you need to do some kind of loop for generating values for the OrderQty, I have done a while loop in this answer that exists as part of TSQL
Assuming that you create TableA and TableB with following schemas:
create table [dbo].[TableA]
(
ID integer not null,
TotalQty integer not null,
Date Date not null
)
go
create table [dbo].[TableB]
(
ID integer not null,
OrderQty nvarchar(10) not null,
Date Date not null
)
go
You can create the trigger as follows to do the job for you:
CREATE TRIGGER [dbo].[InsertFromAToB]
ON [dbo].[TableA]
AFTER INSERT
AS
BEGIN
Declare #counter as integer;
set #counter=1;
Declare #qty as integer;
set #qty = (Select TotalQty from inserted);
While(#counter <= #qty)
Begin
Insert into TableB(ID,OrderQty,Date) select ID,(CONVERT(nvarchar(10),#counter) + ' of ' + Convert(nvarchar(10),#qty)) as OrderQty,Date From inserted
set #counter=#counter+1;
END
END
I have a table in sql server with ID field as the primary key. In the rows of the ID field, some have primary key values while some rows do not have primary key values because the ID column allows null. Now I want to run a query to insert values incrementally into the rows that are null so that they can have primary key values. I have tried using an ALTER command but no head way
because you didn't provide any table structure description and we don't know if there are any business key or some unique combinations of data exists to identify a row without primary key then the easiest way, imho, is to use update cursor:
begin tran
-- rollback
-- commit
select * from [Table_1] where id is null
declare #Id int, #i int = 0
,#MaxId int
set #MaxId = (select Max(Id) from [Table_1] )
declare Update_cur cursor local
for select Id from [Table_1] where id is null
for update of Id
open Update_cur
fetch next from Update_cur into #Id
while ##FETCH_STATUS = 0 begin
set #i += 1
update [Table_1] set Id = #MaxId + #i where CURRENT OF Update_cur
fetch next from Update_cur into #Id
end
close Update_cur
deallocate Update_cur
select * from [Table_1] order by Id
P.S. don't forget to commit or rollback transaction after performing tests
You can DROP that column and ADD again with Auto Increment value.
ALTER TABLE your_table DROP COLUMN ID
ALTER TABLE your_table ADD ID INT IDENTITY(1,1)
This will generate all values from the start and as a result you will lose existing value (upto 6).
I have a procedure for insert in multiple dependent tables (update in case record exist). I have input parameters as comma separated string which I am reading in table.
After 1st insertion I am getting InsertedIds in another table variable.
I am struggling over how to do insert in 2nd table. I have following input parameters for 2nd table:
Declare #IdsToBeUpdated table (primary key identity pkey, id int) -- values are 1,-1,3,-1
Declare #CommentsTobeInserted table( primary key identity pkey, comment varchar (max)) -- values are 'com1', 'com2', 'com3'
-1 input in table #IdsToBeUpdated depicts insertion for the corresponding rows in all input tables and value other than -1 depicts that records at that pkey value in all other tables (#CommentsTobeInserted table as in example) have to be updated.
So after first insertion I am getting the inserted ids for rows with -1 value. #InsertedIds = 4,5
So my Ids list logically would become.. 1,4,3,5.
But I am stuck now how to insert/update records in second table respectively.
2nd table would be like follows:
Pkey Primary key identity, commentIds(coming from inserted ids and #IdsToBeUpdated), comments.
I have added one more InsertedIds column in #CommentsTobeInserted. If I could fill it with right InsertedId against correct row, I guess I would be able to do insert/update in 2nd table. And where the value is Id in new column I would do insert, where it's null, I would perform update using #IdsToBeUpdated table.
But currently none of my approach is working. I am new to SQL. Any help would be highly appreciated.
Following is some portion of script for better understanding. I have added first insertion script as well.
USE [Demo]
GO
/****** Object: StoredProcedure [dbo].[USP_NewRequest_Insert] Script Date: 2/11/2016 2:50:34 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[USP_NewRequest_Insert]
-----------------------------------
--------- SomeInput parameters
---------for first insertion omitted
--------------------------------
#IdsToBeUpdated varchar(MAX), --Values are 23|-1|32|-1|-1
#CommentList AS VARCHAR(MAX), --Values 'C2'|'C4'|'C5'|'C6'|'C7'
#MCodeList VARCHAR(MAX), --Values 'M2'|'M4'|'M5'|'M6'|'M7'
#CustomerIdsList VARCHAR(MAX), ----Values 'Cust2'|'Cust4'|'Cust5'|'Cust6'|'Cust7'
#ReturnValue AS INT OUTPUT,
------------------Data is in the order it has to be inserted.. where IDs exist in #IdsToBeUpdated, updation would be done. For -1 values, insertion.
AS
BEGIN
BEGIN TRANSACTION
--------------------------Split input strings aand insert in Table variable----------------------------
declare #MCodes Table (pkey int primary key identity, MCode varchar(20))
insert into #MCodes select s.Item from [dbo].UDF_SplitString(#MCodeList, '|') s
declare #CusCodes Table (pkey int primary key identity, CusCode varchar(200))
insert into #CusCodes select s.Item from [dbo].UDF_SplitString(#CustomerIdsList, '|') s
declare #ReqDetailsIds Table (pkey int primary key identity, Id Int)
insert into #ReqDetailsIds select Convert(INT,RTRIM(LTRIM(s.Item))) from [dbo].UDF_SplitString(#IdsToBeUpdated, '|') s
where s.Item is not null and RTRIM(LTRIM(s.Item)) <>''
declare #ProductComments Table (pkey int primary key identity, Comment Varchar(max), insertedId int null)
insert into #ProductComments(Comment) select s.Item from [dbo].UDF_SplitString(#CommentList, '|') s
DECLARE #intErrorCode int;
------------------------------------------------------------------------------------------------------------
-----------------First Insertion which returns inserted IDs for 2nd insertion
------------------------------------------------------------------------------------------------------------------
---Insert/Update product details in [RequestDetails]
Declare #InsertedIDList Table (pkey int primary key identity, ID int); --------Table to read inserted Ids. Used in query below
-----------------------Insert query in case Detail id = -1
INSERT INTO [dbo].[RequestDetails]
[MCode]
,[CustomerIds]
,[ExpectedVolume]------Some parameters coming for first insertion in input
,[StatusCode])
Output INSERTED.ReqDetailId Into #InsertedIDList(ID)
SELECT A.MCode, B.CusCode, E.Vol,1
FROM #MCodes A
JOIN #CusCodes B ON B.pkey = A.pkey
JOIN #ExpectedVols E ON E.pkey = A.pkey
JOIN #ReqDetailsIds G ON G.pkey = A.pkey
WHERE G.Id = -1 --If id = -1, insert
---------------------------Update Query for rest records
UPDATE [dbo].[RequestDetails]
SET [MCode] = upd.MCode
,[CustomerIds] = upd.CusCode
,[ExpectedVolume] = upd.ExVol
,[StatusCode] = 1
FROM(
SELECT A.MCode, B.CusCode, E.ExVol, G.Id
FROM #MCodes A
JOIN #CusCodes B ON B.pkey = A.pkey
JOIN #ExpectedVols E ON E.pkey = A.pkey
JOIN #ReqDetailsIds G ON G.pkey = A.pkey
WHERE G.Id <> -1
) upd
WHERE upd.Id = dbo.RequestDetails.ReqDetailId
IF(##Error<>0)
BEGIN
SET #intErrorCode = ##Error
GOTO ERROR
END
ELSE
BEGIN
SET #ReturnValue=1
END
---------------------------------------------------------------------------
----------------------------Now similarly I have to do insert/update in Comments Table. But
----------------------------Comments table has RequestDetails Id column as foreign key. So
----------------------------now the challange is to add the rows with Inserted ID where value was = -1
----------------------------in input. We have got the IDs corresponding to -1 values from above insertion
----------------------------in the #InsertedIDList Table variable
-----------------------------------------------------------------------------------------
----------------------------Following is what I have tried so far. But I am not able to insert
----------------------------correct InsertedId against correct record.
----------------------------------------------------------------------------------------
-----------------------Here I tried to insert the new generated ids against corresponding comments in table variable.
-----------------------So that I can perform insert where value is not null. As NULL would be inserted where new ID has not been created
-----------------------and corresponding updated ID exists in input (Values not equal to -1)
-------------------------------------------------------------------------------------------------
Update #ProductComments set insertedId = i.ID from ---------This query is not working
(select A.pkey, B.id as detailId, row_number() over (order by (select 0)) as row_num from
#ProductComments A
JOIN #ReqDetailsIds B ON B.pkey = A.pkey) as mappedNewIds right join
#InsertedIDList i on i.pkey = mappedNewIds.row_num
where mappedNewIds.pkey = [#ProductComments].pkey
----Insert in CommentsTable for New Comments against request
---------------------------------
INSERT INTO [dbo].CommentsTable
( ReqDetailId, Comments, CreatedOn )
SELECT A.insertedId, A.Comment, GETDATE()
FROM #ProductComments A
where A.insertedId is not null
-----Update Query
------------------------------------------------------------------------------------------
UPDATE [dbo].[CommentsTable]
SET [ReqDetailId] = upd.Id
,[Comments] = upd.Comment
,[CreatedOn] = GetDate()
FROM(
SELECT A.Comment, B.Id
FROM #ProductComments A
JOIN #ReqDetailsIds B ON B.pkey = A.pkey
WHERE A.insertedId is not null
) upd
WHERE upd.Id = dbo.CommentsTable.ReqDetailId
END
select * from CommentsTable;
---------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
IF(##Error<>0)
BEGIN
SET #intErrorCode = ##Error
GOTO ERROR
END
ELSE
BEGIN
SET #ReturnValue=1
END
COMMIT TRANSACTION
ERROR:
IF (#intErrorCode <> 0) BEGIN
SET #ReturnValue = 0;
ROLLBACK TRANSACTION
END
END
I'm selecting data from a table within a trigger (FOR UPDATE). Some rows I'm selecting have been updated by a transaction, that initiated the trigger, and some rows are not. I'd like to write somewhere in the dataset a flag for every row, which value would be calculated as "id IN (SELECT [id] FROM INSERTED)". This flag would show, is a row updated with the last transaction or not.
Is it possible in SQL?
Sure, I can do 2 separate queries, with 2 different conditions, but the trigger perfomance is real bottleneck...
Here's an example for SQL Server:
if object_id('TriggerTest') is not null
drop table TriggerTest
create table TriggerTest (id int identity, name varchar(50), inLastUpdate bit)
insert TriggerTest (name) values ('joe'), ('barrack'), ('george'), ('dick')
go
create trigger dbo.TriggerTestDelete
on TriggerTest
after update
as begin
declare #date datetime
set #date = GETDATE()
update dbo.TriggerTest
set inLastUpdate =
case when id in (select id from inserted) then 1
else 0
end
end
go
update TriggerTest set name = name where id in (1,2)
update TriggerTest set name = name where id in (1,3)
select * from TriggerTest
This prints:
id name inLastUpdate
1 joe 1
2 barrack 0
3 george 1
4 dick 0