create an sp that receives the necessary parameters to enter a new element in "x" table, but that the name of the new element does not duplicate

0

my question is how to compare in the if, the variable that I created called @name with the attribute of the table called Name?

    
asked by m3nt0r1337 19.07.2016 в 19:52
source

2 answers

0

It's really quite simple:

IF EXISTS(SELECT 1 FROM Sales.Store WHERE Name = @name)
BEGIN
    SELECT '[nombre de tienda existente]';
END
ELSE
BEGIN
    INSERT INTO Sales.Store
    VALUES(@entityID, @name, @salespersonID, @demographics, @rowguid, @modifieddate);
END

Next time, please post the code inside the question, instead of an image.

    
answered by 19.07.2016 / 20:02
source
0

Two other alternatives to be taken into account to take care of the consistency of the data, especially if this sp is not the only point of data entry to it:

1) Create a unique constraint on the Name field of the Sales.Store table

ALTER TABLE Sales.Store  ADD CONSTRAINT UK_Name UNIQUE (Name)   

And then handle the error with a Try Catch Block.

2) Create a trigger on the table to control that the Name value does not repeat

    
answered by 19.07.2016 в 21:12