为什么我的存储过程不会使用我传递的参数? - MySQL

时间:2018-01-15 19:57:06

标签: mysql stored-procedures

我正在尝试创建一个存储过程,它将使用增量递增的数字更新表的一列。

我的代码

CREATE DEFINER=`cis23383518`@`192.168.122.1` PROCEDURE `Increment_rows`(IN TheTable VARCHAR(20),IN TheID VARCHAR(20))
BEGIN
DECLARE i INT;
SET i = 0;
UPDATE TheTable 
SET TheID = i,i= i+1;
END

我正在调用以下程序

CALL Increment_rows('Officer','IncidentID');

我收到以下错误:表cis23383518。表不存在。

2 个答案:

答案 0 :(得分:0)

REFINER不是强制性的。

如果您使用与其创建的用户ID相同的方法调用该过程,请将其删除:

public const string selAppearanceWhenContainedInInstancesOfClasses = "appearanceWhenContainedInInstancesOfClasses:";

public static readonly IntPtr UIKitLibraryHandle = Dlfcn.dlopen("/System/Library/Frameworks/UIKit.framework/UIKit", 0);

[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
public static extern IntPtr IntPtr_objc_msgSend_IntPtr(IntPtr receiver, IntPtr selector, IntPtr arg1);

[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
public static extern void void_objc_msgSend_IntPtr(IntPtr receiver, IntPtr selector, IntPtr arg1);

public static IntPtr GetAppearanceEx(IntPtr class_ptr, params Type[] whenFoundIn)
{
    var ptrs = TypesToPointers(whenFoundIn);
    var handles = NSArray.FromIntPtrs(ptrs);
    using (var array = handles)
    {
        return IntPtr_objc_msgSend_IntPtr(class_ptr, Selector.GetHandle(selAppearanceWhenContainedInInstancesOfClasses), array.Handle);
    }
}

public static IntPtr[] TypesToPointers(Type[] whenFoundIn)
{
    IntPtr[] ptrs = new IntPtr[whenFoundIn.Length];

    for (int i = 0; i < whenFoundIn.Length; i++)
    {
        if (whenFoundIn[i] == null)
            throw new ArgumentException(String.Format("Parameter {0} was null, must specify a valid type", i));
        if (!typeof(NSObject).IsAssignableFrom(whenFoundIn[i]))
            throw new ArgumentException(String.Format("Type {0} does not derive from NSObject", whenFoundIn[i]));

        var classHandle = Class.GetHandle(whenFoundIn[i]);
        if (classHandle == IntPtr.Zero)
            throw new ArgumentException(string.Format("Could not find the Objective-C class for {0}", whenFoundIn[i].FullName));
        ptrs[i] = classHandle;
    }
    return ptrs;
}

答案 1 :(得分:0)

mysql不会用提供的参数替换表名,update会查找名为thetable的表。如果要为过程提供表名,则必须构建一个sql语句并提交,例如

DROP PROCEDURE IF EXISTS `Increment_rows`;
DELIMITER $$

CREATE  PROCEDURE `Increment_rows`(IN TheTable VARCHAR(20),IN TheID VARCHAR(20))
BEGIN

set @sqlstmt =  concat('UPDATE ', THETABLE, ' SET STATUS =  (SELECT @I:=@I + 1 FROM (SELECT @I:=0) I) ORDER BY ID DESC;');
SELECT @SQLSTMT;
prepare stmt from @sqlstmt;
execute stmt;
deallocate prepare stmt;

END $$

请注意使用变量来增加状态列。 所以给出了

+----+----------+--------+
| id | username | status |
+----+----------+--------+
|  1 | John     |      0 |
|  2 | Jane     |      0 |
|  3 | Ali      |      0 |
|  6 | Bruce    |      0 |
|  7 | Martha   |      0 |
|  8 | Sidney   |      0 |
| 10 | charlie  |      0 |
| 12 | Elisa    |      0 |
| 14 | Samantha |      0 |
| 15 | Hannah   |      0 |
| 16 | Hannah   |      0 |
| 17 | Kevin    |      0 |
| 18 | Kevin    |      0 |
| 19 | Ruth     |      0 |
+----+----------+--------+
14 rows in set (0.00 sec)

MariaDB [sandbox]> CALL Increment_rows('USERS','INCIDENTID');
+----------------------------------------------------------------------------------------+
| @SQLSTMT                                                                               |
+----------------------------------------------------------------------------------------+
| UPDATE USERS SET STATUS =  (SELECT @I:=@I + 1 FROM (SELECT @I:=0) I) ORDER BY ID DESC; |
+----------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

MariaDB [sandbox]> select id,username,status from users;
+----+----------+--------+
| id | username | status |
+----+----------+--------+
|  1 | John     |     14 |
|  2 | Jane     |     13 |
|  3 | Ali      |     12 |
|  6 | Bruce    |     11 |
|  7 | Martha   |     10 |
|  8 | Sidney   |      9 |
| 10 | charlie  |      8 |
| 12 | Elisa    |      7 |
| 14 | Samantha |      6 |
| 15 | Hannah   |      5 |
| 16 | Hannah   |      4 |
| 17 | Kevin    |      3 |
| 18 | Kevin    |      2 |
| 19 | Ruth     |      1 |
+----+----------+--------+
14 rows in set (0.02 sec)

注意我认为您的更新语句可能需要where语句。