Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using While in Stored procedure

Posted on 2004-08-23
4
Medium Priority
?
305 Views
Last Modified: 2008-02-01
I am trying to import a text file to an SQL table.
I would like to check if the text file exists, if it exists, do the import, exit, if not, wait for 5 seconds, check again, and I want to loop through this process 5 times.
Here is my code, but it seems not working. Please help. Thanks

SET @TextFile = 'C:\ClaimsInquiry\Inbox\'+@ProviderNumber+'.Login.txt'
WHILE @t = 6
      BEGIN
      exec master..xp_fileexist @TextFile, @i out
      IF @i=1
      BEGIN
      SET @WriteToDB = "BULK INSERT Inbox_Login  FROM @TextFile  WITH (FIELDTERMINATOR = ';') "
                         EXEC (@WriteToDB)


                        BREAK

                  END
            ELSE
            PRINT 'NOTHING'

            WAITFOR DELAY '00:00:03'

            CONTINUE
      END
0
Comment
Question by:CochiseCounty
[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
4 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 11873526
1. I presume you left out some of your code. This doesn't declare your variables or set values for @t or @i. Can you post the rest?
2. Have you tried executing your @WriteToDB string in Query Analyzer?
3. What error messages are you getting?
4. Is 'NOTHING' getting printed?
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 1000 total points
ID: 11873541
CochiseCounty:

> Here is my code, but it seems not working. Please help. Thanks

I made some changes and commented them below.  Try this:
    SET @TextFile = 'C:\ClaimsInquiry\Inbox\'+@ProviderNumber+'.Login.txt'
    SET @t = 1
    SET @i = 0

    -- Loop 5 times, or until the file is found
    WHILE @t < 5 AND @i <> 1
    BEGIN
        -- Check to see if the file exists
        exec master..xp_fileexist @TextFile, @i out

        -- If the file isn't found, then wait
        IF @i <> 1
        BEGIN
                WAITFOR DELAY '00:00:03'
                SET @t = @t + 1
        END
    END

    -- If the file was found
    IF @i = 1    
        BEGIN
            -- Do the import
            SET @WriteToDB = "BULK INSERT Inbox_Login  FROM @TextFile  WITH (FIELDTERMINATOR = ';') "
            EXEC (@WriteToDB)
        END
    ELSE
        -- Print the error
        PRINT 'NOTHING'

Hope That Helps,
Dex*
0
 

Author Comment

by:CochiseCounty
ID: 11873827
It works perfect. Thanks so much!
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 11874018
CochiseCounty:

> It works perfect. Thanks so much!

You're welcome.  I think the main problem was that you weren't incrementing @t so that it eventually gets to 5 and stops executing.

-D*
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

715 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