How to output to a text file from a cursor query

  • I have a query from SS 2000 table, and need to output to a text file.  How to use DTS to get it work?

    declare @msg as varchar(50)

    declare @area as char(2)

    declare @store as char(7)

     

    -- Work variables....

    declare @lastmsg as varchar(50)

    declare @lastarea as char(2)

     

    declare csrMissing cursor for

      Select Message, Area, Store_num

        from micros.Portal_MissingSales_Tble

        order by Message, Area, Store_num

     

    open csrMissing

     

    fetch next from csrMissing into

       @msg

      ,@area

      ,@store

     

     

    while @@FETCH_STATUS = 0

      begin

        set @lastmsg = @msg

        set @lastarea = NULL

        print ' '

        print @Msg

        print ' '

     

        while @lastmsg = @msg and @@FETCH_STATUS = 0

          begin

     

            if @area = @lastarea

              begin

                print '     ' + @store

              end

            else

              begin

                print @area + '   ' + @store

                set @lastarea = @area

              end

     

            fetch next from csrMissing into

               @msg

              ,@area

              ,@store

     

          end

     

      end

     

    close csrMissing

    deallocate csrMissing

    Thank you.

     

  • create a temporary table (or a standard table that you can re-create each time the DTS runs), replace the print statements with inserts into that table, direct DTS to output to your text file destination.

    I hope this makes sense, if it doesn't hit me back and I'll try to elaborate.

    Jim

  • mmm.....I think I didn't make my question clear.

    I have a table which changes every 15 minutes.

    In DTS, I use "Microsoft OLEDB Provider for SQL Server" as the source and a "Text file (Destination)" as the destination. I tried to put the cursor query into the Transform data Task, but seems it won't work because there's no output column name in this query.

    I need help to find out how to output the cursor query into a text file.

     

    Thank you.

  • Try taking a look at sp_makewebtask in BOL.

     

    I have used the following example in an application that creates a small text file when data changes from a trigger.

    sp_makewebtask @outputfile = @out, @query = @sql, @templatefile  = @template, @colheaders = 1, @lastupdated = 0

  • Try this, it works for me.

    declare @filename varchar(200),

     @ole INT,

     @file INT,

     @fs INT,

     @archivo VARCHAR(100),

     @trenutniRed varchar(200)

    set @archivo='e:\apps\mssql7\backup\testtxt.txt'

    SELECT @trenutniRed = 'del '+ @archivo

    EXEC master..xp_cmdshell @trenutniRed, NO_OUTPUT

     

    EXECUTE @ole = sp_OACreate 'Scripting.FileSystemObject', @FS OUT

     

        EXECUTE @ole = sp_OAMethod @FS, 'OpenTextFile', @file OUT, @archivo, 8, 1

        DECLARE TxtKursor  INSENSITIVE SCROLL CURSOR

        FOR SELECT field1 + filed2 FROM ivavtas_temp

        FOR READ ONLY   

        OPEN TxtKursor

        FETCH NEXT FROM TxtKursor INTO @trenutniRed

        WHILE @@Fetch_Status = 0

     BEGIN

       EXECUTE @ole = sp_OAMethod @file, 'WriteLine', Null, @trenutniRed

       FETCH NEXT FROM  TxtKursor INTO @trenutniRed

            END

        CLOSE TxtKursor

        DEALLOCATE TxtKursor

        EXECUTE @ole = sp_OADestroy @file

        EXECUTE @ole = sp_OADestroy @FS

     

    You must be sa or has admin priv.

    bye, Eduardo

  • My task is to output the text from "MissingSales_Tble" which changes every 15 minutes based on how many stores didn't transfer in their sales file.

    There are 3 possibilities in MissingSales_Tble:
     

    1 or 2 areas has missing sales
    AreaMessageStore_Num
    CNMissing Sales as of 2/10/2004 11:05:00 ABK00522
    WINo Missing Stores      
    CSNo Missing Stores      
    all areas have missing sales
    AreaMessageStore_Num
    CNMissing Sales as of 2/10/2004 11:05:00 ABK00522
    WIMissing Sales as of 2/10/2004 11:05:00 ABK02164
    WIMissing Sales as of 2/10/2004 11:05:00 ABK04143
    CSMissing Sales as of 2/10/2004 11:05:00 ABK00213
    CSMissing Sales as of 2/10/2004 11:05:00 ABK01470
    no missing sales
    AreaMessageStore_Num
      No Missing Stores  

    I experienced problem when it's in #1 possiblity (1 or 2 areas has missing sales) and my code's output becomes
     
    Missing Sales as of 2/10/2004 11:05:00 A

    CN      BK00522

    No Missing Stores

    WI      No Missing Stores

    CS      No Missing Stores

     
    and it should be
     

    Missing Sales as of 2/10/2004 11:05:00 A

    CN      BK00522

    WI      No Missing Stores

    CS      No Missing Stores

     
    But other 2 conditions are working fine.........can you help?
     
    My SP is:
     

    CREATE PROCEDURE SP_MISSINGSALES(@filenm varchar(300)) AS

    declare @vfile as varchar(300)

    declare @msg as varchar(50)

    declare @area as char(2)

    declare @store as char(6)

     

    declare @lastmsg as varchar(50)

    declare @lastarea as char(2)

    declare @fnlprint varchar(60)

     

    declare csrMissing cursor for

      Select Message, Area, Store_num

        from dbo.MissingSales_Tble

        order by Message, Area, Store_num

     

    open csrMissing

     

    fetch next from csrMissing into

       @msg

      ,@area

      ,@store

     

     

    while @@FETCH_STATUS = 0

      begin

        set @lastmsg = @msg

        set @lastarea = NULL

        print ' '

        print @Msg

        print ' '

       

       

        exec dbo.SP_WRITE_TO_FILE @vfile=@filenm, @MSG=@MSG

        while @lastmsg = @msg and @@FETCH_STATUS = 0

          begin

     

            if @area = @lastarea

              begin

                print '     ' + @store

                if @store = ''

                    set @fnlprint = '     ' + 'No store missing'

                else

                    set @fnlprint = '     ' + @store

                exec dbo.SP_WRITE_TO_FILE @vfile=@filenm, @MSG=@fnlprint

              end

            else

              begin

                print @area + '   ' + @store

               

                if @store = ''

                    begin

                        set @fnlprint=@area + '   ' + 'No store missing'

                    end

                else

                    begin

                        set @fnlprint=@area + '   ' + @store

                    end

                   

                set @lastarea = @area

                exec dbo.SP_WRITE_TO_FILE @vfile=@filenm, @MSG=@fnlprint

              end

     

            fetch next from csrMissing into

               @msg

              ,@area

              ,@store

     

          end

     

      end

     

    close csrMissing

    deallocate csrMissing

    GO

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply