Solved

Using While in Stored procedure

Posted on 2004-08-23
4
255 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
  • 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 250 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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now