SQL 7 vs. 2000 issue -trigger and nulls

From: Bill Polewchak (pchak_at_troopone.net)
Date: 12/22/04


Date: Wed, 22 Dec 2004 11:34:54 -0800

We're trying to dump our remaining v7 SQL server and update to 2000.
We're having trouble with a trigger updating some tables.

At the beginning, it has this statement:
COMMIT TRANSACTION --This unlocks the Lot table so I can update UDAs,
etc...
BEGIN TRANSACTION

This is what throws the trigger off. I created a "play" table with an
Update trigger, and when I added this statement at the beginning, my
trigger stopped working (this is all on the new server). I took these
two lines out of Lot's Update trigger (again on the new server), ran an
Update query, and the trigger populated all the fields correctly. Does
anybody know what it means about unlocking the Lot table in order to
update? Do we need this in SQL2000 or can we take it out, or do we need
to replace it by something?

CREATE TRIGGER trLot_U ON dbo.Lot
FOR UPDATE
AS

DECLARE
        @rows int,
        @nRunsToSlit int,
        @SiteID varchar(8),
        @SalesOrder varchar(15),
             @Lot varchar(40)

SELECT @rows = @@rowcount
IF @rows=0
  RETURN

--THE NEXT 2 LINES CAUSE THE PROBLEM!

COMMIT TRANSACTION --This unlocks the Lot table so I can update UDAs,
etc...
BEGIN TRANSACTION

SELECT @nRunsToSlit = COUNT(DISTINCT RunNumber)
FROM SlitRollDetail srd, inserted i
WHERE srd.LotID = i.LotID
  AND srd.Completed = 'N'
  AND srd.Disposition = 'NONE'

UPDATE Lot
  SET Lot.PrimaryUOM='MSI'
  WHERE Lot.LotID IN (SELECT i.LotID FROM inserted i)

/*****************Add Check for site id*************/
Select @SalesOrder = substring(LotID, 1, CHARINDEX('-', LotID)-1),
@SiteID = SiteID
from inserted

IF @SiteID = NULL or @SiteID = ''
select @SiteID = SiteID
from SalesOrder
where SalesOrder = @SalesOrder

UPDATE Lot
SET SiteID = @SiteID
WHERE Lot.LotID IN (SELECT i.LotID FROM inserted i)
/******************end fix***********************/

UPDATE Lot
SET Lot.CommonCarrier=
  CASE
    WHEN Lot.LotID LIKE 'E%' THEN 'EXACT ORDER'
    WHEN Updates.Carrier<>'' THEN Updates.Carrier
    ELSE 'NONE'
  END
FROM (SELECT i.LotID, S.Carrier

      FROM inserted i, SalesOrder S
      WHERE i.LotID LIKE S.SalesOrder+'%' OR i.LotID LIKE 'E%') AS
Updates
WHERE Lot.LotID=Updates.LotID

UPDATE Lot
SET Lot.CommonWidth=Updates.CommonWidth
FROM (SELECT WOI.LotID AS LotID, MAX(WOI.RollWidth) AS CommonWidth
        FROM WorkOrderItem AS WOI,
                (SELECT WOI.LotID, MAX(WOI.RollQuantity) AS MaxQty
                FROM WorkOrderItem AS WOI, inserted
                WHERE WOI.LotID=inserted.LotID
                GROUP BY WOI.LotID) AS MaxRolls
        WHERE MaxRolls.MaxQty = WOI.RollQuantity
                AND MaxRolls.LotID = WOI.LotID
        GROUP BY WOI.LotID) AS Updates
WHERE Lot.LotID=Updates.LotID

UPDATE Lot
SET Lot.EUs = Round(0.0077*Lot.OrderLength*@nRunsToSlit/3.0
            + 9.6300*@nRunsToSlit
            + 0.8100*Lot.NumberUp
            + 5.0000, 1)

FROM inserted
WHERE Lot.LotID=inserted.LotID

SELECT @Lot = (SELECT i.LotID FROM inserted i)

IF @Lot like 'E%%'
BEGIN
        INSERT INTO WorkOrderDetailTemp(LotID) SELECT @Lot
END

Any ideas?

Likewise, we had a problem with this:

In SQL 7.0: substring ("yes", 0, 255) returns NULL, substring("yes", 1,
255") returns "yes".
In SQL 2000: both statements return "yes" (which, to be honest, makes
perfect sense to me).

select..
..
case
        when @nDBRollNumber>=@MaximumRollNumber then 'yes'
        else 'no'
end

This would replace the existing

select..
..
isnull(substring('yes', charindex(convert(varchar,@MaximumRollNumber)
,convert(varchar,@nDBRollNumber)),255),'no')

I wasn't expecting any real problems with SQL 7 code problems in SQL
2000.

thanks!
pchak

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Relevant Pages

  • Re: DTC Fehler
    ... Auf dem Windows Server ist SQL Server 2000 SP3a und ... wird der Barcode im SQL ... Server abgespeichert und der Trigger ausgelöst (After ... Access Express ODBC to a R510 or later OS/400 system. ...
    (microsoft.public.de.sqlserver)
  • on delete set null
    ... daß der SQL ... Server ON DELETE SET bei einem FOREIGN KEY nicht unterstützt. ... Nun versuche ich das ganze mit einem Trigger zu simulieren (auf SProcs ... ID UNIQUEIDENTIFIER PRIMARY KEY); ...
    (microsoft.public.de.sqlserver)
  • =?iso-8859-1?q?Re:_Frage_zu_nem_Insert-Statement_und_Zeichenfolgen_w=FCrden_abgeschnitte
    ... irgendeinen Trigger INSTEAD OF INSERT, UPDATE habe, sollte der nicht ... nenn' das einfach mal Bug im SQL Server...oder doch besser Feature ... Hat mich halt trotzdem etwas gewundert, dass der SQL Server sowas trotz ...
    (microsoft.public.de.sqlserver)
  • Re: maximum characters per column
    ... > How do you set the maximum characters per column in SQL ... In SQL Server ... you could use a trigger, in SQL Server 7/2000 you could use a trigger or ...
    (microsoft.public.sqlserver.server)
  • Re: how to assign the contents of a field to a variable
    ... what happens if the MSMQ server is ... teh requirements of the trigger are to export an XML document to MSMQ ... DECLARE @int_msmqqueue INT ... IF @int_result 0 GOTO ErrorHandler ...
    (microsoft.public.sqlserver.programming)