473,382 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Not getting require result from Store Procedure

6
I created an SP to query a temp table for a list of part numbers. The part number list would be used in a WHILE loop to return a record set of each part number row.
The part number list has 2 part numbers. The last part number is the only part number being returned.

Any help would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE [dbo].[sp_Ample_TEST] AS
  2.  
  3.     BEGIN
  4.     CREATE TABLE #PartNoList
  5.         (
  6.         partno varchar(25) NULL        
  7.         )
  8.  
  9.     INSERT INTO #PartNoList (partno)
  10.  
  11.     SELECT DISTINCT partno
  12.     FROM PhysicalInv
  13.     END
  14.  
  15. DECLARE @partno varchar(25)
  16. SET @partno = NULL
  17.  
  18. SELECT DISTINCT @partno = [partno] FROM #PartNoList ORDER BY PartNo
  19.  
  20. WHILE (SELECT DISTINCT @@ROWCOUNT FROM #PartNoList) > 0
  21.     BEGIN
  22.  
  23.     BEGIN TRAN
  24.  
  25.     CREATE TABLE #ScanData
  26.         (
  27.         partno varchar(25) NULL,
  28.         OHlot varchar(20) NULL,
  29.         OHqty numeric(18,4) default 0,
  30.         PHlot  varchar(20) NULL,
  31.         PHonhand numeric(18,4) default 0        
  32.         )
  33.  
  34.     INSERT INTO #ScanData (partno, OHlot, OHqty, PHlot, PHonhand)
  35.  
  36.     SELECT coalesce(dbo.InvOnHand.partno, dbo.PhysicalInv.partno) as Partno, dbo.InvOnHand.lot as OHlot, dbo.InvOnHand.onhand as OHqty, 
  37.     dbo.physicalInv.LOT as PHlot, dbo.PhysicalInv.onhand as PHonhand
  38.  
  39.     FROM InvOnHand FULL OUTER JOIN  PhysicalInv ON dbo.PhysicalInv.lot = dbo.InvOnHand.lot
  40.  
  41.     WHERE @partno = dbo.PhysicalInv.partno OR @partno = dbo.InvOnHand.partno
  42.  
  43.     DELETE FROM #PartNoList WHERE partno = @partno
  44.  
  45.     COMMIT TRAN
  46.  
  47.     SET @partno = NULL
  48.  
  49.     SELECT DISTINCT @partno = [partno] FROM #PartNoList ORDER BY PartNo    
  50.  
  51.     SELECT partno, OHlot, OHqty, PHlot, PHonhand
  52.  
  53.     FROM #ScanData
  54. END
  55. GO
  56.  
  57.  
  58.  
Jan 16 '09 #1
2 1229
ck9663
2,878 Expert 2GB
This is your problem.

Even if there are two or more records on your #partnolist table, it will always return the last row and store it on your variable.

SELECT DISTINCT @partno = [partno] FROM #PartNoList ORDER BY PartNo
I did not look at your entire code. But I would assume there will be error in the way you do it. You placed the CREATE TABLE inside the WHILE LOOP. If the code inside the loop run twice, you'll have an error since the temp table is already existing. Try to reconsider another technique.

Also about the assignment of values to variable, read more about ithere.

Happy coding!

-- CK
Jan 16 '09 #2
Ample
6
Thanks CK for the tips! I moved the temp table out of the While loop and added
Top 1 to my select statement (SELECT DISTINCT Top 1 @partno = [partno] FROM #PartNoList ORDER BY PartNo). I also moved the last select statement out of the BEGIN...END block (SELECT partno, OHlot, OHqty, PHlot, PHonhand
FROM #ScanData) to achieve the desired result.

Regards
Jan 17 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Martin Raychev | last post by:
Hi all, I have the following problem: I have a private method that returns a SqlDataReader. For this to work I have not to close the DB connection in the above method. I do this only to
2
by: Patrick Olurotimi Ige | last post by:
When i run the code below with stored proc :- I get only the first table results :-"templates" even if i fill the dataset with another table for example category,pages etc.. Any ideas? ...
3
by: Peter | last post by:
Hello all, I have the following t-sql batch: create procedure stp_test ( @p_date1 as datetime = null, @p_date2 as datetime = null )
8
by: gacuna | last post by:
i want to insert into a temporal table the result of a store procedure. on sql server the sentence would look like this (already working) INSERT INTO #SHIPINFO exec TESTDTA.S59RSH05 @SCBILLTO,...
9
by: serge | last post by:
/* Subject: How to build a procedure that returns different numbers of columns as a result based on a parameter. You can copy/paste this whole post in SQL Query Analyzer or Management Studio...
4
by: meendar | last post by:
Hi to all, I just need to get two fields from a table and manipulate the results in next query of a procedure.I planned to code like what you see below, create procedure marks1 as @ sql1 as...
1
by: Dayzz | last post by:
Hi Everyone I have a code in VB that calls a Dynamic store procedure in MySQL DB, when I execute this store directly from the SQL Manager 2007 for MySQL it works very well, but when I called from...
5
by: bmm | last post by:
I have a Store Procedure on a Sql Server 2000 Where I use the Table Hint "NoLock" on all selects. One of my clients (OleDbConnection from C#) doesn't get the same Result Set as the others. The...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.