Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using While in Stored procedure

Posted on 2004-08-23
4
Medium Priority
?
309 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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 the fundamental information of how to create a table.

824 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