Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 January 12th, 2006, 08:45 PM
Authorized User
 
Join Date: Jun 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to insert row no automatically

Hi,

i have a one table as this:


col1 col2

A 1000.00
A 3000.00
B 250.00
B 5000.00
B 1500.00
C 4000.00
C 2500.00
C 100.00
C 800.00
D 3500.00
D 8000.00


i need add one more column acccording to col2 in descending order as this:

col1 col2 seq_no
A 1000.00 2
A 3000.00 1
B 250.00 3
B 5000.00 1
B 1500.00 2
C 4000.00 1
C 2500.00 2
C 100.00 4
C 800.00 3
D 3500.00 2
D 8000.00 1



may i know what the SQL command if i need a seq_no as above?




Thanks
 
Old January 13th, 2006, 12:20 PM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 146
Thanks: 0
Thanked 1 Time in 1 Post
Default

-- In SQL 2000 (and earlier) you need to use a correlated SubQuery
--Setup Test Data
CREATE TABLE dbo.OrderDetail
(OrderID int
,ProdID int
,Qty DECIMAL(12,4)
,PriceUtilized MONEY
,LineNum int);

INSERT dbo.OrderDetail (OrderID, ProdID, Qty,PriceUtilized)
VALUES (1,501,30,25.99);
INSERT dbo.OrderDetail (OrderID, ProdID, Qty,PriceUtilized)
VALUES (1,502,300,2.99);
INSERT dbo.OrderDetail (OrderID, ProdID, Qty,PriceUtilized)
VALUES (1,503,301,249.99);
INSERT dbo.OrderDetail (OrderID, ProdID, Qty,PriceUtilized)
VALUES (2,501,3,29.99);
INSERT dbo.OrderDetail (OrderID, ProdID, Qty,PriceUtilized)
VALUES (2,502,40,25.99);
INSERT dbo.OrderDetail (OrderID, ProdID, Qty,PriceUtilized)
VALUES (3,601,7,204.99);
INSERT dbo.OrderDetail (OrderID, ProdID, Qty,PriceUtilized)
VALUES (3,501,2000,25.99);

-- Update Statements Use Correlated SubQuery
UPDATE dbo.OrderDetail SET LineNum = (SELECT COUNT(*)
    FROM dbo.OrderDetail ODI
    WHERE ODI.OrderID = ORDERDetail.OrderID -- Limits us to the set belonging to one order
    AND ODI.PRODID >= ORDERDetail.ProdID -- Enables the Count to mimic Row_Number and
                                -- determines the order of the Line Nums
    )

SELECT ORDERID, PRODID, Qty, PriceUtilized, LineNum
FROM dbo.OrderDetail
Order BY OrderID, LineNum

David Lundell
Principal Consultant and Trainer
www.mutuallybeneficial.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
insert row into table stealthdevil Access VBA 11 June 16th, 2007 08:32 AM
insert new row in datagrid sumith ASP.NET 1.0 and 1.1 Professional 1 February 15th, 2007 01:53 PM
Insert Row in Database monika.vasvani ASP.NET 1.0 and 1.1 Professional 1 November 18th, 2006 05:17 AM
automatically update or insert record Jittermg Dreamweaver (all versions) 0 July 7th, 2005 03:55 PM
add row no automatically Jane SQL Language 9 June 28th, 2004 02:11 AM





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