SQLEXEC(nhandle,"select * from ckckzz","tmp1") SELECT tmp1 COPY STRUCTURE EXTENDED TO FileName USE FileName SCAN kk=ALLTRIM(field_name) DO case CASE field_type="C" SQLEXEC(nhandle,"alter table ckckzz add CONSTRAINT &kk default space(2) for &kk") &&字符型默认为二个空格 CASE field_type="N" OR field_type="L" SQLEXEC(nhandle,"alter table ckckzz add CONSTRAINT &kk default 0 for &kk") &&数值型或逻辑型默认为0 ENDCASE ENDSCAN
[解决办法] alter table ckckzz add CONSTRAINT &kk default space(2) for &kk
其实你注意一下语法中这个 &kk 是什么意义就明白了。
ALTER TALBE ...ADD CONSTRAINT constraint_name DEFAULT constant_expression FOR column 红字部分对应的是约束名,而约束名在数据库中是不可以重复的。 你可以在字段名前再加个表名一同构成约束名。 [解决办法]
[解决办法] 请参考:
Set Talk Off Set Safe Off Public nhandle nhandle=Sqlstringconnect("driver=sql server;server=yjr;uid=sa;pwd=12;database=msgl") If nhandle<=0 Messagebox("连接失败") Return Endif SQLExec(nhandle,"select * from ckckzz","tmp1") Select tmp1 Copy Structure Extended To FileName Use FileName *Brow lcTableName='ckckzz1' &&第一个表名,此处改为你的实际表名 Scan kk1=lcTableName+"_"+Alltrim(field_name) kk2=Alltrim(field_name) Do Case Case field_type="C" SQLExec(nhandle,"alter table &lcTableName. drop CONSTRAINT &kk1.") &&删除,防止多次运行 SQLExec(nhandle,"alter table &lcTableName. add CONSTRAINT &kk1. default ' ' for &kk2.") &&字符型默认为二个空格 Case field_type="N" Or field_type="L" SQLExec(nhandle,"alter table &lcTableName. drop CONSTRAINT &kk1.") &&删除,防止多次运行 SQLExec(nhandle,"alter table &lcTableName. add CONSTRAINT &kk1. default 0 for &kk2.") &&数值型或逻辑型默认为0 Endcase Endscan lcTableName='ckckzz2' &&第二个表名,此处改为你的实际表名,与第一个表名字段一样 Scan kk1=lcTableName+"_"+Alltrim(field_name) kk2=Alltrim(field_name) Do Case Case field_type="C" SQLExec(nhandle,"alter table &lcTableName. drop CONSTRAINT &kk1.") &&删除,防止多次运行 SQLExec(nhandle,"alter table &lcTableName. add CONSTRAINT &kk1. default ' ' for &kk2.") &&字符型默认为二个空格 Case field_type="N" Or field_type="L" SQLExec(nhandle,"alter table &lcTableName. drop CONSTRAINT &kk1.") &&删除,防止多次运行 SQLExec(nhandle,"alter table &lcTableName. add CONSTRAINT &kk1. default 0 for &kk2.") &&数值型或逻辑型默认为0 Endcase Endscan