SQL Query check if unique constraint is Updated , If its not Altered then run Alter Query

Indudhar Gowda 426 Reputation points
2022-06-02T19:25:47.037+00:00

I need a IF Statement to check and run SQL Query

  alter table [ReportableResults] add constraint   
   Unique_ReportableResults_ReportableResultCode_IsCalculated unique (ReportableResultCode, IsCalculated);  

Above Code is Working Fine.....But I dont want to run Everytime...

I want to check if its already altered then not need to run the Above Query.

208012-image.png

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,702 questions
Developer technologies Transact-SQL
SQL Server Other
{count} votes

Accepted answer
  1. Tom Cooper 8,481 Reputation points
    2022-06-03T03:51:17.907+00:00

    The following code will create the constraint if there is no object named Unique_ReportableResults_ReportableResultCode_IsCalculated, but if it already exists, the code will do nothing.

    If Not Exists(Select * From sys.objects Where Name = 'Unique_ReportableResults_ReportableResultCode_IsCalculated')
      alter table [ReportableResults] add constraint
       Unique_ReportableResults_ReportableResultCode_IsCalculated unique (ReportableResultCode, IsCalculated);
    

    Tom

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.