Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Duplicate SQL table to creat large dummy table

I have an SQL table already, the format of which I just want duplicating for testing purposes. I tried the following:

DECLARE @Counter AS INT(1)
WHILE ( @Counter <= 10)
BEGIN 
CREATE TABLE my_new_big_table
  AS (SELECT * FROM my_table)
  SET @Counter = @Counter +1
END;

However I encounter an error with ‘DECLARE’

ParseException: 
[PARSE_SYNTAX_ERROR] Syntax error at or near 'DECLARE'(line 1, pos 0)

Is this the best approach to duplicate an existing table?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your approach seems wrong. With a CREATE TABLE statement in the loop, you’d try to create the same table ten times.

Here is a solution in pure SQL that I hope will work for you. I take the original table and cross join it with 10 generated rows, so as to get the original rows tenfold.

CREATE TABLE my_new_big_table AS
  SELECT t.*
  FROM my_table
  CROSS JOIN VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10) v(i);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading