[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

FILTERED QUERY (2 TABLES)

Asked by pjordanna in MS SQL Server

Tags: adjno, insert, select

Hi Experts,

I have a table called "sp_orders" which holds sales information and is set up like this:

column1 - name = "sp_orderNo", value = "numberic (with identinty)"
column2 - name = "salesInfo", value = "varchar (50)"

when a sale is made the sp_orders table is updated with the sales data and an order number is automatically assisgned.

If the sales data needs to be altered at a later date then the order is copied from the sp_orders table, adjusted, and then written to another table called "sp_adjustments" which looks like this:

column1 - name = "sp_orderNo", value = "varchar (50)"
column2 - name = "adjustedOrderNoSuffix", value = "varchar (20)"
column3 - name = "adjustedSalesInfo", value = "varchar (50)"

the sp_orderNo" column holds the order number and can be used to join the table to the "orders" table. The "adjustedOrderNoSuffix" table holds the order number suffix - i.e. "1" for the first amendment, "2" if it's amended a second time etc.

My question is this:

I need to create a recordset to return all the bookings from the "orders" table with a join to the "adjustments" table on "orderNo = adjustedOrderNo".

If an order from the orders table has been adjusted then I want the LATEST adjustment from the adjustments table to be returned and not the original order from the orders table.




The SQL code I have so far for this is as follows:



set nocount on

create table sp_ordersTemp (tempOrderNo varchar(500), tempAdjustedOrderNo Varchar(50), tempSalesInfo varchar(50))

Insert into  sp_ordersTemp (tempOrderNo) select distinct sp_orderNo from sp_orders



declare @orderID varchar(500)
declare @salesInfo varchar(500)
declare @adjNo varchar(500)
declare recCnt integer


DECLARE Htree_cursor CURSOR FOR select tempOrderNo from sp_ordersTemp

  OPEN Htree_cursor
     FETCH NEXT FROM Htree_cursor INTO @orderId
          WHILE @@FETCH_STATUS = 0
          BEGIN
               
          select count(*) into recCnt from sp_adjustments where sp_orderNo = @orderId
          if recCnt>0
          begin
           select @adjno = Max(adjustedOrderNoSuffix), @Salesinfo= adjustedSalesInfo from sp_adjustments where orderNo = @orderId
          end
          else
          begin
           set @adjno=0
           select @Salesinfo= SalesInfo from sp_orders where sp_orderNo = @orderId
          end

      UPDATE sp_ordersTemp  SET tempAdjustedOrderNo = @adjno, tempSalesInfo =@salesinfo  where tempOrderNo = @OrderId

     
        FETCH NEXT FROM Htree_cursor INTO @orderId

      END

CLOSE Htree_cursor

DEALLOCATE Htree_cursor




Select * from  sp_ordersTemp order by  tempOrderNo


drop table sp_ordersTemp




This has come from EE and been modified by myself.


I am getting an error on the line "select count(*) into recCnt from sp_adjustments where sp_orderNo = @orderId "


"'integer' is not a recognized CURSOR option."


I don't understand enough about SQL to debug this effectively. Can any of you help? Also are there any additional bugs in this code?




Thanks,



PJORDANNA


 
Loading Advertisement...
 
[+][-]07/09/04 03:12 AM, ID: 11510103Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/09/04 03:23 AM, ID: 11510144Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/09/04 03:51 AM, ID: 11510271Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: MS SQL Server
Tags: adjno, insert, select
Sign Up Now!
Solution Provided By: mcmonap
Participating Experts: 1
Solution Grade: B
 
[+][-]07/09/04 06:13 AM, ID: 11511379Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81