多级导入数据.
以下代码可在JS 中生成多级导入数据库的SQL .
如数据:
Logistic Services
* » Air Freight
-- Lifting Services
在数据库里是主子孙关系. 要将它们导入, 需要借助 @@Identity. 下面是生成脚本.
<html>
<head>
<script src="WebCon/jquery-1.3.2.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
// <!CDATA[
function proc() {
var str = "declare @NewID int ;" ;
var Pid = $("#pid").val() ;
var list = $("span") ;
var LastLevel = 0 ;
list.each(function(i)
{
var theOne = $.trim( this.innerHTML ) ;
theOne = theOne.replace("'","''") ;
var theLevel = 0 ;
if (theOne.substr(0,1) == "*" )
{
theLevel = 1 ;
}
else if ( theOne.substr(0,1) == "-")
{
theLevel = 2 ;
}
theOne = $.trim( theOne.replace("* »","").replace("--","") );
if ( theLevel == 0 )
{
str += "insert into category (pid , name ) Select " + Pid + ",'" + theOne + "';\n"
}
else
{
if ( theLevel != LastLevel )
{
str += " select @NewID = @@identity ; " ;
}
str += "insert into category (pid , name ) Select @NewID" + ",'" + theOne + "';\n"
}
LastLevel = theLevel ;
}
) ;
$("#res").val(str) ;
}
// ]]>
</script>
</head>
<body >
PID : <input type="text" id="pid" /><input type="button" onclick="proc();" value="Proc"/><br />
<textarea id="res" cols="100" rows="10"></textarea>
<span>Aircraft @</span>
<span>ATV @</span>
<span>Automobile @</span>
<span>Barrow, Trolley & Cart</span>
<span>Bicycle & Electric Bicycle @</span>
<span>Container & Fittings</span>
<span>Electric Motorcycle @</span>
<span>Elevators & Funicular Cars</span>
<span>Logistic Services</span>
<span> * » Air Freight</span>
<span> -- Lifting Services</span>
<span> * » Logistic Agents</span>
<span> * » Multi-model Transportation</span>
<span> * » Railway Freight</span>
<span> -- Road Freight</span>
<span> * » Sea Freight</span>
<span> * » Storage Services</span>
<span> * » Waterway Freight</span>
<span> * » Others</span>
<span>Marine Craft @</span>
<span>Motorcycle @</span>
<span>Specialized Vehicle @</span>
<span>Tractor</span>
<span>Trailer</span>
<span>Train & Railroad @</span>
<span>Transportation Facilities</span>
<span> * » Gas Station & Equipment</span>
<span> * » Parking Facilities</span>
<span> * » Roadway Facility</span>
<span> * » Roadway Safety @</span>
<span> * » Others</span>
<span>Transportation Product Agents</span>
<span>Transportation Projects</span>
<span>Tricycle @</span>
<span>Truck & Bus @</span>
<span>Others</span>
body>
</html>
<head>
<script src="WebCon/jquery-1.3.2.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
// <!CDATA[
function proc() {
var str = "declare @NewID int ;" ;
var Pid = $("#pid").val() ;
var list = $("span") ;
var LastLevel = 0 ;
list.each(function(i)
{
var theOne = $.trim( this.innerHTML ) ;
theOne = theOne.replace("'","''") ;
var theLevel = 0 ;
if (theOne.substr(0,1) == "*" )
{
theLevel = 1 ;
}
else if ( theOne.substr(0,1) == "-")
{
theLevel = 2 ;
}
theOne = $.trim( theOne.replace("* »","").replace("--","") );
if ( theLevel == 0 )
{
str += "insert into category (pid , name ) Select " + Pid + ",'" + theOne + "';\n"
}
else
{
if ( theLevel != LastLevel )
{
str += " select @NewID = @@identity ; " ;
}
str += "insert into category (pid , name ) Select @NewID" + ",'" + theOne + "';\n"
}
LastLevel = theLevel ;
}
) ;
$("#res").val(str) ;
}
// ]]>
</script>
</head>
<body >
PID : <input type="text" id="pid" /><input type="button" onclick="proc();" value="Proc"/><br />
<textarea id="res" cols="100" rows="10"></textarea>
<span>Aircraft @</span>
<span>ATV @</span>
<span>Automobile @</span>
<span>Barrow, Trolley & Cart</span>
<span>Bicycle & Electric Bicycle @</span>
<span>Container & Fittings</span>
<span>Electric Motorcycle @</span>
<span>Elevators & Funicular Cars</span>
<span>Logistic Services</span>
<span> * » Air Freight</span>
<span> -- Lifting Services</span>
<span> * » Logistic Agents</span>
<span> * » Multi-model Transportation</span>
<span> * » Railway Freight</span>
<span> -- Road Freight</span>
<span> * » Sea Freight</span>
<span> * » Storage Services</span>
<span> * » Waterway Freight</span>
<span> * » Others</span>
<span>Marine Craft @</span>
<span>Motorcycle @</span>
<span>Specialized Vehicle @</span>
<span>Tractor</span>
<span>Trailer</span>
<span>Train & Railroad @</span>
<span>Transportation Facilities</span>
<span> * » Gas Station & Equipment</span>
<span> * » Parking Facilities</span>
<span> * » Roadway Facility</span>
<span> * » Roadway Safety @</span>
<span> * » Others</span>
<span>Transportation Product Agents</span>
<span>Transportation Projects</span>
<span>Tricycle @</span>
<span>Truck & Bus @</span>
<span>Others</span>
body>
</html>
作者:NewSea 出处:http://newsea.cnblogs.com/
QQ,MSN:iamnewsea@hotmail.com 如无特别标记说明,均为NewSea原创,版权私有,翻载必纠。欢迎交流,转载,但要在页面明显位置给出原文连接。谢谢。 |