简体   繁体   中英

How to add same identity column to different tables?

I have the following stored procedure in my 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. The problem is there is a UserNum value that is persistent in all tables. When I insert data into the first table, this UserNum is generated, it is an auto increment value. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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