Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

i don't work with access very much so i'm having a little trouble with this one.

 

how do i set the default value with alter statements. i have this which gets me half the way there

 

ALTER TABLE TESTTABLE ADD COLUMN MYFIELD INTEGER  NOT NULL

 

i have tried putting Default 0, Default(0) , Default=0, and numerous other combinations at the end of the above statement but know default value of zero is set.

  • 4 weeks later...
Posted

it does work but i would like to set the default which is not happening.

 

i've tried several combinations of syntax to set it but no success. i really think it's one of those thing it should do but doesn't.

Posted

Possible Access quirk

 

The correct syntax would be:

 

ALTER TABLE TESTTABLE ADD COLUMN MYFIELD INTEGER NOT NULL DEFAULT 0;

 

If, as you say, this causes the column to be added but without the default value set, then Access is not behaving as it should.

 

However, I'd be interested to see if this affects setting any default value - does it work if you use another number (non-zero) as the default? Also, does it work if you add the column and then set the defaut value in a seperate SQL statement?

Never trouble another for what you can do for yourself.
  • *Experts*
Posted

Because I, personally, don't like adding defaults to a column when I don't really want one, here's an approach to add the column, set the default, and change the column:

ALTER TABLE TESTTABLE ADD MYFIELD INTEGER  NULL
GO
UPDATE TESTTABLE SET MYFIELD = 0
GO
ALTER TABLE TESTTABLE ALTER COLUMN MYFIELD INTEGER  NOT NULL

 

In my test against SQL Server (not Access), the first ALTER should NOT have the word "COLUMN" - just "...ADD MYFIELD...". The second alter needs the words "...ALTER COLUMN...". Mabye Access syntax is different?

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...