我有一个将值插入表中的过程。 / I; L) b8 M8 D: j* [ Q4 a2 bdeclare @fName varchar(50),@lName varchar(50),@check tinyintINSERT INTO myTbl(fName,lName) values(@fName,@lName)编辑:7 d. G; [! F9 g: T
现在我想检查它是否成功插入设置@check = 0否则@check = 1# b0 p, `; B+ J) t _" W2 @7 b
: T" |0 a( Q* ~4 d0 w 解决方案: / W9 @ b' Q% I" w" M6 r( t& _- ]2 M
您可以@@ROWCOUNT通过插入操作,立即使用服务器变量检查受影响的行数。" _, r0 B# j" x; F0 g9 v3 R4 a0 a+ T
declare @fName varchar(50) = 'Abcd @lName varchar(50) = 'Efgh'INSERT INTO myTbl(fName,lName) values(@fName,@lName)PRINT @@ROWCOUNT --> 0- means no rows affected/nothing inserted --> 1- means your row has been inserted successfully您可以根据您的要求使用一个Case句子(根据评论): 6 x" u) n: z# p! ?, h--If you need @check as a bit type please change Int to bitDECLARE @check Int = CASE WHEN @@ROWCOUNT = 0 THEN 1 ELSE 0 END