SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 I have a steps counter with increment 1 after
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cplusplus
Constraint Violating Yak Guru

481 Posts

Posted - 05/24/2012 :  15:10:43  Show Profile  Reply with Quote
In the following sp, it builds row id's which are steps increment of 1.
upto 10 it is good, after 10 instead of 11, it is again showing 10.
here is my scritp within SP. which i use:Cast is the one it is causing issue.

	CREATE TABLE #Steps(Step varchar(20) null)

	DECLARE @i Int, @Max Int

	SET @i = 1

	IF @Edit = 0 
		INSERT INTO #Steps VALUES(null)

	IF EXISTS(SELECT 1 FROM dbo.Tab_WorkflowActivity WHERE ModuleRecordID = @ModId And ModuleName = @ModuleName)
		BEGIN
			INSERT INTO #Steps SELECT DISTINCT Step FROM dbo.Tab_WorkflowActivity WHERE ModuleRecordID = @ModId And ModuleName = @ModuleName;
			SELECT @Max = CAST(MAx(Step) As Int) FROM #Steps;
			INSERT INTO #Steps VALUES(@Max + 1);
		END
	ELSE
		BEGIN
			INSERT INTO #Steps VALUES('1');
		END

	SELECT * from #Steps




Here are the results, it shows 10 after 10 instead of 11.
Step
-----
NULL
1
2
3
4
5
6
7
8
9
10
10 it should be 11.

Can you please tell me what am i doing wrong.

Thanks a lot for the helpful info.

yosiasz
Flowing Fount of Yak Knowledge

USA
1608 Posts

Posted - 05/24/2012 :  15:44:07  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
9 varchar > 10 varchar hence you will always be adding 1 + 9 = 10

select * From #Steps order by Step asc


NULL
1
10
2
3
4
5
6
7
8
9


<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion

Edited by - yosiasz on 05/24/2012 15:44:21
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 05/24/2012 :  15:48:15  Show Profile  Reply with Quote
why do you need to generate this manually? why not make it an identity field to generate values automatically? or if sequence has to be based on some other column value, use ROW_NUMBER() function

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cplusplus
Constraint Violating Yak Guru

481 Posts

Posted - 05/24/2012 :  17:13:09  Show Profile  Reply with Quote
Visakh,

I am populating teh result to a dropdownlist box on teh front end.

if the max number is 9 then fill dropdown upto 10 numbers.

if teh highest id in the table is 10, then fill the results to 11.

starting from NULL, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...

Because i don't want to provide more than 1 increment to the users that way the users willl select only the nu,bers which are in dropdown list box.

Regards.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 05/25/2012 :  10:09:47  Show Profile  Reply with Quote
thats ok. but why cant you use query based on ROW_NUMBER() to generate this and populate dropdown?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000