iT邦幫忙

1

MS SQL Auto Increment的欄位可否依據某欄位做遞增值?

  • 分享至 

  • xImage

各位前輩好
我知道SQL可以設定某個欄位為AUTO INCREMENT(通常也會一併設定PK),但這個欄位可否能依據同Table內某個欄位做AUTO INCREMENT呢? 就不設定該欄位為PK了
主要依據的欄位為SN!
圖示說明如下:
https://ithelp.ithome.com.tw/upload/images/20181115/20107387kMZFgILP3y.png

謝謝~~~

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
純真的人
iT邦大師 1 級 ‧ 2018-11-15 17:17:59
最佳解答

可以用查詢的方式去新增..

declare @Test table(
	Test_ID int
	,Test_Txt nvarchar(5)
	,Test_Count int
)

insert into @Test
select Set_ID
,Set_Txt
,isNull((select count(*) from @Test where Set_ID >= Test_ID and Set_Txt = Test_Txt),0)+1 as Set_Count
from (
	select isNull((select max(Test_ID) from @Test),0) + 1 as Set_ID
	,'A' as Set_Txt
) as k

insert into @Test
select Set_ID
,Set_Txt
,isNull((select count(*) from @Test where Set_ID >= Test_ID and Set_Txt = Test_Txt),0)+1 as Set_Count
from (
	select isNull((select max(Test_ID) from @Test),0) + 1 as Set_ID
	,'B' as Set_Txt
) as k

insert into @Test
select Set_ID
,Set_Txt
,isNull((select count(*) from @Test where Set_ID >= Test_ID and Set_Txt = Test_Txt),0)+1 as Set_Count
from (
	select isNull((select max(Test_ID) from @Test),0) + 1 as Set_ID
	,'C' as Set_Txt
) as k

insert into @Test
select Set_ID
,Set_Txt
,isNull((select count(*) from @Test where Set_ID >= Test_ID and Set_Txt = Test_Txt),0)+1 as Set_Count
from (
	select isNull((select max(Test_ID) from @Test),0) + 1 as Set_ID
	,'B' as Set_Txt
) as k

insert into @Test
select Set_ID
,Set_Txt
,isNull((select count(*) from @Test where Set_ID >= Test_ID and Set_Txt = Test_Txt),0)+1 as Set_Count
from (
	select isNull((select max(Test_ID) from @Test),0) + 1 as Set_ID
	,'C' as Set_Txt
) as k

insert into @Test
select Set_ID
,Set_Txt
,isNull((select count(*) from @Test where Set_ID >= Test_ID and Set_Txt = Test_Txt),0)+1 as Set_Count
from (
	select isNull((select max(Test_ID) from @Test),0) + 1 as Set_ID
	,'C' as Set_Txt
) as k

select * from @Test

https://ithelp.ithome.com.tw/upload/images/20181115/20061369pWR4dz0wg9.png

rchin iT邦新手 5 級 ‧ 2018-11-16 08:48:22 檢舉

感謝,我會參考這個方法跟後端去做配合測試~~

1

一般來說是不太可能直接這樣用。
只能利用後端程式,來寫程式處理。或是sql的事件功能函式來幫你處理了。
當然用「純真的人」給你用的方式也是一種方法。

其實這方法只是利用了sql來寫程式處理。也算是一招就是了。

rchin iT邦新手 5 級 ‧ 2018-11-16 08:50:09 檢舉

瞭解! 本來以為資料庫若能直接這樣設定會省事很多

1
sam0407
iT邦大師 1 級 ‧ 2018-11-16 16:29:05

我的作法是這樣:
1.先建一個函數GetSNCount(假設Table Name是dbo.test)

CREATE FUNCTION [dbo].[GetSNCount](@SN varchar(10))  

RETURNS  int
AS
BEGIN
	Declare @SNCount int = 0
	SELECT @SNCount=count(SN) from dbo.test where SN=@SN group by SN

	set @SNCount = @SNCount + 1
	-- Return the result of the function
	RETURN @SNCount

END

2.Insert測試資料

insert test(sn,[count]) values('A',dbo.getSNCount('A'))
insert test(sn,[count]) values('B',dbo.getSNCount('B'))
insert test(sn,[count]) values('C',dbo.getSNCount('C'))
insert test(sn,[count]) values('B',dbo.getSNCount('B'))
insert test(sn,[count]) values('C',dbo.getSNCount('C'))
insert test(sn,[count]) values('C',dbo.getSNCount('C'))

3.結果如下
https://ithelp.ithome.com.tw/upload/images/20181116/20012665etYfkVfdrv.png

0
kennaanna
iT邦見習生 ‧ 2024-01-05 12:49:39

感謝您的詢問。 MS SQL Server 中如果不將該列設為主鍵,則無法直接基於另一列實作自增。 但是,您可以透過使用基於所需列遞增值的序列或觸發器來實現類似的功能。 至於rice purity test,它是一種有趣而輕鬆的方式來衡量一個人的純真或生活各個方面的經歷。 這是一個流行的網路測驗,涵蓋從個人行為到浪漫關係的廣泛主題。

0
john746
iT邦見習生 ‧ 2024-03-14 01:22:34

Mbwhats.com is your prime source for MBWhatsApp, the popular modded version of WhatsApp. Stay updated on the latest MBWhatsApp APK releases, guides, tips and news. mbwhats.comhttps://mbwhats.com/

0
03212345678
iT邦見習生 ‧ 2024-03-21 19:32:26

Download watch anime online. aniwatch.com.co is a fantastic anime streaming that allows users to watch their favorite anime shows on their mobile devices. With a vast collection of anime.
visit: http://aniwatch.com.co/

0
03236345688
iT邦見習生 ‧ 2024-03-22 16:03:10

ChatGPT is an advanced AI chatbot engineered by OpenAI. It’s crafted to excel in a myriad of tasks, ranging from resolving your queries to rendering intricate information.
visit: https://chatgptdownload.me/

0
03334455786
iT邦見習生 ‧ 2024-03-23 17:50:40

Enjoy the best doramas.
Doramasvip is the best free for all drama lovers: K-Drama, J-Drama, C-Drama, and many more.
Answer questions according to your level of knowledge in the Doramas in Spanish quiz.

visit: https://doramavip.com/

0
viritot776
iT邦見習生 ‧ 2024-05-06 12:50:00

可以考慮延期嗎?
duck life

0
john4755
iT邦見習生 ‧ 2024-05-11 04:17:05

IGram on Download Instagram videos easily with IGram. This free online tool lets you save Instagram video posts directly to your device. visit our website https://igram.bar/

0
john546
iT邦見習生 ‧ 2024-05-11 04:31:43

Pikashow APK offers HD streaming of entertainment. Includes TV shows and movies. Download the latest version for Android, PC or iOS. visit our website https://pikashowhd.net.in

0

This game provides an incredibly detailed driving environment, focusing on realistic vehicle behavior. In BeamNG.drive, players can experiment with different cars and terrains. The primary goal is to master the intricacies of driving and enjoy the realistic simulation of crashes and stunts.

0
kiminorris
iT邦見習生 ‧ 2024-08-14 23:28:58

Your mission in this game is to tick off as many boxes as you can. one million checkboxes offers a massive grid of checkboxes, and your objective is to mark as many as possible. The simplicity of the game hides a deeper challenge that requires concentration and patience.

我要發表回答

立即登入回答