Microsoft
Software
Hardware
Network
Question : Creating a Trigger to increase column value
Hi All,
I haven't a clue how to write a trigger but have a feeling this could provide me with a solution on what I want to achieve:
I have a table called tbl_List_items
Within this table there are columns called
ItemID INT (Primary Key)
ParentItemID INT (foreign key) and
ItemRef INT.
What I need help with is after a new record has been inserted/created I need to select the TOP 1 record based on the new records ParentItemID and insert a value of the selected ItemRef + 1 into the new ItemRef column....
That may not have made a lot of sense but this is an abstract of SQL that I would create within a stored procedure to achieve this:
DECLARE @ItemRef INT
SET @ItemRef = (SELECT TOP 1 [ItemRef]+1 FROM [tbl_List_items] WHERE [ParentItemID ] = @ParentItemID ORDER BY [ItemRef] DESC)
UPDATE [tbl_List_items]
SET
[ItemRef] = @ItemRef
WHERE
ItemID = @ItemID
The reason I can use a stored procedure is that my app also uses ASP.Net's bulk insert which happens directly on the table rather than going through a SPROC. So to me it made sense to try a Trigger.
many thanks,
Rit
Answer : Creating a Trigger to increase column value
The only thing I can thing of would be the use of a CURSOR (which I don't normally recommend) in the trigger.
@Lowfatspread: Any better ideas?
Random Solutions
PHP 5.3.2 installation problem on Vista IIS7
PL/SQL Export table data to .csv file and save; launch Excel inside PL/SQL and load saved .csv file
Should Sys Admins (Domain Admins) also have user accounts?
SQL 2005 Select Query from multiple tables
Variable not defined error using VBA ExportAsFixedFormat Type:=xlTypePDF in excel 2003
Need a random list of 1000 geocoded locations in any one country
FTP not working on Windows Server 2008 IIS 7
Displaying GroupWise contacts in Exchange address book?
How to fix windows xp service pack 3 install error: "The system cannot find the file specified"
what msstd in outlook anywhere used for?