1

SQL Server 2000

Say if I have a table like

CREATE TABLE [Message] (
    [MessageIdx] [int] IDENTITY (1, 1) NOT NULL ,
    [Message] [varchar] (1024) COLLATE Latin1_General_CI_AS NOT NULL ,
    [column1] ... ,
    [column2] ... ,
    ... ,
    [ValidUntil] [datetime] NULL ,
    CONSTRAINT [PK_Message] PRIMARY KEY  CLUSTERED 
    (
        [MessageIdx]
    ) WITH  FILLFACTOR = 90  ON [PRIMARY] 
) ON [PRIMARY]
GO

Since there're too many columns, so I am trying to insert value without specify column names explicitly. I want to insert a new row with all columns except 'MessageIdx' and 'ValidUntil' not specified. Therefore, I definitely don't want to type all column names. I tried below statement but it causes error. How can I do that? Thanks.

insert into message values (DEFAULT,'blah',something, ..., DEFAULT);

EDIT: AFAIN, SQL 2005 server you can skip the identity column when inserting. So that will be

insert into message values ('blah',something, ..., DEFAULT);

But is there any work around for SQL server 2000?

3 Answers 3

1

you have to specify column names if you use set identity_insert

but you can do this

set identity_insert caconfig..fxmessage on;
insert into message (MessageIdx,[Message],[ValidUntil) 
values (1,'blah',GETDATE());
set identity_insert caconfig..fxmessage off;

I assume what you really want is this, it will generate the identity value for you

 insert into message ([Message],[ValidUntil) values ('blah',GETDATE());
Sign up to request clarification or add additional context in comments.

Comments

1

Don't be lazy. Do it the correct way, which is to specify the column list (excluding the identity column).

Comments

0

Use getdate(), and for IDENTITY columns (if using uniqueidentifier which I see you're not), you can use newid(), and set them in the design-view of the table in the default value. After that you'd simply go:

INSERT INTO Message (Message) VALUES ('blah');

1 Comment

@SQL - I know, it's in there. Saw that shortly after I answered and clarified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.