Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL to update table with "item numbers"

Posted on 2011-09-13
3
Medium Priority
?
262 Views
Last Modified: 2012-05-12
I am trying to duplicate some functionality I had in an Access app in a SQL Server environment via asp.net web forms.

My problem is with numbering items that are assigned to a Purchase Order.  After creating a new Purchase Order, users are directed to a screen where they can add items to their "cart".  The cart is based on a temporary table created via stored procedure with a parameter for the table name, which is the username plus date plus "TempItems".  

When they click a "submit" button, I want to sort the items they've added in the temp table and populate an "itemnumber" field with a sequential number - for each record, it's the count of items with itemdescriptions that are <= each record's itemdescription, if that makes any sense.

I used DCount in my Access app, and the Append Query I ran had a field with alias "Sequence" which was:  Sequence: Dcount("itemid", "tempitems", "itemdescription <=itemdescription).  This method worked very well.  

How do I do it in SQL Server??
0
Comment
Question by:Alastair_White
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 36533165
Hi. In the tags you have SQL 2005, is that the correct version you are using? If so, introduced in SQL 2005 is windows function ROW_NUMBER() which combined with OVER() analytical clause will do what you need.

Some thing like:
ROW_NUMBER OVER(ORDER BY ItemDescription) AS "Sequence"

Open in new window


0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36533173
For some more information on it, especially since you did this in Access, please see my article which compares the ranking options between SQL and Access; therefore, it should be a worthwhile read.
http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/A_1555-Analytical-SQL-Where-do-you-rank.html
0
 

Author Closing Comment

by:Alastair_White
ID: 36537005
Yes, I am using SQL Server 2005, and I have now used the row_number() / over() combo to get my "item numbers" as desired.  

Thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question