您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何将临时变量设置为基于另一个表的值

如何将临时变量设置为基于另一个表的值

语法没有错

SET @AccountID = (SELECT AccountID
                  FROM   Transactions
                  WHERE  TransID = @TranID)
SET @WithdrawalCount = (SELECT WithdrawalCount
                        FROM   Accounts
                               INNER JOIN Transactions
                                       ON Transactions.AccountID = Accounts.AccountID
                        WHERE  Transactions.AccountID = @AccountID)

但是在这里,您尝试将TransID = @ TranID的AccountID设置为@AccountID。如果您的交易记录表中@TranID有多行,则最后插入的值将分配给该变量,因此请尝试top 1order by

SET @AccountID = (SELECT top 1 AccountID
                  FROM   Transactions
                  WHERE  TransID = @TranID order by column)

SET @WithdrawalCount = (SELECT top 1 WithdrawalCount
                        FROM   Accounts
                               INNER JOIN Transactions
                                       ON Transactions.AccountID = Accounts.AccountID
                        WHERE  Transactions.AccountID = @AccountID order by column)
其他 2022/1/1 18:53:19 有434人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶