lundi 29 juillet 2019

i try to write a trigger that check the value if exists in table returns me a default value or a random value

i try to write a trigger after insert ,checks my values if exists return me a default value or random one else insert users value in table.

alter trigger [dbo].[changevalues] 
on [dbo].[products]
after insert
as
begin
    declare @productname nvarchar(20),@prodcutprice money
    select productname=@productname ,price=@prodcutprice
    from inserted
    if exists ( select * from products 
                where productname like @productname and price=@prodcutprice)
    begin 
        insert products
        values('fast',100)
    end
    else
    begin
    insert products
    values(@productname,@prodcutprice)
    end
end

i expect that the code returns me ('fast',100) but it return null in table

Aucun commentaire:

Enregistrer un commentaire