注意001:@IDENTITY和scope_identity()

本文探讨了在T-SQL中使用@@Identity与scope_identity()的区别。@@Identity可能因触发器导致获取错误的自增列值,而scope_identity()则能准确地返回所需的值。

        很多TSQL方面的书都告诉大家如何使用@@Identity 获取最新的自增列的最新值。网上的文章或杂志也一样这么说。但是你可能不知道,这可能在你的应用程序中引起一些你难以追踪到的bugs。@@Identity的使用可能是一件非常非常坏的事情,你最好在每一个案例中使用scope_identity()进行替代。

       为什么呢?

       @@Identity返回的是你当前连接最新创建自动编号的值。在你第一次使用的时候,它也许没有错,直到有人增加在了触发。如果这个触发引起了另外一个自动编号的创建,猜猜你在调用@@Identity时将会得到什么值呢?不好吧。而scope_identity()就非常好了,它能返回你想要的。 

guo@guo-Dell-G15-5520:~/g2o仿真/src/build$ make Consolidate compiler generated dependencies of target g2o_demo [ 50%] Building CXX object CMakeFiles/g2o_demo.dir/optimize.cc.o /home/guo/g2o仿真/src/optimize.cc: In function ‘std::vector<Eigen::Transform<double, 3, 1> > generateNoisyTrajectory(int)’: /home/guo/g2o仿真/src/optimize.cc:73:44: error: expected primary-expression before ‘(’ token 73 | Quaterniond noise_rot = Quaterniond(AngleAxisd( | ^ /home/guo/g2o仿真/src/optimize.cc:75:32: error: expected ‘)’ before ‘;’ token 75 | Vector3d::UnitZ())); | ^ | ) /home/guo/g2o仿真/src/optimize.cc:73:44: note: to match this ‘(’ 73 | Quaterniond noise_rot = Quaterniond(AngleAxisd( | ^ /home/guo/g2o仿真/src/optimize.cc: In function ‘int main(): /home/guo/g2o仿真/src/optimize.cc:162:9: error: ‘Matrix6d’ was not declared in this scope 162 | Matrix6d info = Matrix6d::Identity(); | ^~~~~~~~ /home/guo/g2o仿真/src/optimize.cc:163:30: error: ‘info’ was not declared in this scope 163 | edge->setInformation(info); | ^~~~ /home/guo/g2o仿真/src/optimize.cc:178:5: error: ‘Matrix6d’ was not declared in this scope 178 | Matrix6d loopInfo = Matrix6d::Identity() * 10.0; | ^~~~~~~~ /home/guo/g2o仿真/src/optimize.cc:179:30: error: ‘loopInfo’ was not declared in this scope 179 | loopEdge->setInformation(loopInfo); | ^~~~~~~~ In file included from /usr/local/include/eigen3/Eigen/Geometry:42, from /home/guo/g2o仿真/src/optimize.cc:4: /usr/local/include/eigen3/Eigen/src/Geometry/Quaternion.h: In instantiation of ‘Derived& Eigen::QuaternionBase<Derived>::operator=(const Eigen::MatrixBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Matrix<double, 3, 1> >, const Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, 3, 3> >, 3, 1, false> >; Derived = Eigen::Quaternion<double>]’: /usr/local/include/eigen3/Eigen/src/Geometry/Quaternion.h:313:90: required from ‘Eigen::Quaternion<Scalar, Options>::Quaternion(const Eigen::MatrixBase<OtherDerived>&) [with Derived = Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Matrix<double, 3, 1> >, const Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, 3, 3> >, 3, 1, false> >; _Scalar = double; int _Options = 0]’ /usr/local/include/eigen3/Eigen/src/Geometry/AngleAxis.h:201:18: required from ‘Eigen::AngleAxis<_Scalar>& Eigen::AngleAxis<Scalar>::operator=(const Eigen::MatrixBase<OtherDerived>&) [with Derived = Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Matrix<double, 3, 1> >, const Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, 3, 3> >, 3, 1, false> >; _Scalar = double]’ /usr/local/include/eigen3/Eigen/src/Geometry/AngleAxis.h:88:85: required from ‘Eigen::AngleAxis<Scalar>::AngleAxis(const Eigen::MatrixBase<OtherDerived>&) [with Derived = Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Matrix<double, 3, 1> >, const Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, 3, 3> >, 3, 1, false> >; _Scalar = double]’ /home/guo/g2o仿真/src/optimize.cc:75:31: required from here /usr/local/include/eigen3/Eigen/src/Geometry/Quaternion.h:583:59: error: incomplete type ‘Eigen::internal::quaternionbase_assign_impl<Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Matrix<double, 3, 1> >, const Eigen::Block<const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, 3, 3> >, 3, 1, false> >, 3, 1>’ used in nested name specifier 583 | internal::quaternionbase_assign_impl<MatrixDerived>::run(*this, xpr.derived()); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~ make[2]: *** [CMakeFiles/g2o_demo.dir/build.make:76:CMakeFiles/g2o_demo.dir/optimize.cc.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:673:CMakeFiles/g2o_demo.dir/all] 错误 2 make: *** [Makefile:146:all] 错误 2 guo@guo-Dell-G15-5520:~/g2o仿真/src/build$
08-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值