得到剛插入資料庫的自動增量的ID值

2021-08-28 22:37:28 字數 1057 閱讀 2970

dataset dss = sqlhelper.executedateset(sqlhelper.connectionstringlocaltransaction, commandtype.text, "insert into [w2_user] (w2_user_code,w2_user_name,w2_user_word_1,w2_user_word_2,w2_user_phone,w2_user_adress,w2_user_sfz,w2_user_xjb,w2_user_dzb,w2_user_isbdzx) values (@w2_user_code,@w2_user_name,@w2_user_word_1,@w2_user_word_2,@w2_user_phone,@w2_user_adress,@w2_user_sfz,0,0,@w2_user_isbdzx) select scope_identity() as id", param);

在sql語句的尾巴上加上這樣一句就可以了「 select scope_identity() as id」

但是我在做更新的時候,發現這檔一句卻不能得到id

後來用到了output inserted.w2_code_id  才搞定,但是一定要注意,如果更新後的數值和原先的一樣,也就是說數值更本沒根新的話,是得不到id值的。

dataset ds_zdhl = sqlhelper.executedateset(sqlhelper.connectionstringlocaltransaction, commandtype.text, "update [w2_code] set w2_code_sj=" + dsd.tables[0].rows[0][0].tostring() + "output inserted.w2_code_id  where w2_code_id=(select w2_code_id from [w2_code] where w2_code_id<>" + dsd.tables[0].rows[0][0].tostring() + " and w2_code_bdzx=@w2_code_bdzx and w2_code_type=0 and w2_code_date2 is null and w2_code_sj=0)", param);

資料庫增量資料的幾種方式

a.觸發器 在要抽取的表上建立需要的觸發器,一般要建立插入 修改 刪除三個觸發器,每當源表中的資料發生變化,就被相應的觸發器將變化的資料寫入乙個臨時表,抽取執行緒從臨時表中抽取資料,臨時表中抽取過的資料被標記或刪除。觸發器方式的優點是資料抽取的效能較高,缺點是要求業務表建立觸發器,對業務系統有一定的...

取得剛插入的資料ID

class.forname com.mysql.jdbc.driver string url jdbc mysql localhost bbs?user root password root connection conn drivermanager.getconnection url conn.s...

MySQL 得到資料庫的大小

1.命令列方式 前言介紹 要知道乙個表占用空間的大小,那就相當於是 資料大小 索引大小 即可。show databases 檢視有多少 database,也叫做table schema 有點串用 1.1 檢視單個database 或是table schema 占用的大小 select sum dat...