【问题标题】:How to add same identity column to different tables?如何将相同的标识列添加到不同的表?
【发布时间】:2021-02-24 19:24:08
【问题描述】:

我的 MSSQL 中有以下存储过程。

declare @UserNum as int

    insert into auth_table( ID, Password, Login, AuthType, IdentityNo ) 
    values(@id, pwdencrypt(@password), '0', 1, '7700000000000' )

    set @UserNum = @@identity


    insert into charge_auth(usernum, type, expiredate, payminutes)
    values(@UserNum, 0, DATEADD(day, 1000, getdate()), 0)
    
    insert into CashDB..cashaccount (id,UserNum,Cash,CashBonus,UpdateDateTime) values(@id,@UserNum,0,0,GETDATE())

    select @UserNum as usernum

我希望能够将其转换为 PDO PHP,我设法进行了第一次插入。问题是所有表中都有一个UserNum 值。当我将数据插入第一个表时,会生成这个UserNum,它是一个自动增量值。我想同时把它传递给其他表,所以它们是通过这个值连接的。

我不知道如何使用 PHP 和 PDO 来做到这一点。

提前致谢。

【问题讨论】:

    标签: php sql-server pdo


    【解决方案1】:

    试试这个。有关分步说明,请参见 cmets。 (未经测试!)

    // Construct query, using positional variables (?).
    $sql =
        'INSERT INTO ' .
        '`auth_table` ' .
        '( `ID`, `Password`, `Login`, `AuthType`, `IdentityNo` )  ' .
        'VALUES ' .
        '(?, pwdencrypt(?), ?, ?, ?)';
    // Create prepared statement from query.
    $statement = $pdo->prepare($sql);
    // Bind parameters by position, the index of which is 1-based.
    $bindIndex = 1;
    // Increase index on every line. Enforce type as we go.
    $statement->bindValue($bindIndex++, $id, PDO::PARAM_INT);
    $statement->bindValue($bindIndex++, $plaintextPassword, PDO::PARAM_STR);
    $statement->bindValue($bindIndex++, '0', PDO::PARAM_STR);
    $statement->bindValue($bindIndex++, 1, PDO::PARAM_INT);
    $statement->bindValue($bindIndex++, '7700000000000', PDO::PARAM_STR);
    $statement->execute();
    
    // This is your @@identity
    $userNum = $pdo->lastInsertId();
    
    // Second insert.
    $sql =
        'INSERT INTO ' .
        '`charge_auth` ' .
        '(`usernum`, `type`, `expiredate`, `payminutes`) ' .
        'VALUES ' .
        '(?, ?, DATEADD(day, ?, getdate()), ?)';
    $statement = $pdo->prepare($sql);
    $bindIndex = 1;
    $statement->bindValue($bindIndex++, $userNum, PDO::PARAM_INT);
    $statement->bindValue($bindIndex++, 0, PDO::PARAM_INT);
    $statement->bindValue($bindIndex++, 1000, PDO::PARAM_INT);
    $statement->bindValue($bindIndex++, 0, PDO::PARAM_INT);
    $statement->execute();
    
    // Third insert.
    $sql =
        'INSERT INTO ' .
        '`CashDB..cashaccount` ' .
        '(`id`, `UserNum`, `Cash`, `CashBonus`, `UpdateDateTime`) ' .
        'VALUES ' .
        '(?, ?, ?, ?, GETDATE())';
    $statement = $pdo->prepare($sql);
    $bindIndex = 1;
    $statement->bindValue($bindIndex++, $id, PDO::PARAM_INT);
    $statement->bindValue($bindIndex++, $userNum, PDO::PARAM_INT);
    $statement->bindValue($bindIndex++, 0, PDO::PARAM_INT);
    $statement->bindValue($bindIndex++, 0, PDO::PARAM_INT);
    $statement->execute();
    

    【讨论】:

    • 您好,前两个正在工作,但由于某种原因,第三个插入没有向 CashDB 添加任何内容。
    • 没关系,我有错字。它现在正在工作。谢谢!
    • 太棒了!如果您觉得我的回答有帮助,请考虑将其标记为已接受:)。
    • 很抱歉不同意,但我看到的只是一个赞成票(你的?)。接受答案意味着勾选复选标记,然后变为绿色。我不想提出这个问题,但我们都在努力维护我们的声誉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2019-10-08
    • 2022-11-13
    相关资源
    最近更新 更多