CREATE TABLE [dbo].[tmp_rg_xx_LBJ]
(
[ROWVERSION] [bigint] NULL,
[ROWDATE] [datetime] NULL,
[SAPNO] [int] NOT NULL DEFAULT 1)
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF__tmp_rg_xx__SAPNO__1B0907CE]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[LBJ] DROP CONSTRAINT [DF__tmp_rg_xx__SAPNO__1B0907CE]
END
GO
I have a script that creates a table with a default in, but I want to drop the constraint. No problem manually as I know the name, except I want drop the constraint after recreating the table. So I can just re run the drop constraint script. How do you do this without explicitly using the name?
You can try this – this will look up the necessary info in the system catalog views and create a “drop” T-SQL command that you can copy & execute to get rid of your system-named default constraint:
If you need to put this into your own script – you can assign the
DropCmdto a variable and then execute that SQL statement using dynamic SQL: