Can I print immediately for each iteration in a loop?

Iain Samuel McLean Elder

My deployment server runs a deployment script for every new database build.

Part of the script blocks to wait for another asynchronous operation to complete.

The blocking code looks like this:

DECLARE @i INT = 0;
DECLARE @laststatus NVARCHAR(MAX) = N'';

WHILE @i < 5
BEGIN
  -- the real delay is longer
  WAITFOR DELAY '00:00:01';

  -- poll async operation status here
  SET @i = @i + 1;

  SET @laststatus = N'status is ' + CAST(@i AS NVARCHAR(MAX));
  RAISERROR(@laststatus, 0, 1) WITH NOWAIT;
END;

It uses the WITH NOWAIT clause of RAISERROR instead of PRINT because it's supposed to print a status update for every iteration.

The deployment server runs the script in sqlcmd with this command:

sqlcmd.exe -i print_test.sql

The output appears all at once like this:

status is 1
status is 2
status is 3
status is 4
status is 5

It should print this after one second:

status is 1

After another second it should print this

status is 2

And so on.

Is there a way to do this in sqlcmd?

Iain Samuel McLean Elder

You can use osql instead. It's deprecated, but it works as you expect.

The equivalent command is:

osql -E -n -i print_test.sql

osql by default expects a username and password. Use the -E switch to use Windows authentication. This is the opposite of sqlcmd default behavior.

osql by default prints a number for every line in the input file script.

1> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> 14> 15>

Use the -n switch to suppress the line numbers.

sqlcmd has no -n switch. It just doesn't print line numbers when the -i switch is set.

Martin Smith led me to the workaround by quoting the Microsoft Connect item about this issue.

If you a script which uses RAISERROR WITH NOWAIT, the output is nevertheless buffered. This works correctly with OSQL and SQLCMD from SQL 2008.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make speech occur immediately for each iteration of a while loop in Swift?

From Dev

How can I print the first result of a search sequence for each iteration?

From Dev

How can I measure the time it takes for each iteration of a for loop?

From Dev

How can I add a delay inside each iteration of an _.each loop in underscore.js?

From Dev

How can I change this code to make the progress bars appear for each file and the iteration be each loop?

From Dev

How can I add a delay inside each iteration of an _.each loop in underscore.js?

From Dev

jQuery .dialog inside .each loop - new dialog content divs are written on each iteration - how can I destroy the old divs on each loop?

From Dev

In for each loop i want to skip ", " in last iteration

From Dev

Loop through an array and print out a template for each iteration

From Dev

Make output print once per loop, not each iteration

From Dev

How can I replace a javascript for loop with a forEach and get access to the index for each iteration?

From Dev

How do I print each iteration in Selection sort in C?

From Dev

Where can I print out logs to see immediately in Android Studio?

From Dev

How can I print the text immediately in combobox with bind event?

From Dev

How do I increment row in mysql at each iteration of a"for" loop?

From Dev

How do I set the names of a list on each iteration through a loop

From Dev

Can I use a for loop that shifts in 1 new bit each iteration to get some 8-bit number in Java?

From Dev

MVC 5 - Each iteration of foreach loop creates new radio button group. How can I access results in the controller?

From Dev

I can't print each element in a queue

From Dev

In rails I can't display the new comment with iteration each do

From Dev

How can I call the callback function after each string iteration?

From Dev

await task in each iteration of a loop

From Dev

Add delay on each loop iteration

From Dev

Delaying For Loop Between Each Iteration

From Dev

R loop: How can I determine the iteration that caused warnings?

From Dev

How can I repeat an iteration in a PowerShell foreach loop?

From Dev

How can I assign different iteration within for loop in C#?

From Dev

How can I repeat an iteration in a PowerShell foreach loop?

From Dev

How can I count the result of a for each loop?

Related Related

  1. 1

    How can I make speech occur immediately for each iteration of a while loop in Swift?

  2. 2

    How can I print the first result of a search sequence for each iteration?

  3. 3

    How can I measure the time it takes for each iteration of a for loop?

  4. 4

    How can I add a delay inside each iteration of an _.each loop in underscore.js?

  5. 5

    How can I change this code to make the progress bars appear for each file and the iteration be each loop?

  6. 6

    How can I add a delay inside each iteration of an _.each loop in underscore.js?

  7. 7

    jQuery .dialog inside .each loop - new dialog content divs are written on each iteration - how can I destroy the old divs on each loop?

  8. 8

    In for each loop i want to skip ", " in last iteration

  9. 9

    Loop through an array and print out a template for each iteration

  10. 10

    Make output print once per loop, not each iteration

  11. 11

    How can I replace a javascript for loop with a forEach and get access to the index for each iteration?

  12. 12

    How do I print each iteration in Selection sort in C?

  13. 13

    Where can I print out logs to see immediately in Android Studio?

  14. 14

    How can I print the text immediately in combobox with bind event?

  15. 15

    How do I increment row in mysql at each iteration of a"for" loop?

  16. 16

    How do I set the names of a list on each iteration through a loop

  17. 17

    Can I use a for loop that shifts in 1 new bit each iteration to get some 8-bit number in Java?

  18. 18

    MVC 5 - Each iteration of foreach loop creates new radio button group. How can I access results in the controller?

  19. 19

    I can't print each element in a queue

  20. 20

    In rails I can't display the new comment with iteration each do

  21. 21

    How can I call the callback function after each string iteration?

  22. 22

    await task in each iteration of a loop

  23. 23

    Add delay on each loop iteration

  24. 24

    Delaying For Loop Between Each Iteration

  25. 25

    R loop: How can I determine the iteration that caused warnings?

  26. 26

    How can I repeat an iteration in a PowerShell foreach loop?

  27. 27

    How can I assign different iteration within for loop in C#?

  28. 28

    How can I repeat an iteration in a PowerShell foreach loop?

  29. 29

    How can I count the result of a for each loop?

HotTag

Archive