Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Trigger to update the same table, plus update another table

Posted on 2004-09-16
6
Medium Priority
?
323 Views
Last Modified: 2008-03-06
Here is my trigger so far :

CREATE TRIGGER trig_addCoupon
ON tStatsUsr
FOR INSERT
AS
-- Get the first 3 characters of the user's Name
DECLARE @coupon VARCHAR(6)
SELECT @coupon = (SELECT LEFT(usrName,3) FROM Inserted)
-- Print first 3 characters of the user's Name
PRINT 'Coupon "' + @coupon + '" added.'
-- Concatenate first 3 characters of the user's Name  with a 3 digit random number
-- Update the same table [tStatsUsr] with newly concatenated variable (usrName + random number)

-- now take the same data and put it in a new table called [tStatsUsr2]
0
Comment
Question by:jkt182
  • 4
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
BillAn1 earned 2000 total points
ID: 12079196
you need to be specific about what you want, but this is the geberal way to do it :
Be warned, this type of trigger will not work if you do a set-based insert. It will only work if you insert one row at a time.

CREATE TRIGGER trig_addCoupon
ON tStatsUsr
FOR INSERT
AS
-- Get the first 3 characters of the user's Name
DECLARE @coupon VARCHAR(6)
SELECT @coupon = (SELECT LEFT(usrName,3) FROM Inserted)
-- Print first 3 characters of the user's Name
PRINT 'Coupon "' + @coupon + '" added.'
-- Concatenate first 3 characters of the user's Name  with a 3 digit random number
DECLARE @RND integer
SET @RND = RAND() * 1000
DECLARE @NEWVAR varchar(6)
SET @NEWVAR = @coupon + RIGHT('000' + cast(@RND AS VARCHAR(3)))
-- Update the same table [tStatsUsr] with newly concatenated variable (usrName + random number)
UPDATE [tStatsUsr] SET ?WHATEVER_COLUMN_YOU_WANT_TO_UPDATE?  = @NEWVAR
WHERE useName = (SELECT useName FROM INSERTED)
-- now take the same data and put it in a new table called [tStatsUsr2]
INSERT INTO [tStatsUsr2] (COL1, COL2,....) VALUES (@NEWVAR, whatever columns you want......)
0
 

Author Comment

by:jkt182
ID: 12079377
Here is my updated Trigger :

I get this error : ERROR 174 : The Right Function Requires 2 Arguments

CREATE TRIGGER trig_addCoupon
ON tStatsUsr
FOR INSERT
AS
-- Get the first 3 characters of the user's Name
DECLARE @coupon VARCHAR(6)
SELECT @coupon = (SELECT LEFT(usrName,3) FROM Inserted)
-- Print first 3 characters of the user's Name
PRINT 'Coupon "' + @coupon + '" added.'
-- Concatenate first 3 characters of the user's Name  with a 3 digit random number
DECLARE @RND integer
SET @RND = RAND() * 1000
DECLARE @NEWVAR varchar(6)
SET @NEWVAR = @coupon + RIGHT('000' + cast(@RND AS VARCHAR(3)))
-- Update the same table [tStatsUsr] with newly concatenated variable (usrName + random number)
UPDATE tStatsUsr SET usr  = @NEWVAR
WHERE useName = (SELECT usrName FROM INSERTED)
-- now take the same data and put it in a new table called [tStatsUsr2]
INSERT INTO Coupons (couponCode) VALUES (@NEWVAR)
0
 

Author Comment

by:jkt182
ID: 12079439
Ok, i figured out the error, and this works !

Thank you so much !
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jkt182
ID: 12079444
here is my final trigger in case others need a real working example :

CREATE TRIGGER trig_addCoupon
ON tStatsUsr
FOR INSERT
AS
-- Get the first 3 characters of the user's Name
DECLARE @coupon VARCHAR(6)
SELECT @coupon = (SELECT LEFT(usrName,3) FROM Inserted)
-- Print first 3 characters of the user's Name
PRINT 'Coupon "' + @coupon + '" added.'
-- Concatenate first 3 characters of the user's Name  with a 3 digit random number
DECLARE @RND integer
SET @RND = RAND() * 1000
DECLARE @NEWVAR varchar(6)
SET @NEWVAR = @coupon + RIGHT('000' + cast(@RND AS VARCHAR(3)),2)
-- Update the same table [tStatsUsr] with newly concatenated variable (usrName + random number)
UPDATE tStatsUsr SET usr  = @NEWVAR
WHERE usrID = (SELECT usrID FROM INSERTED)
-- now take the same data and put it in a new table called [tStatsUsr2]
INSERT INTO Coupons (couponCode) VALUES (@NEWVAR)

0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12079476
close!! but if you want a 3 digit rand number, then take RIGHT('000' + cast(@RND AS VARCHAR(3)),3)
0
 

Author Comment

by:jkt182
ID: 12079771
Thanks ! You've been a great help again !
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question