想在ASP中创建一个变量来改变一个值

时间:2013-08-18 21:43:14

标签: sql asp-classic

刚刚更新了我的问题。但我有一个数组

Dim divName(3) 'Fixed size array
divName(0) = "DIV1"
divName(1) = "DIV2"
divName(2) = "DIV3"

我想在我的SQL查询中从我的数组中应用一个特定值(“DIV1”)

sql = "SELECT * FROM DivisionNew814 WHERE JMS_UpdateDateTime >= DATEADD(day,-7, GETDATE()) AND  Division ='" & divName (divrec(0)) &"' order by JMS_UpdateDateTime desc" 

它不起作用。

“Divrec”是输出到“Division 1”的变量我想在SQL查询中使用我的数组将该值更改为“DIV1”。

3 个答案:

答案 0 :(得分:1)

<%
if divrec = "Division 1" then
  divrec = "Div1"
end if
%>

答案 1 :(得分:1)

要输出数组值,您需要使用索引:

Dim divName(3) 'Fixed size array
divName(0) = "DIV1"
divName(1) = "DIV2"
divName(2) = "DIV3"


... AND  Division ='" & divName(0) &"' order by ...

如果您需要来自&#34; Division 1&#34;到&#34; DIV1&#34;如果它总是喜欢&#34; Division X&#34;到&#34; DIVX&#34;你可以替换:

... AND  Division ='" & Replace(divrec(0), "Division ", "DIV") &"' order by ...

答案 2 :(得分:0)

听起来你需要的是一个键值集合,这意味着将“分区1”文本“翻译”为“DIV1”。为此,我们有Scripting.Dictionary对象:

Dim divNames
Set divNames = Server.CreateObject("Scripting.Dictionary")
divNames.Add "Division 1", "DIV1"
divNames.Add "Division 2", "DIV2"
divNames.Add "Division 3", "DIV3"

然后:

sql = "SELECT * FROM DivisionNew814 WHERE JMS_UpdateDateTime >= DATEADD(day,-7, GETDATE()) AND  Division ='" & divNames(divrec(0)) &"' order by JMS_UpdateDateTime desc" 

如果divrec只是一个字符串,则更改为:

sql = "SELECT * FROM DivisionNew814 WHERE JMS_UpdateDateTime >= DATEADD(day,-7, GETDATE()) AND  Division ='" & divNames(divrec) &"' order by JMS_UpdateDateTime desc"