1

嗨,我继承了一堆旧的经典 ASP 代码,这些代码对我认为旧的 MS access db 中的一些表进行了一些更新。

数据库现在已经转换为 SQL 并且工作正常,但是我需要将一些旧的 ASP 代码转换为等效的 TSQL。TSQL 不是我的强项,希望能帮助我将 vb 脚本转换为等效的 TSQL,以用于名为“UpdateCircuitOrdersComments”的存储过程,它不是我正在努力解决的基本语法,它可以做更多的事情 IE FOR 的 IF 案例、循环等以在sql中实现以下功能。

我知道下面的代码不是很好或者可以做得更好,但这就是我继承的抱歉。

除了“合同期”之外,每个字段都可以通过使用 ajax 传递给 sproc 的 c# 参数提供给我

期待从你们的任何合理建议中学习...

脚本如下:

Dim connect2, Class2, Query2, Counter
Dim indate1, indate2, indate3, aday1, amonth1, ayear1, aday2, amonth2, ayear2, aday3, amonth3, ayear3, length, maintrate, equiprate, stqrrate, startserial, liveserial, endserial

Dim splitArray


Set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open QuotebaseDB

Set Class1 = Server.CreateObject("ADODB.Recordset")
Query =    "SELECT * FROM circuits WHERE ID=" & Request("ID")
Class1.Open Query,Connect,adOpenDynamic,adLockOptimistic

if Class1("Contract Period") <> "" Then
    length = Class1("Contract Period")
    splitArray = split(length, " ")
    length = CInt(splitArray(0))
else
    length = 1
end if

Class1("actual live date") = Request("actuallivedate")
Class1("lastupdater") = Session("username")
Class1("lastupdate") = Date()
Class1("Contract Period") = length

Class1.Update

'=========================================
' Add Rate Information - Based On Actuals
'=========================================

   indate1 = Request("actuallivedate")   
   indate2 = Request("actuallivedate")
   indate3 = Request("actuallivedate")

   aday1 = Left(indate1, 2)
   amonth1 = Mid(indate1, 4, 2)
   ayear1 = Right(indate1, 2)
   aday2 = Left(indate2, 2)
   amonth2 = Mid(indate2, 4, 2)
   ayear2 = Right(indate2, 2)
   aday3 = Left(indate3, 2)
   amonth3 = Mid(indate3, 4, 2)
   ayear3 = Right(indate3, 2) + length

   startserial = dateserial(ayear1, amonth1, aday1)
   liveserial = dateserial(ayear2, amonth2, aday2)
   endserial = dateserial(ayear3, amonth3, aday3)


'========================================================
' Check that current maintenance rate will be superseded
'========================================================


Dim MaintConnect1, MaintRS1, MaintQuery1 

Set MaintConnect1 = Server.CreateObject("ADODB.Connection")
MaintConnect1.Open QuotebaseDB

Set MaintRS1 = Server.CreateObject("ADODB.Recordset")
MaintQuery1 =    "SELECT * FROM maintenancetable WHERE CircuitID=" & Request("ID")
MaintRS1.Open MaintQuery1,MaintConnect1,adOpenDynamic,adLockOptimistic

Do while not MaintRS1.eof

If (MaintRS1("startdate") < startserial) OR (MaintRS1("enddate") > endserial) Then

MaintRS1("startdate") = StartSerial
MaintRS1("enddate") = EndSerial
MaintRS1("circuitnum") = Class1("circuit number")
MaintRS1("modifieddate") = now

MaintRS1.Update

End If

MaintRS1.movenext

Loop

MaintRS1.close
set MaintRS1 = nothing

MaintConnect1.close
set MaintConnect1 = nothing

'========================================================
' Check that current equipment rate will be superseded
'========================================================


Dim EquipConnect1, EquipRS1, EquipQuery1 

Set EquipConnect1 = Server.CreateObject("ADODB.Connection")
EquipConnect1.Open QuotebaseDB

Set EquipRS1 = Server.CreateObject("ADODB.Recordset")
EquipQuery1 =    "SELECT * FROM [equipment table] WHERE CircuitID=" & Request("ID")
EquipRS1.Open EquipQuery1,EquipConnect1,adOpenDynamic,adLockOptimistic

Do while not EquipRS1.eof

If (EquipRS1("startdate") < startserial) OR (EquipRS1("enddate") > endserial) Then

EquipRS1("startdate") = StartSerial
EquipRS1("enddate") = EndSerial
EquipRS1("circuitnum") = Class1("circuit number")
EquipRS1("modifieddate") = now

EquipRS1.Update

End If

EquipRS1.movenext

Loop

EquipRS1.close
set EquipRS1 = nothing

EquipConnect1.close
set EquipConnect1 = nothing

'========================================================
' Check that current rental rate will be superseded
'========================================================

Dim STQRConnect1, STQRRS1, STQRQuery1 

Set STQRConnect1 = Server.CreateObject("ADODB.Connection")
STQRConnect1.Open QuotebaseDB

Set STQRRS1 = Server.CreateObject("ADODB.Recordset")
STQRQuery1 =    "SELECT * FROM STQRtable WHERE CircuitID=" & Request("ID")
STQRRS1.Open STQRQuery1,STQRConnect1,adOpenDynamic,adLockOptimistic

Do while not STQRRS1.eof

If (STQRRS1("startdate") < startserial) OR (STQRRS1("enddate") > endserial) Then

STQRRS1("startdate") = StartSerial
STQRRS1("enddate") = EndSerial
STQRRS1("circuitnum") = Class1("circuit number")
STQRRS1("modifieddate") = now

STQRRS1.Update

End If

STQRRS1.movenext

Loop

STQRRS1.close
set STQRRS1 = nothing

STQRConnect1.close
set STQRConnect1 = nothing

'===========================================
' Update Connection Charge As A One Off Charge
'===========================================

Dim OneConnect, OneRS, OneQuery 

Set OneConnect = Server.CreateObject("ADODB.Connection")
OneConnect.Open QuotebaseDB

Set OneRS = Server.CreateObject("ADODB.Recordset")
OneQuery = "SELECT * FROM OneOffCharges WHERE chargetype = 'Connection Charge' and CircuitID=" & Request("ID")

OneRS.Open OneQuery,OneConnect,adOpenDynamic,adLockOptimistic

If not oners.eof Then

OneRS("chargedate") = startserial
OneRS("modifieddate") = now
OneRS("chargetype") = "Connection Charge"

OneRS.Update

End If

OneRS.close
set OneRS = nothing

OneConnect.close
set OneConnect = nothing

Class1.close
set Class1 = nothing

Connect.close
set Connect = nothing
4

1 回答 1

3

除了遍历记录集之外,这些循环似乎没有做太多其他事情。您不太可能需要在 T-SQL 中执行此操作,因为您可能可以从查询中推断出所有内容。

大多数看起来就像更新语句

-- Check that current maintenance rate will be superseded
UPDATE maintenancetable 
SET StartDate = @startSerial,
    EndDate = @endSerial,
    CircuitNum = @circuitNumber,
    ModifiedDate = CURRENT_TIMESTAMP
WHERE CircuitID=@circuitId
AND (Startdate < @startSerial OR EndDate > @endSerial)

合同期有点令人困惑,它似乎正在获取数组的第一项,所以也许这相当于

SELECT TOP 1 ContractPeriod FROM circuits WHERE ID=@id
-- ? ORDER BY ContractPeriod ?
于 2013-08-22T11:47:00.513 回答