Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 13th, 2004, 07:27 PM
Authorized User
 
Join Date: Jun 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default add row no automatically

hi,

how am i add a row no. on my table to the record automatically using a SQL command?

eg

table

row# cust no value
1 a0010 30000
2 a0001 15000
3 b0002 2000


 
Old June 13th, 2004, 11:07 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
Default

If you are using SQL, then you can set this field as identity. By setting this as identity, new Id will be generated automatically, when new row is created.

Om Prakash
 
Old June 23rd, 2004, 06:57 PM
Authorized User
 
Join Date: Jun 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

WHAT R THE COMMAND USING SQL TO ADDING ROW?
 
Old June 23rd, 2004, 07:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're not just after:
Code:
INSERT INTO TableName (IntegerField, TextField) VALUES (1, 'Text')
are you?

I am a loud man with a very large hat. This means I am in charge
 
Old June 24th, 2004, 07:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Jane,

You can use this, if that ROWNUMBER column is not set as Identity.

Code:
Declare @newRowNo int
select @newRowNo = coalesce(max(RowNo)+1,1) from YOURTABLENAME
insert YOURTABLENAME values(@newRowNo, 'a0010', 30000)
Everytime you run this
select coalesce(max(RowNo)+1,1) from YOURTABLENAME
You get the new incremented row number which you can use for next INSERT.

Hope that helps.
Cheers!

_________________________
-Vijay G
Strive for Perfection
 
Old June 24th, 2004, 07:42 PM
Authorized User
 
Join Date: Jun 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes, i can get it where i use 'INSERT' COMMAND BUT FROM MY SCENARIO, I ALREADY HAVE A DATA IN MY TABLE, JUST THAT WANT TO ADD THE ROW NO INTO IT?
 
Old June 25th, 2004, 05:02 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Still not sure if you are using ACCESS or SQL SERVER. Let me know so that I can give you the proper approach to put values into rownum column.

_________________________
-Vijay G
Strive for Perfection
 
Old June 27th, 2004, 02:40 AM
Authorized User
 
Join Date: Jun 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i using SQL Server,
and my table as this

rowno item_no value
null a0100 10000
null a0101 30000
null b0100 20000



i need to add a row no according to the value ascending.
 
Old June 27th, 2004, 01:58 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Is this something you are writing into a program or are you just modifying data?
 
Old June 28th, 2004, 02:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Jane,

Is this what you are looking for?

Code:
Declare @ctr int
Set @ctr=1
Declare @Itemno varchar(10)

Declare Update_Rownum cursor For
Select Item_no from <yourTable> order by Item_no

Open Update_Rownum
Fetch next from Update_Rownum into @Itemno
while @@Fetch_Status=0 
begin
     Update <yourTable> set rowno=@ctr where Item_no = @Itemno
     Set @ctr=@ctr+1
    Fetch next from Update_Rownum into @Itemno
end
Close Update_Rownum
Deallocate Update_Rownum
Hope that helps.
Cheers!

_________________________
-Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Add Primery Key value Automatically kamran Yousaf Bk C# 1 October 6th, 2007 04:39 PM
Add lines in php page automatically saifi4u PHP How-To 1 December 19th, 2006 04:42 AM
Automatically add querystring paramaters to URL trancehead ASP.NET 2.0 Professional 0 July 24th, 2006 03:03 AM
How to insert row no automatically Jane SQL Server 2000 1 January 13th, 2006 12:20 PM
change order of an array automatically after add SauSaigon Beginning PHP 7 February 22nd, 2004 12:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.