Sql Server - SQL分页存储过程自动生成

< script language ='vbscript' >
dim field_info,theexceptfield,tablename
function createsqlstr
dim str : str=""
str=str & "@num int" & " < br />"
str=str & "@page int" & " < br />"
for each thefieldname in field_info            
str=str & "@" & field_info(thefieldname) & " < br />"
next
str=str & "@" & theexceptfield & " < br />"

str=str & "declare @thepage int" & " < br />"

str=str & "set @thepage=(@page-1) * @num" & " < br />"

    

str=str & "set @sql='select top '+cast(@num as nvarchar(30))+' * from " & tablename
             i=1
    for each thefieldname in field_info
        if i=1 then str=str & " where "
            str=str & field_info(thefieldname) & "='+cast(@" & field_info(thefieldname) & " as nvarchar(30))+' and "
            i=i+1
    next
    str=str & theexceptfield & " not in(select top '+cast(@thepage as nvarchar(30))+'" & " " & theexceptfield
    str=str & " from " & tablename &    " where "
    i=1
    for each thefieldname in field_info
            if i < >1 then str=str & " and "
            str=str & field_info(thefieldname) & "='+cast(@" & field_info(thefieldname) & " as nvarchar(30))+'"
            i=i+1
    next
    str=str & "order by " & theexceptfield &") order by " & theexceptfield & "'"
    createsqlstr=str
end function


function setfieldnum
set thefieldname=document.getElementById("fieldname")
set thesetnum=document.getElementById("setnum")
thenum=thesetnum.value
if isnumeric(thenum) then
         thenum=Cint(thenum)
        
         str="请输入您所要查询的字段名:" & " < br />"
         for i=0 to thenum-1
                 str=str & "字段" & i+1 & ": < input type ='text' name ='fieldnames' size ='30' />" & " < br />"
         next
         str=str & "请输入您所用的标识字段名:" & " < input type ='text' id ='exceptfield' />"    & " < br />"
         str=str & "请输入表明:" & " < input type ='text' id ='tablenamed' />"    & " < br />"
         str=str & " < a href ='vbscript:saveinfo()' >显示sql </a>"
         thefieldname.innerHTML=str
end if
end function

function saveinfo()
set theshowsql=document.getElementById("showsql")
set thefieldnames=document.getElementsByName("fieldnames")
set the_exceptfield=document.getElementById("exceptfield")
set field_info=createobject("scripting.dictionary")
set thetablename=document.getElementById("tablenamed")
theexceptfield=the_exceptfield.value
tablename=thetablename.value
for i=0 to thefieldnames.length-1
     thevalue=thefieldnames(i).value
     field_info.add thevalue,thevalue
next
str=createsqlstr
theshowsql.innerhtml=str
end function
</script>
< html >
< head >
< base target ="_self" >
</head>
< body >
设定: < input type ='text' id ='setnum' size ='2' />---- < a href ='vbscript:setfieldnum()' >确定 </a>
< div id ='fieldname' >
</div>
< div id ='showsql' >
</div>
</body>
</html>

你可能感兴趣的:(sql,数据库,server,自动,休闲)