quinta-feira, 25 de fevereiro de 2016

Repeating a T-SQL script a number of times, without using a loop

Ever wondered how to execute a certain command a bunch of times, without using a loop (while, cursors, etc)?

Try using GO with a number. That will execute the prior query the number of times you set.

If you execute the code bellow, it will create the sample "mytable" and add the name of a 100 table names in your database.

CREATE TABLE mytable
  (
     NAME VARCHAR(100)
  );

INSERT INTO mytable
            (NAME)
SELECT TOP 1 NAME
FROM   sys.tables
ORDER  BY Newid()
go 100 


That was a good one! ;)

Check out my new book about R Language http://www.amazon.com/dp/B00SX6WA06

Nenhum comentário:

Postar um comentário

Leave your comment here!