我需要一个SQLserver代码,自动生成单据编码
时间: 2023-05-15 12:07:28 浏览: 108
当然,我可以为您提供一个SQL Server代码,用于自动生成单据编码。以下是示例代码:
CREATE PROCEDURE [dbo].[GenerateDocumentCode]
@DocumentType VARCHAR(50),
@DocumentCode VARCHAR(50) OUTPUT
AS
BEGIN
DECLARE @Prefix VARCHAR(10)
DECLARE @Suffix VARCHAR(10)
DECLARE @Counter INT
SET @Prefix = LEFT(@DocumentType, 3)
SET @Suffix = RIGHT(@DocumentType, 3)
SELECT @Counter = ISNULL(MAX(CAST(RIGHT(DocumentCode, 6) AS INT)), 0) + 1
FROM dbo.Documents
WHERE LEFT(DocumentCode, 3) = @Prefix AND RIGHT(DocumentCode, 3) = @Suffix
SET @DocumentCode = @Prefix + RIGHT('000000' + CAST(@Counter AS VARCHAR(6)), 6) + @Suffix
END
请注意,此代码仅供参考,您需要根据您的具体需求进行修改。
阅读全文