473,395 Members | 1,681 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.

Sql Trigger Problem

I'm trying to write a trigger for a database that will capture the previous value of a field and, on insert of data from the app, increment that field by 1.

This field is similar to a check number, and the first record in the database will not be 1, it will be something like 386651. I'd like to be able to grab that as my initial starting number and increment each record thereafter by 1.

I do have a record identifier set up that is separate from this value.

There will be a couple hundred records imported into this table before it goes live for data entry. It is at this point that the trigger will need to be in place to continue sequentially assigning a "document number" to these records.

So far, everything I've tried either errors out or starts at 1 regardless of whether or not I have select max() in the trigger.

Obviously I am just getting my feet wet with SQL triggers, but have had success creating them in the past. However, this has me stumped.
Aug 3 '07 #1
8 3449
ilearneditonline
130 Expert 100+
I'm trying to write a trigger for a database that will capture the previous value of a field and, on insert of data from the app, increment that field by 1.

This field is similar to a check number, and the first record in the database will not be 1, it will be something like 386651. I'd like to be able to grab that as my initial starting number and increment each record thereafter by 1.

I do have a record identifier set up that is separate from this value.

There will be a couple hundred records imported into this table before it goes live for data entry. It is at this point that the trigger will need to be in place to continue sequentially assigning a "document number" to these records.

So far, everything I've tried either errors out or starts at 1 regardless of whether or not I have select max() in the trigger.

Obviously I am just getting my feet wet with SQL triggers, but have had success creating them in the past. However, this has me stumped.
I would be most helpful if you provided your table schema and what you have tried. Is it not possible to make the field an IDENTITY field and have it increment by 1?
Aug 4 '07 #2
I'm trying to write a trigger for a database that will capture the previous value of a field and, on insert of data from the app, increment that field by 1.

This field is similar to a check number, and the first record in the database will not be 1, it will be something like 386651. I'd like to be able to grab that as my initial starting number and increment each record thereafter by 1.

I do have a record identifier set up that is separate from this value.

There will be a couple hundred records imported into this table before it goes live for data entry. It is at this point that the trigger will need to be in place to continue sequentially assigning a "document number" to these records.

So far, everything I've tried either errors out or starts at 1 regardless of whether or not I have select max() in the trigger.

Obviously I am just getting my feet wet with SQL triggers, but have had success creating them in the past. However, this has me stumped.

I think that there is no need to create a trigger for achieving that functionality,
u are provided with an identity propertey in sql server,

CREATE TABLE exampleidentity_starts_from_386651_increments_by_1
(
id_num int IDENTITY(386651,1),
fname varchar (20),
minit char(1),
lname varchar(30)
)
Aug 4 '07 #3
ck9663
2,878 Expert 2GB
I think that there is no need to create a trigger for achieving that functionality,
u are provided with an identity propertey in sql server,

CREATE TABLE exampleidentity_starts_from_386651_increments_by_1
(
id_num int IDENTITY(386651,1),
fname varchar (20),
minit char(1),
lname varchar(30)
)

he's right. however, identity column ensures uniqueness but does not guarantees a fully sequential series. there could be hole on your sequence if the insert fails. also, you can not reuse a value once it's deleted.
Aug 6 '07 #4
The trigger I most recently tried is as follows:

CREATE TRIGGER [LW_NO] ON dbo.AP_LIEN
AFTER INSERT
AS

DECLARE @LW INT
SELECT @LW =MAX(LW_NUMBER)
FROM AP_LIEN
UPDATE AP_LIEN
SET LW_NUMBER = @LW + 1

The only problem is that this trigger updates the lw_number on all records, not just the one most recently inserted.

I don't think the create_table trigger would work because there are pre-existing records that will be imported into this table with lw_numbers already assigned.

This is why I need to be able to grab the previous lw_number, increment by 1, and assign this number to the record being inserted.

The table columns are as follows:

record_id uniqueidentifier
lw_number
sub_number
vendor_code
check_number
payment_amount
lw_date
lw_return_date

Of these data fields, all information is grabbed from our accounting SQL database except for the lw_number. The lw_number is currently assigned manually via an excel spreadsheet, which is VERY time consuming for our AP department. We may issue 175 - 200 lw's (lien waivers) with each check run.

Basically, all this table is doing is giving them a more efficient way to track lien waivers. To do this, I will be importing all lien waivers that are currently outstanding into the table, then all new lien waivers will be inserted after each check run.
Aug 6 '07 #5
try this one.

CREATE TRIGGER [LW_NO] ON dbo.AP_LIEN
AFTER INSERT
AS

DECLARE @LW INT
SELECT @LW =MAX(LW_NUMBER)
FROM AP_LIEN

UPDATE AP_LIEN
SET inserted.LW_NUMBER = @LW + 1

this way you only update the inserted record.
Aug 6 '07 #6
try this one.

CREATE TRIGGER [LW_NO] ON dbo.AP_LIEN
AFTER INSERT
AS

DECLARE @LW INT
SELECT @LW =MAX(LW_NUMBER)
FROM AP_LIEN

UPDATE AP_LIEN
SET inserted.LW_NUMBER = @LW + 1

this way you only update the inserted record.
Gave it a try but got the following error:
Error 1032: Cannot usethe column prefix "INSERTED". This must match the object in the update clause.
Aug 6 '07 #7
I found a workable solution.

After inserting all existing records into the table I changed the LW_NUMBER column to an identity column.

This allows future values in the LW_NUMBER column to be automatically assigned using the previous maximum value as the new starting value.

There could be gaps in the sequence if an insert fails, but for our purposes this is a non-issue.
Aug 7 '07 #8
A very simple example of how to write an sql trigger to increment a column on every insert:

http://www.chapterzero.co.uk/article...-triggers.aspx




You can also download the sql file from this page. I always find it easier if i have an sql file to work on rather than reading the code from the webpages.

Cheers!
Sep 17 '07 #9

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

Similar topics

2
by: Galina | last post by:
Hello I work with Oracle 9 database. I want to create a trigger using 2 tables: KEY_SKILLS_STUDENT and KEY_SKILLS. There are fields in KEY_SKILLS_STUDENT: KEY_SKILLS_ID, PORTFOLIO_RESULT and...
9
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
13
by: Tolik Gusin | last post by:
Hello All, DB2 UDB 8.1 FP3 for Linux The table has 4 triggers on the Insert operation. Three triggers small (200 bytes) and one trigger large (3 ËÂ). In the large trigger there is one long...
0
by: Dave Sisk | last post by:
I've created a system or external trigger on an AS/400 file a.k.a DB2 table. (Note this is an external trigger defined with the ADDPFTRG CL command, not a SQL trigger defined with the CREATE...
5
by: William of Ockham | last post by:
Hi, I was asked to recreate a new clean database for our developers because the current one they use is not entirely up to date. So I created a new database and I run into the followin strange...
2
by: gustavo_randich | last post by:
Hi :-) I'm porting a project from Oracle to DB2 and now I'm trying to avoid error SQL0746N in a trigger which reads the same table in which the trigger is defined. Below is Oracle's...
12
by: Bob Stearns | last post by:
I am trying to create a duplicate prevention trigger: CREATE TRIGGER is3.ard_u_unique BEFORE UPDATE OF act_recov_date ON is3.flushes REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL WHEN...
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...
2
by: dean.cochrane | last post by:
I have inherited a large application. I have a table which contains a hierarchy, like this CREATE TABLE sample_table( sample_id int NOT NULL parent_sample_id int NOT NULL ....lots of other...
1
by: veasnamuch | last post by:
I have a problem while I create a trigger to my table. My objective is getting any change made to my table and record it in to another table . My have thousands records before I add new trigger to...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.