我想自动增加字母数字字段(例如productid
在product
表中)。使用字母数字自动递增值
但我收到一个错误(见下文)。 有人可以看看这个错误或任何其他方法来完成这项任务?
我的表的详细信息:
create table tblProduct
(
id varchar(15)
)
create procedure spInsertInProduct
AS
Begin
DECLARE @PId VARCHAR(15)
DECLARE @NId INT
DECLARE @COUNTER INT
SET @PId = 'PRD00'
SET @COUNTER = 0
--This give u max numeric id from the alphanumeric id
SELECT @NId = cast(substring(id, 3, len(id)) as int) FROM tblProduct group by left(id, 2) order by id
--here u increse the vlaue to numeric id by 1
SET @NId = @NId + 1
--GENERATE ACTUAL APHANUMERIC ID HERE
SET @PId = @PId + cast(@NId AS VARCHAR)
INSERT INTO tblProduct(id)values (@PId)
END
我刚开了以下错误:
Msg 8120, Level 16, State 1, Procedure spInsertInProduct, Line 10 Column 'tblProduct.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. Msg 8120, Level 16, State 1, Procedure spInsertInProduct, Line 10 Column 'tblProduct.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.**
这是否意味着要求帮助? “创建表”语句是一个很长的行,因此无法读取,与错误消息相同。此外,没有提供关于使用的数据库和数据库版本的信息。 – vog 2010-10-31 15:10:03
我的第一个问题是无法格式化问题的可读格式。数据库版本是SQL Server 2005 – sunit 2010-10-31 15:12:00
我试图重新格式化问题并将您的数据库版本添加为标记。 – 2010-10-31 15:13:11