sqlscope
❶ 如何在sql中插入记录时返回id(id为自动增长)
insert into [table] values(null) select SCOPE_IDENTITY()
重点在插入后面的select
❷ SQL Server 鏁版嵁搴揿备綍镆ュ嚭链钖庝竴娆℃彃鍏ョ殑涓𨱒
SCOPE_IDENTITY銆両DENT_CURRENT 鍜 @@IDENTITY
鏄鐩镐技镄勫嚱鏁帮纴锲犱负瀹冧滑閮借繑锲炴彃鍏ュ埌镙囱瘑鍒椾腑镄勫笺
IDENT_CURRENT 涓嶅弹浣灭敤锘熷拰浼氲瘽镄勯檺鍒讹纴钥屽弹闄愪簬鎸囧畾镄勮〃銆侷DENT_CURRENT
杩斿洖涓轰换浣曚细璇濆拰浣灭敤锘熶腑镄勭壒瀹氲〃镓鐢熸垚镄勫笺傛湁鍏宠︾粏淇℃伅锛岃峰弬阒匢DENT_CURRENT
(Transact-SQL)銆
SCOPE_IDENTITY 鍜 @@IDENTITY
杩斿洖鍦ㄥ綋鍓崭细璇濅腑镄勪换浣曡〃鍐呮墍鐢熸垚镄勬渶钖庝竴涓镙囱瘑鍊笺备絾鏄锛孲COPE_IDENTITY 鍙杩斿洖鎻掑叆鍒板綋鍓崭綔鐢ㄥ烟涓镄勫硷绂@@IDENTITY
涓嶅弹闄愪簬鐗瑰畾镄勪綔鐢ㄥ烟銆
渚嫔傦纴链変袱涓琛 T1 鍜 T2锛屽苟涓斿湪 T1 涓婂畾涔変简 INSERT 瑙﹀彂鍣ㄣ傚綋灏嗘煇琛屾彃鍏 T1 镞讹纴瑙﹀彂鍣ㄨ婵鍙戯纴骞跺湪 T2
涓鎻掑叆涓琛屻傝ユ柟妗堟紨绀轰简涓や釜浣灭敤锘燂细鍦 T1 涓婄殑鎻掑叆锛屼互鍙婂湪 T2 阃氲繃瑙﹀彂鍣ㄧ殑鎻掑叆銆
锅囱 T1 鍜 T2 閮芥湁镙囱瘑鍒楋纴@@IDENTITY 鍜 SCOPE_IDENTITY 灏嗗湪 T1 涓婄殑 INSERT
璇鍙ョ殑链钖庤繑锲炰笉钖岀殑鍊笺侤@IDENTITY 灏呜繑锲炲湪褰揿墠浼氲瘽涓镄勪换浣曚綔鐢ㄥ烟鍐呮彃鍏ョ殑链钖庝竴涓镙囱瘑鍒楃殑鍊笺傝繖鏄鍦 T2
涓鎻掑叆镄勫笺係COPE_IDENTITY() 灏呜繑锲炲湪 T1 涓鎻掑叆镄 IDENTITY 鍊笺傝繖鏄鍦ㄥ悓涓涓浣灭敤锘熷唴鍙戠敓镄勬渶钖庣殑鎻掑叆銆傚傛灉鍦ㄤ换浣 INSERT
璇鍙ヤ綔鐢ㄤ簬浣灭敤锘熶腑镄勬爣璇嗗垪涔嫔墠璋幂敤 SCOPE_IDENTITY() 鍑芥暟锛屽垯璇ュ嚱鏁板皢杩斿洖 Null銆
濡傛灉璇鍙ュ拰浜嫔姟澶辫触锛屽畠浠浼氭洿鏀硅〃镄勫綋鍓嶆爣璇嗭纴浠庤屼娇镙囱瘑鍒椾腑镄勫煎嚭鐜颁笉杩炶疮鐜拌薄銆傚嵆浣挎湭鎻愪氦璇曞浘钖戣〃涓鎻掑叆鍊肩殑浜嫔姟锛屼篃姘歌繙镞犳硶锲炴粴镙囱瘑鍊笺备緥濡傦纴濡傛灉锲
IGNORE_DUP_KEY 鍐茬獊钥屽艰嚧 INSERT 璇鍙ュけ璐ワ纴琛ㄧ殑褰揿墠镙囱瘑鍊间粛铹朵细澧炲姞銆
鑺傞夎嚜sqlserver 鏁欑▼ SCOPE_IDENTITY (Transact-SQL)