Solved

Problem with T-SQL loop in a stored procedure

Posted on 2002-04-26
3
431 Views
Last Modified: 2008-02-26
Hi All,

This problem has really been bugging me and I hope someone can help. I have written a stored procedure for SQL Server 2000 in T-SQL that searches through codes in sequence until it finds an empty code and then inserts the new record.

I am using a WHILE @ IS NOT NULL loop but the code doesn't jump out of the loop when the variable is NULL

I will post the important bits of my code below.

CREATE PROCEDURE ShipItems (@OrderID as int) AS
DECLARE @OrderCode as nvarchar(14), @i as int, @ID2 as int

SELECT @OrderCode = Code FROM [Order] WHERE ID = @OrderID
SET @i = 1
SELECT @ID2 = ID FROM Shipment WHERE Code = @OrderCode + '-1'
WHILE @ID2 is not null
BEGIN
     SET @i = @i + 1
     SELECT @ID2 = ID FROM Shipment WHERE Code = @OrderCode + '-' + CAST(@i AS nvarchar(2))
END

INSERT INTO Shipment VALUES (@OrderCode + '-' + CAST(@i AS nvarchar(2)), NULL, 0, @OrderID, 1)
GO

Any help with this would be much appreciated as I don't understand why it isn't working 'cos I have used loops like this before without any problems.

TIA,
Ph0bia
0
Comment
Question by:ph0bia
[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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
ID: 6970821
Do this:

WHILE @ID2 is not null
BEGIN
    SET @i = @i + 1
    SET @ID2 = NULL
    SELECT @ID2 = ID FROM Shipment WHERE Code = @OrderCode + '-' + CAST(@i AS nvarchar(2))
END

The problem is that the select will not set @ID2 to NULL when no records are retrieved, but leave the value as is.

CHeers
0
 

Author Comment

by:ph0bia
ID: 6970833
Thanks a lot, I'll try that and let you know :-)
0
 

Author Comment

by:ph0bia
ID: 6970853
Thanks angelIII you're a star... it worked perfectly. I never knew that it did that.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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