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.
LVL 1
rafaelrglAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danielamouraCommented:
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


0
rafaelrglAuthor Commented:
WHERE CAN I DO THAT
0
danielamouraCommented:
add the constrain to the database or trap the exception?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

rafaelrglAuthor Commented:
MY FIELDS ON TB_USER IS

ID
USER
NAME

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

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

0
danielamouraCommented:
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
0
markaureliusCommented:
I use a query that does something like this

if GetIntValue('SELECT Count(*) FROM SomeTable WHERE USername = ' + UserName) > 0 then begin
  MessageDlg('Name already in use', mtWarning, [mbOK], 0);
  exit;
end;
...

where GetIntValue creates a  queryobject and runs the query to get the first field:


function TdConnection.GetIntValue(const sql: string): integer;
var query: TDataSet;
begin
  query := CreateQuery(sql);
  try
    result := query.Fields[0].AsInteger;
  finally
    query.Free;
  end;
end;// GetIntValue ---------------------

CreateQuery in for me uses TADOObject, but it would depend on what suite of database objects you are using. I've done the same sort of thing with IBExpress.

I prefer doing this to letting the program raise an exception, because that way I leave break on exception on in case a real problem arises.

HTH,
Mark
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.