<%
sFileName = "alter_manage.asp"
tableName = "C_alter"
tableName2 = "product"
action = Trim(Request.QueryString("action"))
%>
<%
Select Case action
case ""
Call ShowContent()
case "add"
Call add()
case "addsave"
Call addsave()
case "modify"
Call modify()
case "modifysave"
Call modifysave()
case "delall"
Call delall()
End Select
%>
<%
Sub ShowContent()
Call gRsClose()
sSql = "select * from view_alter"
oRs.Open sSql,oConn,1,1
%>
<%
Call gRsClose()
End Sub
%>
<%
Sub add()
%>
<%
End Sub
%>
<%
Sub addsave()
a_classid = GetSafeStr(Trim(Request.Form("a_classid")))
a_name = GetSafeStr(Trim(Request.Form("a_name")))
a_detail = GetSafeStr(Trim(Request.Form("a_detail")))
a_type = GetSafeStr(Trim(Request.Form("a_type")))
a_regtime = now()
'判断主表中要添加的字段是否存在
eSql = "select * from syscolumns where id=object_id('"& tableName2 &"') and name='"& a_name &"'"
Call gRsClose()
oRs.Open eSql,oConn,1,3
if not oRs.EOF then
Call ErrorMsg(sFileName,"主表中字段已经存在在,三秒后返回",1)
end if
Call gRsClose()
'将字段相关信息添加到从表
sSql = "select * from "& tableName &" where 1=1"
Call gRsClose()
oRs.Open sSql,oConn,1,3
oRs.AddNew()
oRs("a_classid") = a_classid
oRs("a_name") = a_name
oRs("a_detail") = a_detail
oRs("a_type") = a_type
oRs("a_regtime") = a_regtime
oRs.Update()
'添加字段
oConn.Execute("ALTER TABLE "& tableName2 &" ADD "& a_name &" "& a_type &"")
Call ErrorMsg(sFileName,"添加字段成功,三秒后返回",1)
Call gRsClose()
End Sub
%>
<%
Sub modify()
%>
<%
Call gRsClose()
a_id = Trim(Request.QueryString("a_id"))
sSql = "select * from view_alter where a_id = "& a_id
oRs.Open sSql,oConn,1,1
If not oRs.eof and not oRs.bof Then
a_classid = oRs("a_classid")
a_name = oRs("a_name")
a_detail = oRs("a_detail")
a_type = oRs("a_type")
End If
Call gRsClose()
%>
<%
End Sub
%>
<%
Sub modifysave()
a_classid = GetSafeStr(Trim(Request.Form("a_classid")))
a_name = GetSafeStr(Trim(Request.Form("a_name")))
a_detail = GetSafeStr(Trim(Request.Form("a_detail")))
a_type = GetSafeStr(Trim(Request.Form("a_type")))
a_regtime = now()
a_id = Trim(Request.QueryString("a_id"))
'将字段相关信息添加到从表
sSql = "select * from "& tableName &" where a_id = "& a_id
Call gRsClose()
oRs.Open sSql,oConn,1,3
aname = oRs("a_name")
atype = oRs("a_type")
on error resume next
oRs("a_classid") = a_classid
'判断主表中是否有相同的字段(保持主从表中的数据一致)
set rs1=oConn.Execute("select 1 from syscolumns where id=object_id('"& tableName2 &"') and name='"& a_name &"'")
If rs1.eof and rs1.bof Then
oRs("a_name") = a_name
End If
rs1.close()
oRs("a_detail") = a_detail
oRs("a_type") = a_type
oRs.Update()
If a_name<>aname Then
'修改字段名称(判断主表中是否有相同的字段)
oConn.Execute("if not exists(select 1 from syscolumns where id=object_id('"& tableName2 &"') and name='"& a_name &"') EXEC sp_rename '"& tableName2 &".["& aname &"]', '"& a_name &"', 'COLUMN'")
End If
If a_type<>atype Then
'修改字段类型
oConn.Execute("ALTER TABLE "& tableName2 &" ALTER COLUMN "& a_name &" "& a_type &"")
End If
Call ErrorMsg(sFileName,"修改字段成功,三秒后返回",1)
Call gRsClose()
End Sub
%>
<%
Sub delall()
checkzi = Trim(Request("checkzi"))
If checkzi <> "" Then
Dim z1
z1 = tableName
Dim temp_id,last_id
last_id="("
temp_id = Split(Request("checkzi"),",")
For i=0 To UBound(temp_id)
last_id = last_id & Trim(temp_id(i)) & ","
Next
last_id = Left(last_id,Len(last_id)-1)
last_id = last_id & ")"
'last_id为最终要删除的记录ID
'取出要删除的对应字段
sSql = "select a_name from "& z1 &" where a_id in " & last_id
Call gRsClose()
oRs.Open sSql,oConn,1,1
dim a_names
a_names = ""
jj=1
do while not oRs.eof and not oRs.bof
If jj <> oRs.recordcount Then
a_names = a_names & oRs("a_name") &","
Else
a_names = a_names & oRs("a_name")
End If
oRs.movenext
jj = jj + 1
loop
Call gRsClose()
'删除主表对应字段
a_name = Split(a_names,",")
for i=0 to Ubound(a_name)
oConn.Execute("ALTER TABLE "& tableName2 &" DROP COLUMN "& a_name(i) &"")
next
'删除从表中字段的意义信息
sSql = "delete from " & z1 & " where a_id in " & last_id
oConn.Execute(sSql)
Call ErrorMsg(sFileName,"成功删除所选记录,三秒后将返回……",1)
End If
End Sub
%>