Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8285527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:29:28+00:00 2026-06-08T11:29:28+00:00

I am trying to write a procedure to drop the Unique constraints from any

  • 0

I am trying to write a procedure to drop the Unique constraints from any table quicker.

IF EXISTS
        (SELECT * 
        FROM dbo.sysobjects 
        WHERE id = object_id(N'[dba].[spu_drop_uq_index]'))
    DROP PROCEDURE [dba].[spu_drop_uq_index]
GO

CREATE PROCEDURE [dba].[spu_drop_uq_index] (@table varchar(1000), @index varchar(1000))
AS
BEGIN
    DECLARE @sql varchar(1000)
    SET @sql = 'ALTER TABLE ['+@table+'] DROP CONSTRAINT ['+@index+']'
    IF EXISTS (SELECT name FROM sysindexes WHERE name = @index)
        EXEC @sql
END    
GO

EXEC [dba].[spu_drop_uq_index] @table = 'aaa', @index = 'UQ_xxx'
GO

But I get an error:

The name 'ALTER TABLE [aaa] DROP CONSTRAINT [UQ_xxx]' is not a valid identifier.

However, if I execute this not dynamically, it succeeds:

ALTER TABLE [aaa] DROP CONSTRAINT [UQ_xxx]

What am I doing wrong? 🙂 Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:29:29+00:00Added an answer on June 8, 2026 at 11:29 am

    Use

     exec sp_executesql @sql
    

    instead of EXEC, or put the @sql in parenthesis

     Exec (@sql)
    

    sp_executesql is preferred: http://msdn.microsoft.com/en-us/library/ms175170(v=sql.105).aspx

    To execute a string, we recommend that you use the sp_executesql stored procedure instead of the EXECUTE statement. Because this stored procedure supports parameter substitution, sp_executesql is more versatile than EXECUTE; and because sp_executesql generates execution plans that are more likely to be reused by SQL Server, sp_executesql is more efficient than EXECUTE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a stored procedure which selects columns from a table
Problem I am trying to write a stored procedure to select a list of
I'm trying to write a stored procedure to copy a subset of data from
I am trying to write a procedure that will fire the same select query
I am trying to write a stored procedure that takes a table name as
I'm trying to write a stored procedure to select employees who have birthdays that
i am trying to write a procedure that inserts rows into a temp table.
I am trying to write a simple stored procedure that does a select statement
I'm trying to write an procedure that does something after 2 objects are returned
I'm trying to write a procedure for a baseball program that will use multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.