?
Solved

How to add a variable to a drop statement

Posted on 2013-06-24
1
Medium Priority
?
463 Views
Last Modified: 2013-06-24
Hi there

I need to get a constraint name, this is what I'm using:

DECLARE @constraint as varchar(100)
set @constraint =
(select dobj.name
from sys.columns col
left outer join sys.objects dobj
on dobj.object_id = col.default_object_id and dobj.type = 'D'
where col.object_id = object_id(N'dbo.SongShare')
and dobj.name is not null)

and then I want to run this

ALTER TABLE MSO_SongShare DROP CONSTRAINT + ' ' + @constraint
alter table MSO_SongShare drop column share
EXEC sp_rename 'MSO_SongShare.share_New', 'share', 'COLUMN'
ALTER TABLE MSO_SongShare ADD  DEFAULT ((0)) FOR share

as you can see on this line

ALTER TABLE MSO_SongShare DROP CONSTRAINT + ' ' + @constraint

it does not work, how do i get the constraint name and drop it?

thanks
0
Comment
Question by:mousemat24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 2000 total points
ID: 39271048
you have  tse dynamic sql

declare @sql varchar(500)
set @sql = 'ALTER TABLE MSO_SongShare DROP CONSTRAINT ' + ' ' + @constraint
exec(@SQL)

Open in new window

0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question