Monday, June 24, 2013

MySQL: How to Create a Stored Procedure That Names a Table From a Variable and Then Inserts a Specific Set of Data From Another Table

This snippet of code shows you how to build a table from a name that you set in the parameters of a stored procedure. Secondly, it inserts data from specific columns or datasets of another table by using Dynamic SQL prepared statements.

This stored procedure can be used to build new tables in MySQL which can then can be used  like Common Table Expressions (CTEs) in MS SQL Server/SSRS.

This statement:

  1. Builds a table from a variable name.
  2. Loads specific columns from another table into the new table.
  3. The procedure can then be called multiple times to form different tables.
  4. From there, you can do various SELECT statements and joins to process your data comparisons.

Link to Gist on GitHub: https://gist.github.com/jushusted/5853529
Resullt: http://jsfiddle.net/jushusted/YqWhX/embedded/result/


Loading ....

No comments:

Post a Comment