日期:2014-05-17  浏览次数:20801 次

求助代码写法...谢谢.
下面是我的代码(asp+sql)..变量a1对应数据库b1字段...依此类推:  
<%
a1=request( "a1 ")
a2=request( "a2 ")
a3=request( "a3 ")
a4=request( "a4 ")
a5=request( "a5 ")
dim   rs,sql
set   rs=server.createobject( "adodb.recordset ")
if   a3= " "   then
    if   a4= " "   then
sql= "update   A   set   b1= ' "&(a1)& " ',b2= ' "&(a2)& " ',b3=null,b4=null,b5= ' "&(a5)& " ' "
else
sql= "update   A   set   b1= ' "&(a1)& " ',b2= ' "&(a2)& " ',b3=null,b4= ' "&(a4)& " ',b5= ' "&(a5)& " ' "
end   if
  else
      if   a4= " "   then
  sql= "update   A   set   b1= ' "&(a1)& " ',b2= ' "&(a2)& " ',b3= ' "&(a3)& " ',b4=null,b5= ' "&(a5)& " ' "
else
  sql= "update   A   set   b1= ' "&(a1)& " ',b2= ' "&(a2)& " ',b3= ' "&(b3)& " ',b4= ' "&(b4)& " ',b5= ' "&(a5)& " ' "
    end   if
end   if

rs.open   sql,conn,1,3
%>
我这个代码判断当变量a3,a4是否为空时的sql代码..如果变量多起来,那么我这么写会累死人,并且一个不小心
就会出错,请问各位代码应该怎么改?.谢谢..

------解决方案--------------------
update tablename set xxx= ' '
是没问题的,只要是字符型的
------解决方案--------------------
改成这样你看行不行
a1=request( "a1 ")
if a1= " " then a1=null
a2=request( "a2 ")
if a2= " " then a2=null
依此类推,然后
sql= "update A set b1= ' "&(a1)& " ',b2= ' "&(a2)& " ',b3= ' "&(a3)& " ',b4= ' " & (a4) & " ',b5= ' "&(a5)& " ' "

------解决方案--------------------
写一个函数好了
Function checkNull(str)
checkNull = False
if trim(str)= " " or isnull(str) then
checkNull = True
end if
end Function
------解决方案--------------------
dim a,b
for i=1 to 9999
cha=cha& ", " & cstr(i)
next
a=split(cha, ", ")
b=split(cha, ", ")
'以上构造数组,t为上面的a,s
for i=1 to uboud(a)
if b(i) <> " " then
sql= "update A set b "& i & = ' " & "a " & i & " ' "……这里不细写了,你根据判断,依次迭加构造SQL即可。
end if
next
------解决方案--------------------
if request( "a1 ")= " " then
str1= "b1=request( 'a1 ') "
else
str1= "b1=null "
end if
................依次类推
sql= "update A set " & str1 & ", " &str2.........

看看