I have to port a PostgreSQL trigger but since I don't have much experience on that I don't know how to change some parts. The original trigger is:
CREATE TRIGGER aerolinea_tr
AFTER UPDATE
ON public.aerolinea FOR EACH ROW
EXECUTE PROCEDURE public."actualizaWarehoseTemplate"();
DECLARE
mviews RECORD;
BEGIN
IF (TG_OP = 'UPDATE') THEN
FOR mviews IN SELECT DISTINCT template.idtemplate
FROM template
INNER JOIN vueloaerolinea ON (template.idvueloaerolinea = vueloaerolinea.idvueloaerolinea)
WHERE vueloaerolinea.codigolinea = old.codigolinea
LOOP
UPDATE detalletemplate SET idwarehose = new.idwarehouse WHERE detalletemplate.idtemplate = mviews.idtemplate;
END LOOP;
END IF;
RETURN old;
END;
And what I have done is:
CREATE TRIGGER aerolinea_tr
ON aerolinea
AFTER UPDATE
AS
BEGIN
-- if no row affected, the trigger ends.
IF @@ROWCOUNT = 0
BEGIN
RETURN;
END;
IF EXISTS(SELECT * FROM inserted)
BEGIN
IF EXISTS(SELECT * FROM deleted)
BEGIN
--Update code goes here.
END
END
END;
But I can't find an equivalent to RECORD for SQL Server and I don't know how to do that part.
Looks like you need to use a cursor . You can try something like this -- it's not complete because I don't know the table schema of the aerolinea table, but nonetheless, should get you going in the right direction:
DECLARE @idtemplate INT //Assuming this is an int
DECLARE @mviews CURSOR
DECLARE @newidwarehouse INT
SELECT @newidwarehouse = idwarehouse FROM INSERTED
SET @mviews = CURSOR FOR
SELECT DISTINCT template.idtemplate
FROM template
INNER JOIN vueloaerolinea ON (template.idvueloaerolinea = vueloaerolinea.idvueloaerolinea)
INNER JOIN DELETED ON DELETED.codigolinea = vueloaerolinea.codigolinea
OPEN @mviews
FETCH NEXT
FROM @mviews INTO @idtemplate
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE detalletemplate
SET idwarehose = @newidwarehouse
WHERE detalletemplate.idtemplate = @idtemplate
FETCH NEXT
FROM @mviews INTO @idtemplate
END
CLOSE @mviews
DEALLOCATE @mviews
BTW, you don't need to check for Inserted and Deleted when checking for Updates. You can just check for Deleted:
IF EXISTS (SELECT * FROM DELETED)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.