Solved

Using While in Stored procedure

Posted on 2004-08-23
4
288 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL clustering failover 8 39
sql update 2 35
How can I find this data? 3 23
access to sql migration 5 19
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Viewers will learn how the fundamental information of how to create a table.

685 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