Solved

Trigger to update the same table, plus update another table

Posted on 2004-09-16
6
317 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 500 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

773 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