找回密码
 加入
搜索
查看: 4264|回复: 4

[原创] Mssql.au3的update函数增加多列update功能

[复制链接]
发表于 2013-12-5 16:58:35 | 显示全部楼层 |阅读模式
本帖最后由 anypac 于 2013-12-5 17:30 编辑

由于个人经常使用Mssql,原本的udf里面的update过程只能更新单列数据,现在加入多列更新,分隔符"|",列和值的数量要相等,如果要更新的值含有"|"字符的,请单列更新
;===============================================================================
;
; 函数名称.........:    _MSSQL_UpdateRecord
; 描述.............:    在数据库表中修改指定列的数据
; 语法.............:    _MSSQL_UpdateRecord($oConnectionObj, $sTable, $sColumn , $sValue, $condition = "")
; 参数(s)..........:    $oConnectionObj = Object, returned by _MSSQL_Con
;                       $sTable = 表名
;                       $sColumn = 列名,多列更新用"|"作为分隔符
;                       $sValue = 新值,多列更新用"|"作为分隔符
;                       $condition = [可选参数] 查询的条件,可指定某值为查询内容 [默认 = "" (不按条件查询)]
; 返回值(s)........:    成功 - 1
;                       失败 - 0, sets @error
;                       |1 - $oConnectionObj is not an object
;                       |2 - $sTable does not exist
;                       |3 - $sColumn does not exist
;                                                |4 - $sColumn 和  $sValue 数量不匹配
; 作者.............:    TheLuBu <LuBu@veytal.com>
;2013-12-05 增加多列更新功能<anypac>
;===============================================================================
Func _MSSQL_UpdateRecord($oConnectionObj, $sTable, $sColumn, $sValue, $condition = "")
        Local $tableexist, $columnexist, $str, $quer, $sColumnArr, $sValueArr, $sStr = ""
        If StringInStr($sColumn, "|") > 0 Then
                If StringInStr($sValue, "|") > 0 Then
                        $sColumnArr = StringSplit($sColumn, "|")
                        $sValueArr = StringSplit($sValue, "|")
                        For  $i = 1 To $sColumnArr[0]
                                $columnexist = _MSSQL_ColumnExist($oConnectionObj, $sTable, $sColumnArr[$i])
                                If $columnexist = 0 Then Return SetError(3, 0, 0)
                        Next
                        If Not @error Then
                                If $sColumnArr[0] = $sValueArr[0] Then
                                        For $i = 1 To $sColumnArr[0]
                                                $sStr = $sStr & $sColumnArr[$i] & "=" & $sValueArr[$i] & ","
                                        Next
                                        $sStr = StringTrimRight($sStr, 1)
                                Else
                                        SetError(4, 0, 0)
                                EndIf
                        EndIf
                Else
                        SetError(4, 0, 0)
                EndIf
        Else
                $columnexist = _MSSQL_ColumnExist($oConnectionObj, $sTable, $sColumn)
                If $columnexist = 0 Then Return SetError(3, 0, 0)
                If Not @error Then $sStr = $sColumn & "=" & $sValue
        EndIf
        If IsObj($oConnectionObj) And Not @error Then
                $tableexist = _MSSQL_TableExist($oConnectionObj, $sTable)
                If $tableexist = 0 Then Return SetError(2, 0, 0)
                If $condition = "" Then
                        $str = "UPDATE " & $sTable & " SET " & $sStr & ";"
                        $quer = $oConnectionObj.execute($str)
                Else
                        $str = "UPDATE " & $sTable & " SET " & $sStr & " " & $condition & ";"
                        $quer = $oConnectionObj.execute($str)
                EndIf
                Return 1
        Else
                Return SetError(1, 0, 0)
        EndIf
EndFunc   ;==>_MSSQL_UpdateRecord
 楼主| 发表于 2013-12-5 17:00:05 | 显示全部楼层
对了,不要替换,本人水平有限,标注了老的,增加上面的代码,如果有错,请提出
发表于 2013-12-5 21:01:48 | 显示全部楼层
好的,标注收藏,以后要用到的。
发表于 2013-12-31 15:16:24 | 显示全部楼层
学习,以后要用到的!
发表于 2015-12-20 00:38:36 | 显示全部楼层
感謝大大分享
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-29 14:44 , Processed in 0.071392 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表