alter table chat add " & Text1.Text & " int not null default 0
意思是添加一个字段,并设置为非空,默认值为0
但是我需要设置有效性规则,比如大于0,小于100
alter table chat add " & Text1.Text & " int not null check (>=0 and <=100) default 0  不对,该怎么改?

解决方案 »

  1.   

    示例
    alter table TBName add ColumnName int not null check (ColumnName >=0 and ColumnName<=100) default 0
      

  2.   

    alter table TBName add ColumnName int not null check (ColumnName >=0 and ColumnName<=100) default 0
    不行
      

  3.   

    alter table TBName add ColumnName int not null check (ColumnName >=0 and ColumnName<=100) default 0
    不行
      

  4.   

    create table tmp1 (myid int)
    alter table tmp1 add myid2 int not null check (myid2>=1 and myid2<=100) default 0
      

  5.   

    create table tmp1 (myid int)
    alter table tmp1 add myid2 int not null check (myid2>=1 and myid2<=100) default 0
    这个是可以的
      

  6.   

    TO :vbman2003 
    报:字段定义语法错误。TO:clear_zero 
    alter table tmp1 add myid2 int not null  default 0  这句可以,但alter table tmp1 add myid2 int not null check (myid2>=1 and myid2 <=100) default 0 
    不行,字段定义语法错误你试过没有?
      

  7.   

    给你的示例语句测试通过如果是报语法错误。如果你语句组合变量时有问题,你可以输出语句看下
    dim s as string
    s="alter table chat add " & Text1.Text & " int not null check (" & Text1.Text & " >=0 and " & Text1.Text & " <=100) default 0" 
    '输出检查:
    debug.print s
      

  8.   

    试过了,SQL server里面可以用。但是access里面用不了不是变量的问题,alter table tmp1 add myid2 int not null check (myid2>=1 and myid2 <=100) default 0也不行,但alter table tmp1 add myid2 int not null  default 0  这句却可以。。疯了
      

  9.   

    access的话,尝试用ADO连接,然后执行:
    alter table TB add Col int not null constraint Col_Name check (Col >=0 and Col<=100) default 0