2014-02-03 60 views
-2

我有三個表從一個表中檢索值到另一個

  • tab_contactlist(CONTACT_ID(身份(1,1)NOT NULL),姓名,地址,PHONENO,EMAILADDRESS)
  • tab_group(GROUP_ID(身份(1,1)不爲空),組名)
  • tab_information(CONTACT_ID,GROUP_ID)

我的問題是在插入值插入tab_contactlist一個d tab_group那麼如何存儲兩個表(tab_contactlist, tab_group)ID爲tab_contactlist其中contact_idgroup_idtab_contactlist ..

任何人都可以對此有任何想法?請幫我..

回答

0
declare @contact_id_var int 
declare @group_id_var int 

insert into tab_contactlist (....) values (....) 
select @contact_id_var = @@IDENTITY 

insert into tab_groupvalues (....) values (....) 
select @group_id_var = @@IDENTITY 

insert into tab_information(contact_id,group_id) values (@contact_id_var ,@group_id_var) 
+0

也可以把所有的交易,以確保所有插入成功 – user3036688

+0

先生......我已經插入值到tab_contactlist和tab_groupvalues我想提取CONTACT_ID和GROUP_ID成tab_information – shanpal

+0

http://technet.microsoft.com/en-us/library/aa933196(v=sql.80).aspx檢查此 請選擇@contact_id_var = @@ IDENTITY !!!! – user3036688

1
create table #tab_contactlist(
contact_id int identity(1,1) not null 
,name varchar (100) 
,address varchar (100) 
,phoneno varchar (100) 
,emailaddress varchar (100) 
) 

create table #tab_group(
group_id int identity(1,1) not null 
,group_name varchar (100) 
) 

create table #tab_information(
contact_id int 
,group_id int 
) 

declare @contact_id_var int 
declare @group_id_var int 

insert into #tab_contactlist (name,address,phoneno,emailaddress) 
    values ('name1','address1','phoneno1','emailaddress1') 
select @contact_id_var = SCOPE_IDENTITY() 

insert into #tab_group (group_name) values ('group_name1') 
select @group_id_var = SCOPE_IDENTITY() 

insert into #tab_information(contact_id,group_id) values (@contact_id_var ,@group_id_var) 
+0

謝謝先生你的偉大回應..;) – shanpal

相關問題