https://github.com/alejandromeyer/gettotaldata-sql-procedure
This project contains the GetTotalData SQL stored procedure, which dynamically counts records from multiple tables based on the current date. It iterates through a predefined table list, executes queries, and returns a summarized dataset.
https://github.com/alejandromeyer/gettotaldata-sql-procedure
dynamic-query sql stored-procedures
Last synced: 6 months ago
JSON representation
This project contains the GetTotalData SQL stored procedure, which dynamically counts records from multiple tables based on the current date. It iterates through a predefined table list, executes queries, and returns a summarized dataset.
- Host: GitHub
- URL: https://github.com/alejandromeyer/gettotaldata-sql-procedure
- Owner: AlejandroMeyer
- Created: 2025-02-05T11:40:56.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-06T10:15:02.000Z (over 1 year ago)
- Last Synced: 2025-02-16T08:16:33.367Z (over 1 year ago)
- Topics: dynamic-query, sql, stored-procedures
- Language: TSQL
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GetTotalData SQL Procedure
## About
This project contains an SQL stored procedure named `GetTotalData`, which is designed to extract and count records from a specified table based on a date filter. The procedure dynamically constructs SQL queries to count records for each table and stores the results in a temporary table before returning the final dataset.
## Features
- Uses a cursor to iterate over a list of tables.
- Dynamically constructs and executes SQL queries.
- Filters records based on the current date.
- Stores and returns results in a temporary table.
## How It Works
1. Declares a temporary table `#TempResult` to store the results.
2. Declares necessary variables for processing.
3. Uses a cursor to iterate through the `extraccions_test` table to retrieve table names and related metadata.
4. Dynamically constructs and executes a SQL query to count records where the date field matches the current date.
5. Stores the results in the temporary table.
6. Returns the final dataset.
7. Cleans up by closing and deallocating the cursor.
## Usage
To execute the stored procedure, use the following command in SQL Server:
```sql
EXEC GetTotalData;