简体   繁体   English

如何将相同的标识列添加到不同的表?

[英]How to add same identity column to different tables?

I have the following stored procedure in my MSSQL.我的 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

I want to be able to convert this into PDO PHP, I managed to do the first insert.我希望能够将其转换为 PDO PHP,我设法完成了第一次插入。 The problem is there is a UserNum value that is persistent in all tables.问题是有一个UserNum值在所有表中都是持久的。 When I insert data into the first table, this UserNum is generated, it is an auto increment value.当我将数据插入第一个表时,会生成这个UserNum ,它是一个自动递增的值。 I want to pass this to the other tables at the same time, so they are connected by this value.我想同时把这个传递给其他表,所以它们通过这个值连接起来。

I've no idea how to do this using PHP and PDO.我不知道如何使用 PHP 和 PDO 来做到这一点。

Thanks in advance.提前致谢。

Try this.试试这个。 See comments for step-by-step explanation.有关分步说明,请参阅评论。 (Untested!) (未经测试!)

// 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM