Send print statements out to text file

Glen Lepeska 6 Reputation points
2021-11-10T16:52:37.877+00:00

Hello, I need to send these print commands out to a text file. How is this done?

PRINT '@UserID, ' + Cast(@UserID AS VARCHAR(MAX));
PRINT '@UserDate, ' + Cast(@UserDate AS VARCHAR(MAX));
PRINT '@LocationID, ' + Cast(@LocationID AS VARCHAR(MAX));
PRINT '@CustomerID, ' + Cast(@CustomerID AS VARCHAR(MAX));
PRINT '@EquipmentID, ' + Cast(@EquipmentID AS VARCHAR(MAX));
PRINT '@ReadingDate, ' + Cast(@ReadingDate AS VARCHAR(MAX));
PRINT '@ReadingType, ' + Cast(@ReadingType AS VARCHAR(MAX));
PRINT '@NumberOfDays, ' + Cast(@NumberOfDays AS VARCHAR(MAX));
PRINT '@Consumption1, ' + Cast(@Consumption1 AS VARCHAR(MAX));
PRINT '@Consumption2, ' + Cast(@Consumption2 AS VARCHAR(MAX));
PRINT '@Consumption3, ' + Cast(@Consumption3 AS VARCHAR(MAX));
PRINT '@Reading, ' + Cast(@Reading AS VARCHAR(MAX));
PRINT '@PrevReadingValue, ' + Cast(@PrevReadingValue AS VARCHAR(MAX));
PRINT '@umBatchID ' + Cast(@umBatchID AS VARCHAR(MAX));

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
{count} votes

7 answers

Sort by: Most helpful
  1. Guoxiong 8,216 Reputation points
    2021-11-10T18:57:03.8+00:00

    Here is the good example "How to Write in Text File from T-SQL in SQL Server?".

    In the Second Step, you can replace the code at line 6 with the following code:

    EXECUTE sp_OAMethod @FileID, 'WriteLine', Null, 'PRINT ''@UserID, '' + Cast(@UserID AS VARCHAR(MAX));
    PRINT ''@UserDate, '' + Cast(@UserDate AS VARCHAR(MAX));
    PRINT ''@LocationID, '' + Cast(@LocationID AS VARCHAR(MAX));
    PRINT ''@CustomerID, '' + Cast(@CustomerID AS VARCHAR(MAX));
    PRINT ''@EquipmentID, '' + Cast(@EquipmentID AS VARCHAR(MAX));
    PRINT ''@ReadingDate, '' + Cast(@ReadingDate AS VARCHAR(MAX));
    PRINT ''@ReadingType, '' + Cast(@ReadingType AS VARCHAR(MAX));
    PRINT ''@NumberOfDays, '' + Cast(@NumberOfDays AS VARCHAR(MAX));
    PRINT ''@Consumption1, '' + Cast(@Consumption1 AS VARCHAR(MAX));
    PRINT ''@Consumption2, '' + Cast(@Consumption2 AS VARCHAR(MAX));
    PRINT ''@Consumption3, '' + Cast(@Consumption3 AS VARCHAR(MAX));
    PRINT ''@Reading, '' + Cast(@Reading AS VARCHAR(MAX));
    PRINT ''@PrevReadingValue, '' + Cast(@PrevReadingValue AS VARCHAR(MAX));
    PRINT ''@umBatchID '' + Cast(@umBatchID AS VARCHAR(MAX));'
    

    Basically you need to replace the single quote with the double one around the variables.

    0 comments No comments

  2. Glen Lepeska 6 Reputation points
    2021-11-10T20:27:30.057+00:00

    Thank you GuoxiongYuan-7218. I assume I will need to do this for all the print commands then.


  3. Guoxiong 8,216 Reputation points
    2021-11-10T21:21:43.917+00:00

    Replace the code at line 6 with the following one:

    EXECUTE sp_OAMethod @FileID, 'WriteLine', Null, 'PRINT ''@UserID, '' + Cast(@UserID AS VARCHAR(MAX));
    PRINT ''@UserDate, '' + Cast(@UserDate AS VARCHAR(MAX));
    PRINT ''@LocationID, '' + Cast(@LocationID AS VARCHAR(MAX));
    PRINT ''@CustomerID, '' + Cast(@CustomerID AS VARCHAR(MAX));
    PRINT ''@EquipmentID, '' + Cast(@EquipmentID AS VARCHAR(MAX));
    PRINT ''@ReadingDate, '' + Cast(@ReadingDate AS VARCHAR(MAX));
    PRINT ''@ReadingType, '' + Cast(@ReadingType AS VARCHAR(MAX));
    PRINT ''@NumberOfDays, '' + Cast(@NumberOfDays AS VARCHAR(MAX));
    PRINT ''@Consumption1, '' + Cast(@Consumption1 AS VARCHAR(MAX));
    PRINT ''@Consumption2, '' + Cast(@Consumption2 AS VARCHAR(MAX));
    PRINT ''@Consumption3, '' + Cast(@Consumption3 AS VARCHAR(MAX));
    PRINT ''@Reading, '' + Cast(@Reading AS VARCHAR(MAX));
    PRINT ''@PrevReadingValue, '' + Cast(@PrevReadingValue AS VARCHAR(MAX));
    PRINT ''@umBatchID '' + Cast(@umBatchID AS VARCHAR(MAX));'
    
    0 comments No comments

  4. Guoxiong 8,216 Reputation points
    2021-11-10T21:24:19.72+00:00

    It seems the posted code is blocked. Here is the screenshot:

    148236-image.png

    0 comments No comments

  5. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2021-11-10T22:34:35.957+00:00

    If you want the output to file when you run from SSMS, you can press Ctrl-Shift-F to redirect the output to a file. (SSMS will prompt you for a file name.) This is also available in the Query->Results To submenu .

    Another option is to use the command-line tool SQLCMD to run the script and use the -o option redirect the output to a file.

    I'm mentioning this, as you may find this more lightweight than using sp_OAmethod.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.