¸ê®Æ¨Ó·½¡Ghttp://marchlifeshow.blogspot.com/
¼gµ{¦¡±`»Ýn¥Î¨ì¬y¤ô¸¹¡A¥i¥H¥Îµ{¦¡¼g¡A¤£¹L¤ñ¸û³Â·Ð
©³¤Uªº±Ð¾Ç¬O¦bSQL¤º¼g¤@Ө禡
¥H«án¥Î¨ì´N¥un©I¥s´N¥i¥H¤F~®¼¤è«Kªº~
n¨Ï¥Î MSSQL ªº¦Û°Ê½s¸¹Äæ¦ì, ¥u»Ýn²³æªº³]©wÃѧO³W®æ¤¤ Is Identity, ´N¥i¥H«Ü®e©öªº²£¥Í¤@Ӧ۰ʽs¸¹Äæ¦ì¡C¤£¹L¦³®ÉÔ³o¼Ëªº¦Û°Ê½s¸¹¨Ã¤£¯àº¡¨¬©Ò¦³ªº»Ý¨D¡C¨Ò¦p«È¤á¥i¯à·|n¨Dq³æ½s¸¹¬O¥[¤W¤é´Á¤§Ãþªº°µ¬°q³æªº¶}ÀY¡C¨Ò¦p 2008/11/4 ²Ä¤@µ§q³æªºq³æ½s¸¹n¬O 2008110400001, ²Ä¤Gµ§´Nn¬O 2008110400002, 11/5 ªº¤Sn« 1 ¶}©l½s°_ 2008110500001, 2008110500002...¡C
³o®ÉÔ§Q¥Î¦Û°Ê½s¸¹Äæ¦ì´N¤£°÷¨Ï¥Î¤F¡C
³o®ÉÔ¥i¥H§Q¥Î¨ç¼Æªº¤è¦¡¨Ó¸Ñ¨M³oÓ°ÝÃD¡C¤U±´Nª½±µ°µ¤@Ó½d¨Ò¡C
º¥ý§ÚÌ¥ý«Ø¥ß¤@Ó¸ê®Æªí Orders- CREATE TABLE [dbo].[Orders](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [OrderID] [nvarchar](20),
- [CustomerID] [int],
- [OrderDate] [smalldatetime],
- CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
- (
- [ID] ASC
- )
- )
½Æ»s¥N½X ¦A¨Ó, §ÚÌ¥ý«Ø¥ß¤@Ó¨ç¼Æ GetOrderID():- CREATE FUNCTION GetOrderID()
- RETURNS nvarchar(20)
- AS
- BEGIN
- DECLARE @OrderID nvarchar(20)
- DECLARE @DT nvarchar(20)
- SELECT @DT = convert(varchar(10),getdate(),112)
- SELECT @OrderID= @DT + right('0000' + ltrim(isnull(max(cast(right(OrderID, 5) as int)),0)+1), 5) from Orders where left(OrderID, 8) = @DT
- RETURN @OrderID
- END
- GO
½Æ»s¥N½X * ·s«Ø¥ßªº¨ç¼Æ, §A¥i¥H¦b¸ê®Æ®w¤Uªº¡u¥iµ{¦¡©Ê / ¨ç¼Æ / ¯Â¶qȲ[¼Æ¡v¤¤§ä¨ì
³Ì«á, ±N¸ê®Æªí»P¨ç¼Æ¾ã¦Xªº¤èªk¬O¶i¤J³]p¸ê®Æªí¡C¦b OrderID Äæ¦ìªº¡u¹w³]È©Îôµ²¡v¿é¤J dbo.GetOrderID()¡C¦¹®É¦b¸ê®Æªí¤¤¦A·s¼W¸ê®Æ®É, q³æ½s¸¹ OrderID Äæ¦ì, ´N·|²£¥Í§ÚÌ·Qnªº½s¸¹¤è¦¡¤F¡C
¸ê®Æ¨Ó·½¡Ghttp://marchlifeshow.blogspot.com/ |