Link to home
Create AccountLog in
Avatar of rafaelrgl
rafaelrgl

asked on

SEE IF IS THERE SAME FIELD IN SQL DATABASE

I´M TRYING TO INSERT ROW IN MY DATABASE, FIELDS LIKE NAME AND USER CAN NOT EXIST SAME VALUES FOR THIS FIELD. IF EXIST, MY APPLICATION HAVE TO SHOW MESSAGE TO USER LIKE: "YOU HAVE TO CHANGE YOU USER NAME".

HOW CAN I DO THAT.
Avatar of danielamoura
danielamoura

The best way to always guarantee you won't have duplications is by adding an unique constrain to your database on the respective fields. If you are using for example MS SQL Server, this would be the syntax:

alter TABLE test ADD CONSTRAINT uqName UNIQUE (Name)
alter TABLE test ADD CONSTRAINT uqUser UNIQUE (User)

Than, just trap the exception


Avatar of rafaelrgl

ASKER

WHERE CAN I DO THAT
add the constrain to the database or trap the exception?
MY FIELDS ON TB_USER IS

ID
USER
NAME

HOW IS GOING TO BE MY CONTRAINT EXPRESSION FOR DO THAT.
YOUR NAME CAME FROM BRASIL.
THANKS

I DID, BUT RIGHT NOW, I DON´T KNOW HOW CAN I TRAP THE EXCEPTION.
CAN YOU SHOW ME.

Hi Rafael,
  I am brazilian. And I'm assuming you are too, but I will not switch to english, in case anyone else has the same problem as you

  try
      InsertInTable;
  except on E:EDataBaseError do
     
  end;

where InsertInTable is your method to insert in the table.

Daniela
ASKER CERTIFIED SOLUTION
Avatar of markaurelius
markaurelius
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer