For our accounting software system, it's not a good idea to manipulate it by adding triggers that are not a part of the proprietary programming.
Main Topics
Browse All TopicsHello:
In our accounting system, there is a field in a "setup" window that contains the "next" document number. This field automatically generates a new document number in a separate transaction entry window, when a user enters a new transaction. Let's say that the next number in this field in the setup window is UNIVTR000000001. This will be the number in the transaction entry window, when a user enters a new transaction. Now, the "next" number in this setup window will be UNIVTR000000002. And, of course, this number will be the number that pours into the transaction entry window the next time a user enters a transaction.
It's a long story but, due to a bug in the software, the next number is being updated in this setup window when a user overrides that number in the transaction entry window. If the user enters the number "CSITR0000000003" in the transaction entry window, the next document number in the setup window will be "CSITR0000000004". We need for these numbers to stay with the "UNIVTR00000000" theme and not contain the CSITR prefix.
The only way that I know to combat this bug is to create a SQL job that updates this next document number field in the setup window to contain "UNIVTR00000000" plus one more than whatever transaction contains the phrase "UNIVTR00000000".
New transactions are in the SVC00700 table and "historical/posted" transactions are in the SVC30700 table. Also, the setup window is the IV40100 table and this next document number is the NXTSPNUM.
So, I need for the query to say something along the lines of "update IV40100 set NXTSPNUM = "one higher than the highest UNIVTR number whether that number is in the SVC00700 table or in the SVC30700 table"".
How can I do this?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
That's right. And it's equally bad to manipulate data within a proprietary system with rogue programs or procedures. After all, a trigger is just a special kind of procedure that runs automatically.
But then again, I'm guessing you've already dismissed the preferred alternative which is to work with your vendor to have your requirements implemented within the proprietary product. I know there are lots of excuses: you need the fix now, not later; they charge too much for customizations; the product works as designed, etc, etc, etc.
The reality is with a vendor package you sometimes get caught between a rock and a hard spot; you cannot both follow best practices and meet your needs. So, what to do?
Whatever you decide, it should be treated as a "managed" change. By that I mean, somebody needs to keep very good track of it and reinstall/test the code every time the product is reinstalled/upgraded.
Before rejecting the idea of a trigger outright, look at objectively at the pros/cons between doing that way compared to an SQL job.
Let's see...the trigger runs instantly and automatically. The SQL job must be scheduled. How often? Every time a new document number is assigned? Every time somebody notices the next number seed has been corrupted? Once an hour?
Why not just train everybody to override the prefix back to UNIVTR when it goes bad?
)
Here's some (untested) for a procedure
Update IV40100
Set NXTSPNUM='UNIVTR' + right(replicate('0',8) + isnull(cast(
(Select Max(Cast(DOCNUMBER as INT)) FROM
(Select RIGHT(DOCNUMBER,9) From SVC00700
where DOCNUMBER LIKE 'UNIVTR%'
and isnumeric(RIGHT(DOCNUMBER,
union Select RIGHT(DOCNUMER,9) from SVC30700
where DOCNUMBER LIKE 'UNIVTR%'
and isnumeric(RIGHT(DOCNUMBER,
)) as VARCHAR(9) ), 1)
Here's some code (untested) for a trigger:
CREATE TRIGGER PreservePrefix
ON IV40100
INSTEAD OF UPDATE
BEGIN
Update IV40100
Set NXTSPNUM=ISNULL(
(Select Max(NXTSPNUM) FROM Inserted where LEFT(NXTSPNUM,6) = 'UNIVTR')
,NXTSPNUM)
END
Those are some very good points, dqmq! Unfortunately, the vendor is not willing to fix. And, I hate to ask the client to have to monitor a field that should not require monitoring. That's why I was hoping to just find a T-SQL script that says "update IV40100 set NXTSPNUM = "one higher than the highest UNIVTR number whether that number is in the SVC00700 table or in the SVC30700 table"".
Is there anyway that you can help me compose such a script? I'm confident that I can figure out the best way to use it, from there. I jst need help with the syntax.
Hi dqmq:
I ran the code, but I got two error messages as follows:
Msg 4145, Level 15, State 1, Line 7
An expression of non-boolean type specified in a context where a condition is expected, near 'union'.
Msg 4145, Level 15, State 1, Line 10
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
The only differences between the code that I used and what you sent is that I changed the "DOCNUMBER" field to the correct name of "ORDDOCID", and I changed 9 to 8 since the number of characters after "UNIVTR" is 8.
Below is the code. By the way, what does the clause "replicate('0',8)" do?
Update IV40100
Set NXTSPNUM='UNIVTR'+ right(replicate('0',8) + isnull(cast(
(Select Max(Cast(ORDDOCID as INT)) FROM
(Select RIGHT(ORDDOCID, 8) From SVC00700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
union Select RIGHT(ORDDOCID, 8) from SVC30700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
)) as VARCHAR(8) ), 1)
Like I said, it was untested. I see no issues with the changes you made, so let's first see if this runs:
Select RIGHT(ORDDOCID, 8) as ORDDOCID From SVC00700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
union Select RIGHT(ORDDOCID, 8) from SVC30700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
Then, see if this runs:
Select
Max(Cast(ORDDOCID as INT)) FROM
(
Select RIGHT(ORDDOCID, 8) as ORDDOCID From SVC00700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
union Select RIGHT(ORDDOCID, 8) from SVC30700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
) as tmp
Then this:
Select
ISNULL
(
Max(Cast(ORDDOCID as INT)) FROM
(
Select RIGHT(ORDDOCID, 8) as ORDDOCID From SVC00700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
union Select RIGHT(ORDDOCID, 8) from SVC30700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
) as tmp
,1)
Then this:
Select
CAST
(
ISNULL
(
Max(Cast(ORDDOCID as INT)) FROM
(
Select RIGHT(ORDDOCID, 8) as ORDDOCID From SVC00700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
union Select RIGHT(ORDDOCID, 8) from SVC30700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8))
) as tmp
,1
)
as VARCHAR(8)
)
Hi dqmq:
On the first script, I get the following errors:
Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'union'.
Msg 4145, Level 15, State 1, Line 6
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
On the second script, I get the following errors:
Msg 4145, Level 15, State 1, Line 7
An expression of non-boolean type specified in a context where a condition is expected, near 'union'.
Msg 4145, Level 15, State 1, Line 10
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
On the third script, I get the following errors:
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'FROM'.
Msg 4145, Level 15, State 1, Line 9
An expression of non-boolean type specified in a context where a condition is expected, near 'union'.
Msg 4145, Level 15, State 1, Line 12
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
On the fourth script, I get the following errors:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'FROM'.
Msg 4145, Level 15, State 1, Line 11
An expression of non-boolean type specified in a context where a condition is expected, near 'union'.
Msg 4145, Level 15, State 1, Line 14
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
OK, try this:
Update IV40100
Set NXTSPNUM=(SELECT 'UNIVTR' + right(replicate('0',7),8) + CAST (ORDDOCID AS VARCHAR(8)) FROM
(
SELECT
ISNULL(Max(Cast(ORDDOCID as INT)),1) AS ORDDOCid FROM
(
Select RIGHT(ORDDOCID, 8) as ORDDOCID From SVC00700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8)) = 1
union Select RIGHT(ORDDOCID, 8) from SVC30700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8)) = 1
) as tmp
) as tmp)
Slight adjustment:
Update IV40100
Set NXTSPNUM=(SELECT 'UNIVTR' + right(replicate('0',7),8) + CAST (ORDDOCID AS VARCHAR(8)) FROM
(
SELECT
ISNULL(Max(Cast(ORDDOCID as INT)+1),1) AS ORDDOCid FROM
(
Select RIGHT(ORDDOCID, 8) as ORDDOCID From SVC00700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8)) = 1
union Select RIGHT(ORDDOCID, 8) from SVC30700
where ORDDOCID LIKE 'UNIVTR%'
and isnumeric(RIGHT(ORDDOCID, 8)) = 1
) as tmp
) as tmp)
Business Accounts
Answer for Membership
by: dqmqPosted on 2009-11-03 at 09:30:55ID: 25731335
I vote for fixing the bug--prevent overlaying the NXTSPNUM with a bad value. Also for preventing a user from hijacking a higher number in the UNIVTR series. :>)
But I'm guessing you're somehow prevented from changing that code????? Have you considered implementing a trigger to skip the NXTSPNUM update if it does not follow the desired pattern?