我编写了一个脚本,该脚本连接到Oracle数据库,以选择特定表中的多个条目。
声明如下:
rs.open "SELECT PATH301 FROM NC301B WHERE EDIPROC like 'P30_' AND (LF301M > 0) AND (PATH301 NOT LIKE '%saptemp%') AND (PATH301 NOT LIKE '%SAPTEMP%') AND (PATH301 NOT LIKE '%usr%') AND (PATH301 NOT LIKE '%Windows%');", cn, 3
现在我想知道是否可以将此语句拆分为多行。例如:
rs.open "SELECT PATH301 FROM NC301B
WHERE EDIPROC like 'P30_'
AND (LF301M > 0)
AND (PATH301 NOT LIKE '%saptemp%')
AND (PATH301 NOT LIKE '%SAPTEMP%')
AND (PATH301 NOT LIKE '%usr%')
AND (PATH301 NOT LIKE '%Windows%');", cn, 3
第一个SELECT
声明是大而不好看的方式。
我希望你明白我的意思。
答案 0 :(得分:34)
我还是要发一个答案,从来没有成为Fastest Gun in the West Problem
的粉丝正如我mentioned in the comments所说,你想让SQL命令在代码中更具可读性。正常的方法是使用Line Continuation Character (_
) also known as a Statement Break。
Dim sql
sql = "SELECT PATH301 " & _
"FROM NC301B " & _
"WHERE EDIPROC like 'P30_' " & _
"AND (LF301M > 0) " & _
"AND (PATH301 NOT LIKE '%saptemp%') " & _
"AND (PATH301 NOT LIKE '%SAPTEMP%') " & _
"AND (PATH301 NOT LIKE '%usr%') " & _
"AND (PATH301 NOT LIKE '%Windows%');"
请记住,如果我们不处理字符串,这会有所不同,例如继续将If
语句添加到两行上会看起来像这样;
If result = ( _
condition1 _
And condition2 _
And condition3) Then
因为我们不能跨行破坏字符串,所以我们通过终止继续行的字符串然后将字符串连接到下一个字符串& _
来作弊。这也意味着这将起作用;
Dim sql
sql = "SELECT PATH301 " _
& "FROM NC301B " _
& "WHERE EDIPROC like 'P30_' " _
& "AND (LF301M > 0) " _
& "AND (PATH301 NOT LIKE '%saptemp%') " _
& "AND (PATH301 NOT LIKE '%SAPTEMP%') " _
& "AND (PATH301 NOT LIKE '%usr%') " _
& "AND (PATH301 NOT LIKE '%Windows%');"
这完全归功于个人偏好,并且没有任何方式比它的对手有任何性能优势。
就个人而言,虽然我发现这种方法比较麻烦,但值得一提,特别是在SQL字符串方面。
假设您想在没有其中一个条件的情况下进行测试,请说
AND (PATH301 NOT LIKE '%SAPTEMP%')
尝试注释掉该行会生成
Microsoft VBScript编译错误:
语法错误
这是一个例子;
Dim sql
sql = "SELECT PATH301 " & _
"FROM NC301B " & _
"WHERE EDIPROC like 'P30_' " & _
"AND (LF301M > 0) " & _
"AND (PATH301 NOT LIKE '%saptemp%') " & _
'"AND (PATH301 NOT LIKE '%SAPTEMP%') " & _
"AND (PATH301 NOT LIKE '%usr%') " & _
"AND (PATH301 NOT LIKE '%Windows%');"
这使得它非常不灵活,这就是为什么我更喜欢使用String Concatenation来构建一个字符串,就像这样;
Dim sql: sql = ""
sql = sql & "SELECT PATH301 "
sql = sql & "FROM NC301B "
sql = sql & "WHERE EDIPROC like 'P30_' "
sql = sql & "AND (LF301M > 0) "
sql = sql & "AND (PATH301 NOT LIKE '%saptemp%') "
sql = sql & "AND (PATH301 NOT LIKE '%SAPTEMP%') "
sql = sql & "AND (PATH301 NOT LIKE '%usr%') "
sql = sql & "AND (PATH301 NOT LIKE '%Windows%');"
虽然有一点额外的工作,但它更灵活,允许你通过注释掉行而不影响整个SQL字符串来测试SQL字符串,就像这样;
Dim sql: sql = ""
sql = sql & "SELECT PATH301 "
sql = sql & "FROM NC301B "
sql = sql & "WHERE EDIPROC like 'P30_' "
sql = sql & "AND (LF301M > 0) "
sql = sql & "AND (PATH301 NOT LIKE '%saptemp%') "
'sql = sql & "AND (PATH301 NOT LIKE '%SAPTEMP%') "
sql = sql & "AND (PATH301 NOT LIKE '%usr%') "
sql = sql & "AND (PATH301 NOT LIKE '%Windows%');"
我喜欢做的另一件小事就是按照& vbNewLine
这样跟着每一行;
Dim sql: sql = ""
sql = sql & "SELECT PATH301 " & vbNewLine
sql = sql & "FROM NC301B " & vbNewLine
sql = sql & "WHERE EDIPROC like 'P30_' " & vbNewLine
sql = sql & "AND (LF301M > 0) " & vbNewLine
sql = sql & "AND (PATH301 NOT LIKE '%saptemp%') " & vbNewLine
sql = sql & "AND (PATH301 NOT LIKE '%SAPTEMP%') " & vbNewLine
sql = sql & "AND (PATH301 NOT LIKE '%usr%') " & vbNewLine
sql = sql & "AND (PATH301 NOT LIKE '%Windows%');"
这样当输出字符串(使用经典ASP,WScript等)时,它正确格式化并在HTML页面中显示(如果使用经典ASP)你可以轻松使用
sql = Replace(sql, vbNewLine, "<br />")
正确格式化,在尝试使用动态SQL调试问题时非常有用。
答案 1 :(得分:12)
SQL语句是一个字符串,因此您可以将其拆分为多个字符串并WM_SETTEXT。使用行继续运算符(_
)来包装语句。出于可读性的原因,我将查询放在一个变量中,如下所示:
qry = "SELECT PATH301 FROM NC301B " & _
"WHERE EDIPROC like 'P30_' AND (LF301M > 0) " & _
"AND (PATH301 NOT LIKE '%saptemp%') " & _
"AND (PATH301 NOT LIKE '%SAPTEMP%') " & _
"AND (PATH301 NOT LIKE '%usr%') " & _
"AND (PATH301 NOT LIKE '%Windows%');"
rs.open qry, cn, 3
VBScript不支持多行字符串。
答案 2 :(得分:4)
使用续行(_
)和连接或加入:
s = "a b c"
WScript.Echo s
s = "a " _
& "b " _
& "c"
WScript.Echo s
s = Join(Array( _
"a" _
, "b" _
, "c" _
))
WScript.Echo s
输出:
cscript 37564704.vbs
a b c
a b c
a b c
答案 3 :(得分:3)
应该做你想要的,假设你只是希望它看起来更好;这根本不影响任何功能......
rs.open "SELECT PATH301 FROM NC301B " & _
"WHERE EDIPROC like 'P30_' " & _
"AND (LF301M > 0) " & _
"AND (PATH301 NOT LIKE '%saptemp%') " & _
"AND (PATH301 NOT LIKE '%SAPTEMP%') " & _
"AND (PATH301 NOT LIKE '%usr%') " & _
"AND (PATH301 NOT LIKE '%Windows%');", cn, 3