473,395 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trigger - On insert ~ if [column] value = XX, change it to YY, then insert

Hi there.

I'm trying to create a Trigger for on insert, which will be triggered upon any attempted insert into a database table item. Basically want it to say, if the attempted insert value for column A is XXX, then change it to YYY, and proceed with the insert.

Not sure how to do this though. Only noob @ triggers. I managed to do a trigger which AFTER an insert, checks column [info] in item table for value 8 & then sets it to 0.

can anyone tell me how to modify my existing trigger to achieve this?

Expand|Select|Wrap|Line Numbers
  1. ================================================
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. -- =============================================
  7. -- Author:        Name
  8. -- Create date: 
  9. -- Description:    
  10. -- =============================================
  11. CREATE TRIGGER dbo.gmItem 
  12.    ON  dbo.item 
  13.    AFTER INSERT
  14. AS 
  15. BEGIN
  16.     -- SET NOCOUNT ON added to prevent extra result sets from
  17.     -- interfering with SELECT statements.
  18.     SET NOCOUNT ON;
  19.     update item set [info] = '0' where [info] = '8';
  20.     -- Insert statements for trigger here
  21.  
  22. END
  23. GO
  24.  
Sep 17 '06 #1
2 16021
Senthil
10
--declare the variable @info
select @info =info from inserted.
if @info=8
begin
--update statement
end


Cheers,
Sharmila
-------------------------

Hi there.

I'm trying to create a Trigger for on insert, which will be triggered upon any attempted insert into a database table item. Basically want it to say, if the attempted insert value for column A is XXX, then change it to YYY, and proceed with the insert.

Not sure how to do this though. Only noob @ triggers. I managed to do a trigger which AFTER an insert, checks column [info] in item table for value 8 & then sets it to 0.

can anyone tell me how to modify my existing trigger to achieve this?

Expand|Select|Wrap|Line Numbers
  1. ================================================
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. -- =============================================
  7. -- Author:        Name
  8. -- Create date: 
  9. -- Description:    
  10. -- =============================================
  11. CREATE TRIGGER dbo.gmItem 
  12.    ON  dbo.item 
  13.    AFTER INSERT
  14. AS 
  15. BEGIN
  16.     -- SET NOCOUNT ON added to prevent extra result sets from
  17.     -- interfering with SELECT statements.
  18.     SET NOCOUNT ON;
  19.     update item set [info] = '0' where [info] = '8';
  20.     -- Insert statements for trigger here
  21.  
  22. END
  23. GO
  24.  
Sep 19 '06 #2
Shaily
3
Hi there.

I'm trying to create a Trigger for on insert, which will be triggered upon any attempted insert into a database table item. Basically want it to say, if the attempted insert value for column A is XXX, then change it to YYY, and proceed with the insert.

Not sure how to do this though. Only noob @ triggers. I managed to do a trigger which AFTER an insert, checks column [info] in item table for value 8 & then sets it to 0.

can anyone tell me how to modify my existing trigger to achieve this?

Expand|Select|Wrap|Line Numbers
  1. ================================================
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. -- =============================================
  7. -- Author:        Name
  8. -- Create date: 
  9. -- Description:    
  10. -- =============================================
  11. CREATE TRIGGER dbo.gmItem 
  12.    ON  dbo.item 
  13.    AFTER INSERT
  14. AS 
  15. BEGIN
  16.     -- SET NOCOUNT ON added to prevent extra result sets from
  17.     -- interfering with SELECT statements.
  18.     SET NOCOUNT ON;
  19.     update item set [info] = '0' where [info] = '8';
  20.     -- Insert statements for trigger here
  21.  
  22. END
  23. GO
  24.  

What i understand is that u want to insert a changed valur for a perticular column.
If that is so, try to use "Insead Of Insert".


declare @info
selsct @info = info from inserted
if @info = 8
Begin
-----rewrite the insert statement with the value of info change
end

else
begin

---- rewrite the insert statement with the original data.

end





Hope this will heip. For further clarification on "Instead Of Insert" search for "Instead Of Insert" in BOL.


Regareds
Shail
Nov 1 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Dave C. | last post by:
Hello, I have created the following trigger: CREATE TRIGGER tr_UDFTest ON UserDefinedFields FOR INSERT, UPDATE AS DECLARE @foobar varchar(100) SELECT @foobar= foobar FROM inserted IF (...
2
by: Trevor Fairchild | last post by:
I am trying to create a very minimal auditing system for a series of databases. I am in the process of writing Update triggers for 5 Tablse. I will write a trigger for each table-the trigger's...
2
by: Rigs | last post by:
Hi, I'm a SQL Server newbie, so I'd appreciate if someone would tell me if this is possible. I'm running SQL Server 2000 on Win2k Server I have one table with a large number of columns. I...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
33
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL,...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
2
by: 73blazer | last post by:
Perhaps my thinking is wrong but this is what I have: 1 table (Tab1) with 1 attribute (Attr1) Attr1 char(16) for bit data ----------------------------------------------- create trigger...
0
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as...
2
by: mob1012 via DBMonster.com | last post by:
Hi All, I wrote last week about a trigger problem I was having. I want a trigger to produce a unique id to be used as a primary key for my table. I used the advice I received, but the trigger is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.