Advertisement
exihs

Untitled

Jan 13th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.22 KB | None | 0 0
  1. /*Show the content in all tables*/
  2.  
  3. DECLARE @table varchar(20); /*Declare a variable It will be used to store the name of the current table in the loop*/
  4.  
  5. /*Declare the cursor CurrTable, get and assign all the table names*/
  6. /* Since we plan to iterate linearly, FAST_FORWARD is used. It makes the cursor read-only and only move forward. Also, it is better for performance */
  7. DECLARE CurrTable CURSOR FAST_FORWARD FOR SELECT name FROM SYSOBJECTS WHERE xtype = 'U'  
  8.  
  9. /*Activate the cursor and populate it with the data*/
  10. OPEN CurrTable
  11.  
  12. FETCH NEXT FROM CurrTable INTO @table /* Set table to the first item in the cursor*/
  13.  
  14. /* Start iterating through the cursor */
  15.  
  16. WHILE @@FETCH_STATUS = 0 /* if the status of the previous fetch was successful */
  17. BEGIN
  18.     /* Show the content of the table with a select statement*/
  19.     /* EXEC statement executes the statement. It's used for integrating the table variable into it*/
  20.     EXEC('SELECT * FROM ' + @table)
  21.  
  22.     /* Set table to the next item in the cursor*/
  23.     FETCH NEXT FROM CurrTable INTO @table  
  24. END
  25.  
  26. /* Deactivate the cursor and free it */
  27. CLOSE CurrTable  
  28.  
  29. /* Remove the cursor reference, it is no longer needed */
  30. DEALLOCATE CurrTable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement