views:

46

answers:

2

I have a stored procedure that takes a table name and writes out a series of INSERT statements, one for each row in the table. It's being used to provide sample, "real world" data for our test environment.

It works well but some of these sample rowsets are 10, 20k records. The stored proc writes them out using the PRINT statement and it's hard to copy that many lines and paste them into the management studio to run them. Is there a SQL redirect feature I might be able to use, perhaps to write this output to a table and a way to loop through and run each statement that way? Just a thought.

I'd like to do this all from within the management studio and not have to write a C# program to create a dataset and loop over it, etc. I'm essentially looking for suggestions on a good approach. Thanks very much.

+2  A: 

Use EXEC:

PRINT @INSERT_statement
 EXEC @INSERT_statement

...to run the query.

But I'd recommend looking at bulk insertion to make the data load faster:

OMG Ponies
Do you mean " *instead of* `PRINT @INSERT_statement` have your stored proc do `EXEC @INSERT_statement`"?
egrunin
@egrunin: I don't see the harm in printing the statement, but it also serves to show when to use EXEC in the existing setup.
OMG Ponies
I'll look into bulk insert, thanks for the advice. On a side note, is it possible to PRINT statements to a file instead of the console? So I can store the INSERT statements for later use, for example.
larryq
@larryq: I dunno, never had the need for console redirection. Want to create a separate question about it?
OMG Ponies
@OMG Ponies: ah, that makes sense, thanks.
egrunin
+1  A: 

Where is your stored procedure getting this data?

You may want to look into importing it as a table and then running your stored procedure against that inserted table. SQL Server Management studio has many options for importing data.

If your stored proc is generating the data - then that's a whole other issue.

Cyrena