2011-09-26 13 views
1
更多然後一排

我在我的數據庫中的表中給出像波紋管如何使一個普通的ID爲

Requestid(primary key,identity) studentid reqid 
    1        1   bc1 
    2        1   bc1 
    3        2   bc2 

我要生成學生1相同的請求ID,如果他正在更多然後一個請求。 我正在使用SQL Server 2005和請求的ID是身份和學生ID會來時,我提交我的表單,但我想生成reqid as automaticaly。同一個id的學生也是一樣的,下一個學生提交的時候應該改變新的id。

Plz幫我解決它。在此先感謝

+0

@的Mikael感謝 –

+0

如果我的回答是,你需要,你應該考慮接受的答案是什麼。 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –

回答

0

如果我理解你願意,你可以使用一個計算列生成reqid什麼。

create table StudentRequest 
(
    Requestid int identity primary key, 
    studentid int not null, 
    reqid as 'bc'+cast(studentid as varchar(10)) 
) 

測試:

insert into StudentRequest (studentid) values (1) 
insert into StudentRequest (studentid) values (1) 
insert into StudentRequest (studentid) values (2) 

select * 
from StudentRequest 

結果:

Requestid studentid reqid 
----------- ----------- ------------ 
1   1   bc1 
2   1   bc1 
3   2   bc2