我想知道,每当我更改月份时,跟踪号码将从001开始。
例如,这是我的追踪号码:
CAB1108072 == CAB + 11 for year, 08 for month of august, 072 running number
我是否需要在当月的表格中添加其他列,以便每月更改时生成跟踪编号?
输出应该是这样的。
示例:
这是我的SQL:
ALTER PROCEDURE [dbo].[generateTrackNo] AS
Declare @tempYear VARCHAR(5)
Set @tempYear = Year(GetDate())
SELECT 'CAB' + SUBSTRING(CONVERT(VARCHAR(6),GETDATE(),112),3,4) + Right('0000000'+ Cast(CurrentNo as varchar(10)),3) FROM tblTrackNo where GenYear = @tempYear
UPDATE tblTrackNo SET CurrentNo = CurrentNo + 1 where GenYear = @tempYear
我的表tblTrackNo有两个列名,genYear Numeric(18,0)
和CurrentNo Numeric(18,0)
我是否需要在一个月内添加另一列?
答案 0 :(得分:1)
为什么使用Numeric
作为列数据类型?由于你正在构建一个字符串,我建议将它们存储为CHAR以节省转换:
genYear CHAR(2) CurrentNo INT CurrentMonth CHAR(2)
将CurrentNo保留为INT以便于递增。
下面的SP(注意,我没有测试过代码,因此可能需要调整一些内容)执行以下操作:
1. Sets up variables to hold the 2 digit year and 2 digit month (based on the current date) as CHAR(2). 2. Gets the record for the current year. 3. If there is no record for the current year, it generates the tracking number and inserts a new record (for the current year) into the table 3. If there is a record for the current year, but the month in the table is different than the current month, it generates the tracking number starting at 001. Otherwise, it generates the next tracking number. 3A. It then updates the table. 4. Finally, it returns the tracking number
PROCEDURE [dbo].[generateTrackNo] AS
BEGIN
DECLARE @curYear AS CHAR(2),
@curMonth AS CHAR(2)
DECLARE @dbYear AS CHAR(2),
@dbMonth AS CHAR(2),
@dbNumber AS CHAR(3),
@newNumber AS INT,
@trackingNumber AS CHAR(10)
-- Set up the values for the current year and current moth
SET @curYear = RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR), 2)
SET @curMonth = CAST(MONTH(GETDATE()) AS VARCHAR)
-- Use REPLICATE to pad with a leading 0, if needed
SET @curMonth = REPLICATE('0', 2 - DATALENGTH(@curMonth)) + @curMonth
-- Get the current values from the database for the current year
SELECT @dbYear = genYear,
@dbMonth = currentMonth,
@dbNumber = CAST(CurrentNumber AS VARCHAR)
FROM tblTrackNo
WHERE genYear = @curYear
IF (@dbYear IS NOT NULL)
-- There was a record for the current year
BEGIN
IF (@dbMonth != @currMonth)
-- We're in a new month
BEGIN
SET @newNumber = 2
SET @trackingNumber = 'CAB' + @curYear + @curMonth + '001'
END
ELSE
-- We're in the current month
BEGIN
-- Pad with apprpriate number of leading 0's
SET @dbNumber = REPLICATE('0', 3 - DATALENGTH(@dbNumber) + @dbNumber
SET @trackingNumber = 'CAB' + @curYearChar + @curMonthChar + @dbNumber
SET @newNumber = CAST(@dbNumber AS INT)
END
-- Update the table accordingly
UPDATE tblTrackNo
SET CurrentNo = @newNumber,
CurrentMonth = @curMonth
WHERE genYear = @curYear
END
ELSE
-- We don't have a record in the table for the current year
BEGIN
-- Create the tracking number
SET @trackingNumber = 'CAB' + @curYear + @curMonth + '001'
-- Insert a new record into the table
INSERT INTO tblTrackNo(
genYear,
CurrentNo,
CurrentMonth
)
SELECT @curYear,
2,
@curMonth
END
SELECT @trackingNumber AS trackingNumber
END
这很可能不是最有效的SP,但它会完成这项工作。您还可以考虑将逻辑移出数据库,让应用程序根据表中返回的数据生成跟踪号,然后应用程序可以根据需要更新(或插入)。