儲存資料同時查詢儲存資料記錄的ID,資料id

來源:互聯網
上載者:User

儲存資料同時查詢儲存資料記錄的ID,資料id

儲存資料時同時取出該資料的記錄ID

insert into Table(a,b) values(1,2) select @@identity


access資料庫怎才可以儲存後立即就返回當前儲存產生的id,我弄的頭都大了,有沒有人幫幫忙

首先用一樓的語句查詢出最大的那個ID號,然後你增加記錄成功後,那麼當前增加的那條資料產生的ID號就等於查詢出來的那個最大的ID號+1,因為你的ID是自動編號,+1即可。最後你返回那個ID+1的值即可,就是你當前儲存的ID號了。
看了你的追問,如果這句語句報錯,那麼就換一種寫法。
select top 1 * FROM 表明 order by id desc
這個是獲得表中最大的ID,然後你添加成功後,將獲得的這個ID號+1返回即可。
 
用SQL Server 2000實現從一個表篩選所要的資料記錄儲存到另個表

同一個資料庫:
insert into b(name0,address0)
select name,address from a where name='某某'
不同資料庫(假設資料庫名了db):
insert into b(name0,address0)
select name,address from db..a where name='某某'
在不同的伺服器上,可以先執行sp_addlinkedserver預存程序添加一個連結的伺服器:
EXEC master.dbo.sp_addlinkedserver @server = N'link',
@srvproduct=N'SQLNCLI',
@provider=N'SQLNCLI',
@datasrc=N'IP地址'
然後加連結的伺服器映射登入名稱:
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'link',@useself=N'False', @locallogin=N'本地登入名稱',@rmtuser=N'要串連伺服器登入名稱',@rmtpassword='登入密碼'
然後就要以插入了:
insert into b(name0,address0)
select name,address from link.db..a where name='某某'
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.