不单是在DW中
对此你可以专门写个函数。主要是用来校验主键是否重复,这在PB开发中是长用到的。
我给你一个我们程序里使用的,也是我写的(可根据需要修改为检测其他类型主键是否冲突。
f_validateprimarykey(datawindow,string,string,string,string)
/*
检查主键是否冲突
主键类型为字符型
*/
long i,j
long ll_ret,ll_rowcount
string ls_valueold1, ls_valueold2, ls_valueold3,ls_value1, ls_value2, ls_value3
integer ll_valueold, ll_value
string ls_scolcname1, ls_scolcname2, ls_scolcname3, ls_lcolcname, ls_temp
ll_rowcount=dwo.rowcount()
if ll_rowcount>1 then
dwo.accepttext()
ls_scolcname1=dwo.Describe( rs_scolname1 + "_t.text" )
ls_scolcname2=dwo.Describe( rs_scolname2 + "_t.text" )
ls_scolcname3=dwo.Describe( rs_scolname3 + "_t.text" )
ls_lcolcname=dwo.Describe( rs_lcolname + "_t.text" )
ls_temp="关键字字段:"
if rs_scolname1<>"" then
if right(ls_scolcname1,1)=":" then
ls_temp=ls_temp + left( ls_scolcname1, len( ls_scolcname1 ) -1 ) + "、"
else
ls_temp=ls_temp +ls_scolcname1 + "、"
end if
end if
if rs_scolname2<>"" then
if right(ls_scolcname2,1)=":" then
ls_temp=ls_temp + left( ls_scolcname2, len( ls_scolcname2 ) -1 ) + "、"
else
ls_temp=ls_temp + ls_scolcname2 + "、"
end if
end if
if rs_scolname3<>"" then
if right(ls_scolcname3,1)=":" then
ls_temp=ls_temp + left( ls_scolcname3, len( ls_scolcname3 ) -1 ) + "、"
else
ls_temp=ls_temp + ls_scolcname3 + "、"
end if
end if
if rs_lcolname<>"" then
if right(ls_lcolcname,1)=":" then
ls_temp=ls_temp + left( ls_lcolcname, len( ls_lcolcname ) -1) + "、"
else
ls_temp=ls_temp + ls_lcolcname + "、"
end if
end if
ls_temp=left( ls_temp, len(ls_temp )-2 )
for i=1 to ll_rowcount
//获得第i行的记录值
if rs_scolname1<>"" then ls_valueold1=dwo.getitemstring(i,rs_scolname1)
if rs_scolname2<>"" then ls_valueold2=dwo.getitemstring(i,rs_scolname2)
if rs_scolname3<>"" then ls_valueold3=dwo.getitemstring(i,rs_scolname3)
if rs_lcolname<>"" then ll_valueold=dwo.getitemnumber(i,rs_lcolname)
for j=i+1 to ll_rowcount
//获得第j行的记录值
if rs_scolname1<>"" then ls_value1=dwo.getitemstring(j,rs_scolname1)
if rs_scolname2<>"" then ls_value2=dwo.getitemstring(j,rs_scolname2)
if rs_scolname3<>"" then ls_value3=dwo.getitemstring(j,rs_scolname3)
if rs_lcolname<>"" then ll_value=dwo.getitemnumber(j,rs_lcolname)
if ls_valueold1=ls_value1 and ls_valueold2=ls_value2 and ls_valueold3=ls_value3 and ll_valueold=ll_value then
msgerror("第"+string(i)+","+string(j)+"条记录关键字重复!~n" + ls_temp)
dwo.Scrolltorow( i )
dwo.setfocus()
dwo.SetColumn( rs_scolname1 )
return i
end if
next
next
end if
return 0