Solved

Using While in Stored procedure

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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
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.

635 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