Why do they make it so difficult?

From: Wayne Wengert (wayneDONTWANTSPAM_at_wengert.com)
Date: 12/23/04


Date: Thu, 23 Dec 2004 13:27:43 -0700

Kalen;

I finally figured out the syntax to drop the constraint but it still won't
let me add a default value as part of an ALTER COLUMN? I notice all the
examples are ADD, not ALTER (real helpful!)

I tried the following:

ALTER TABLE Units
ALTER COLUMN UnitInfo bit NOT NULL
CONSTRAINT AddUnitInfoDflt <= I also tried just the column name here -
same result
DEFAULT 0 WITH VALUES

But it gives me the error:

Server: Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'CONSTRAINT'.

Wayne

"Kalen Delaney" <replies@public_newsgroups.com> wrote in message
news:upGy%233R6EHA.3616@TK2MSFTNGP11.phx.gbl...
> No, you'll have to drop the existing default first with ALTER TABLE DROP
> CONSTRAINT.
>
> --
> HTH
> ----------------
> Kalen Delaney
> SQL Server MVP
> www.SolidQualityLearning.com
>
>
> "Wayne Wengert" <wayneDONTWANTSPAM@wengert.com> wrote in message
> news:eHJRAgR6EHA.824@TK2MSFTNGP11.phx.gbl...
> > Kalen;
> >
> > That was my first attempt but it gives the following error:
> >
> > Server: Msg 5074, Level 16, State 1, Line 1
> > The object 'AddUnitInfoDflt' is dependent on column 'UnitInfo'.
> > Server: Msg 4922, Level 16, State 1, Line 1
> > ALTER TABLE ALTER COLUMN UnitInfo failed because one or more objects
> > access
> > this column.
> > The object 'AddUnitInfoDflt' is dependent on column 'UnitInfo'.
> >
> > so I assumed that I needed to include the default?
> >
> > BTW - all rows are 0 or 1
> >
> > Wayne
> >
> > "Kalen Delaney" <replies@public_newsgroups.com> wrote in message
> > news:uuCPHVR6EHA.2540@TK2MSFTNGP09.phx.gbl...
> >> Wayne
> >>
> >> The syntax is a bit confusing because there are so many possibilities,
> >> but
> >> there is no possibility that actually changes the datatype and includes
a
> >> constraint. You will have to do this in multiple steps.
> >>
> >> You could change the datatype, and then run ALTER again to add the
> >> constraint. If you get an error changing the type, you might need to
> >> first
> >> update the table to make sure all the rows have 1 or 0 in that column.
> >>
> >> --
> >> HTH
> >> ----------------
> >> Kalen Delaney
> >> SQL Server MVP
> >> www.SolidQualityLearning.com
> >>
> >>
> >> "Wayne Wengert" <wayneDONTWANTSPAM@wengert.com> wrote in message
> >> news:%23aQNU8Q6EHA.3828@TK2MSFTNGP09.phx.gbl...
> >> > Thanks for the clarification but that doesn't work either? That
article
> >> > defines the syntax to be used (very confusing definitions) but the
> >> > examples
> >> > show only ADD or DROP columns. I want to change an existing column to
> > type
> >> > "bit" (it is currently int). I tried:
> >> >
> >> > ALTER TABLE Units
> >> > ALTER COLUMN UnitInfo bit NOT NULL
> >> > DEFAULT 0 WITH VALUES
> >> >
> >> > but that gives a syntax error "near DEFAULT"
> >> >
> >> > then I tried:
> >> >
> >> > ALTER TABLE Units
> >> > ALTER COLUMN UnitInfo bit NOT NULL
> >> > Constraint UnitInfoDflt
> >> > DEFAULT 0 WITH VALUES
> >> >
> >> > (I got the constraint name from - exec sp_helpconstraint 'units') but
> > that
> >> > gives a syntax error "near Constraint"
> >> >
> >> > Are there any examples of simply altering a column with a default
> > setting?
> >> >
> >> > Wayne
> >> >
> >> >
> >> > "Wayne Wengert" <wayneDONTWANTSPAM@wengert.com> wrote in message
> >> > news:eNLXwEQ6EHA.1188@tk2msftngp13.phx.gbl...
> >> >> Kalen;
> >> >>
> >> >> I am using the following QA statement:
> >> >>
> >> >> ALTER TABLE Units
> >> >> ALTER COLUMN UnitInfo bit NOT NULL
> >> >> SET DEFAULT 0
> >> >>
> >> >> But I get a syntax error "near the word DEFAULT"
> >> >>
> >> >> I got this syntax from the MSDN site where the example is:
> >> >>
> >> >> ALTER TABLE TableName1 ALTER [COLUMN] FieldName2 [NULL | NOT NULL]
> >> >> [SET
> >> >> DEFAULT eExpression2]
> >> >> [SET CHECK lExpression2 [ERROR cMessageText2]] [ DROP DEFAULT ] [
> > DROP
> >> >> CHECK ] [ NOVALIDATE ]Why does my code fail?Wayne"Kalen Delaney"
> >> >> <replies@public_newsgroups.com> wrote in message
> >> >> news:ed%23xxVI6EHA.1524@TK2MSFTNGP09.phx.gbl...
> >> >> > Hi Wayne
> >> >> >
> >> >> > I presume you're using Enterprise Manager. Try using Query
Analyzer
> > and
> >> >> > actually make the change using the ALTER TABLE command. Any error
> >> >> > you
> >> > get
> >> >> > should be a lot more meaningful.
> >> >> >
> >> >> > To truly verify you have no triggers on a table, run
> >> >> >
> >> >> > exec sp_helptrigger 'tablename'
> >> >> >
> >> >> > --
> >> >> > HTH
> >> >> > ----------------
> >> >> > Kalen Delaney
> >> >> > SQL Server MVP
> >> >> > www.SolidQualityLearning.com
> >> >> >
> >> >> >
> >> >> > "Wayne Wengert" <wayneDONTWANTSPAM@wengert.com> wrote in message
> >> >> > news:esKGlFI6EHA.3828@TK2MSFTNGP09.phx.gbl...
> >> >> > >I am using SQL Server 2000. I try to modify a filed in a table
> >> > definition
> >> >> > > (from int to bit) and when I save it I get a popup saying that
the
> >> >> update
> >> >> > > trigger could not be read and the process aborts. There are no
> >> > triggers
> >> >> on
> >> >> > > the table. If I right click on the table and open the triggers
it
> >> > offers
> >> >> a
> >> >> > > new trigger - there are none currently.
> >> >> > >
> >> >> > > How do I resolve this?
> >> >> > >
> >> >> > > Wayne
> >> >> > >
> >> >> > >
> >> >> >
> >> >> >
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>
> >
> >
>
>



Relevant Pages

  • How to change column data type from Text to nvarchar()
    ... I need help with changing a column data type from Text to a nvarchar. ... I tried the syntax below in QA and I got an error: ... ALTER COLUMN column_name nvarchar ... I used "sp_helpconstraint table_name" to get the CONSTRAINT names. ...
    (microsoft.public.sqlserver.programming)
  • Re: Restoring database with different COLLATION
    ... > How I can modify the COLLATION of the database after restoring it? ... as SQL Server 2000 supports different collations and sort orders for each ... ALTER DATABASE db_name ... ALTER COLUMN VARCHAR ...
    (microsoft.public.sqlserver.msde)
  • Re: Check constraint
    ... The syntax I showed you is to create the constraint at the time you create ... However, if the table already exists, you can do an ALTER TABLE: ... Columnist, SQL Server Professional ... Now that I have the syntax. ...
    (microsoft.public.sqlserver.server)
  • Re: Change Data Type with VB code
    ... However, it's unlikely you'd need to alter the SYSTEM.MDW, unless you've ... Doug Steele, Microsoft Access MVP ... using ALTER COLUMN did not change the data type. ...
    (microsoft.public.access.tablesdbdesign)
  • Alter Column - strange behaviour msg 5074
    ... ALTER TABLE X ALTER COLUMN V VarChar ... create table testdata ( ... create index ix_testdata_aName on testdata on ...
    (microsoft.public.sqlserver.server)