-1


I am trying automatically increment the alpha numeric field (say for productid in product table).

But I am getting an error (see below). Could somebody please look into this error or any other approach to achieve this task?

My table details:

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

I am gettin the following error:

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.**

Peter Lang
  • 54,264
  • 27
  • 148
  • 161
sunit
  • 1
  • 1
  • 3
  • Is this meant to be a serious appeal for help? The "create table" statement is one long line and thus unreadable, same with the error message. Also, no information about the used database and database version is provided. – vog Oct 31 '10 at 15:10
  • My first problem is not able to format the question readable format..database version is SQL Server 2005 – sunit Oct 31 '10 at 15:12
  • I tried to reformat the question and add your database version as tag. – Peter Lang Oct 31 '10 at 15:13
  • Two answers for alphanumeric/automumber you may want to consider: http://stackoverflow.com/questions/2177584/sqlserver-identity-column-with-text/2177737#2177737 and http://stackoverflow.com/questions/2655392/is-there-a-tsql-alphanumeric-like-identity-column-type/2655410#2655410 – gbn Oct 31 '10 at 17:57

3 Answers3

0

Your line

SELECT @NId = cast(substring(id, 3, len(id)) as int)
  FROM tblProduct
  group by left(id, 2)
  order by id

is not doing what you were wanting it to. It is failing because you can't include id in the select directly, because you're grouping by left(id, 2), not id itself. You cannot put something into the select statement when grouping, unless it is part of the Group By, or an aggregate (such as SUM and MAX.)

(Edit: Corrected, Left and Substring are not 0 based--to get the PRD tag and such, we need substring 4, and left 3.)

The correct way of doing this would be:

SELECT @NId = cast(substring(MAX(id), 4, len(MAX(id))) as int) 
  FROM tblProduct
  group by left(id, 3)
  order by left(id, 3)
Brisbe
  • 1,588
  • 2
  • 20
  • 42
  • Hi Mr Brisbe, could u pls reformat below line in correct way because i am not getting what u r saying... – sunit Oct 31 '10 at 15:17
  • SELECT @NId = cast(substring(id, 3, len(id)) as int) FROM tblProduct group by left(id, 2) order by id – sunit Oct 31 '10 at 15:18
  • a reference in the group by statement is not required to be in the select statement in query – JeffO Oct 31 '10 at 15:19
  • Are you saying this is incorrect (Northwind db): Select LastName from dbo.Employees Group By Title, LastName; because Title is not in the select items nor is there any aggregate function. – JeffO Oct 31 '10 at 15:28
  • @Jeff O: No, because LastName is in the group by, it can be part of the select. I'm saying, on the other hand, that this would be incorrect: SELECT title FROM dbo.Employees GROUP BY LEFT(Title, 2). – Brisbe Oct 31 '10 at 15:32
  • Msg 245, Level 16, State 1, Procedure spInsertInProduct, Line 12 Conversion failed when converting the varchar value 'D00' to data type int. – sunit Oct 31 '10 at 15:47
  • @sunit: Sorry about that, I forgot that substring and left are both 1-based, not 0-based, and corrected that part as well, to strip out the 3 character PRD in both. – Brisbe Oct 31 '10 at 15:58
  • Hi Mr Brisbe, in your latest query its working but after inserting 10 values its incrimenting any more. for example Once its reaching PRD0010...its not incrementing any more again and again taking the same value PRD0010..its should go like....PRD0011...PRD0012....etc – sunit Oct 31 '10 at 16:56
  • @Mr Brisbe: in your latest query its working but after inserting 10 values its incrimenting any more. for example Once its reaching PRD0010...its not incrementing any more again and again taking the same value PRD0010..its should go like....PRD0011...PRD0012....etc – sunit Oct 31 '10 at 17:05
  • @Mr Brisbe: in your latest query its working but after inserting 10 values its not incrimenting any more. for example Once its reaching PRD0010...its not incrementing any more again and again taking the same value PRD0010..its should go like....PRD0011...PRD0012....etc – sunit Oct 31 '10 at 17:23
0
 SELECT @NId = max(
                    cast(substring(id, 4, len(id)) as int)
                  ) 
FROM tblProduct;

This assumes your substring function is returning the numeric portion of your id. I made changes since in other examples your id's start with PRD.

Side note: There is no reason to have you Product ID's start with PRD in the database. If this were an identity field, you could set it to increment by 1 and in any display just have: 'PRD' + Cast(ID as varchar25)) as ProductID. Maybe it is not that simple of all ID's do not start with the same three letters.

JeffO
  • 7,957
  • 3
  • 44
  • 53
  • Hi Mr Jeff, now i am getting this conversation error: Msg 245, Level 16, State 1, Procedure spInsertInProduct, Line 12 Conversion failed when converting the varchar value 'D00' to data type int. – sunit Oct 31 '10 at 15:50
  • Please give an example of ID and we can help you with the substring. I'm guessing you shouldn't be starting at the 3 character in some instances. – JeffO Oct 31 '10 at 17:38
0

I have tested your stored procedure in SQL Server 2000 and Query Analyzer it works very well. Just I have removed create table code from that.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mahesh
  • 1,370
  • 9
  • 36
  • 61