Hi @Bobby P
This is a common challenge when dealing with shared resources like stored procedures, temporary tables, and SSRS reports in multi-user environments.
Challenges:
- Concurrency Issues:
- Multiple users or reports running at the same time might overwrite or conflict with each other's data when using shared resources (e.g., a global temporary table).
- Uniqueness:
- Each report's execution needs a unique identifier to separate its data from others.
- Cleanup:
- If using staging tables, stale or unused data can accumulate and impact performance unless cleaned properly.
- Global Temporary Tables:
- These are automatically dropped when the session ends, but naming conflicts arise if multiple sessions/reports access the same stored procedure simultaneously.
My suggestion would be "use a Staging Table with Unique Identifiers":
Instead of a global temporary table, use a staging table that persists data for the duration of the report execution. Here's how:
- Add a column for a unique session identifier (e.g., SessionID or ExecutionID) in the staging table.
- Generate a unique SessionID for each report execution (e.g., using NEWID() or the SSRS Globals!ExecutionID property).
- Pass the SessionID to the stored procedure and include it in every record inserted into the staging table.
- Modify the stored procedure to filter data based on the SessionID when returning results.
- Clean up the staging table after the report completes using a scheduled cleanup job or a final cleanup query in SSRS.
Example:
CREATE TABLE MemberEligibilityStaging (
SessionID UNIQUEIDENTIFIER,
MemberID INT,
EligibilityStatus VARCHAR(50),
ValidUntil DATETIME,
PRIMARY KEY (SessionID, MemberID)
);
-- Stored Procedure Example
CREATE PROCEDURE GetMemberEligibility
@SessionID UNIQUEIDENTIFIER
AS
BEGIN
-- Insert result set into staging table with the provided SessionID
INSERT INTO MemberEligibilityStaging (SessionID, MemberID, EligibilityStatus, ValidUntil)
SELECT
@SessionID,
MemberID,
EligibilityStatus,
ValidUntil
FROM
SomeSourceTable;
-- Return results for the current session
SELECT
MemberID,
EligibilityStatus,
ValidUntil
FROM
MemberEligibilityStaging
WHERE
SessionID = @SessionID;
END;
Pros:
- Data is explicitly tied to each execution instance.
- Avoids naming conflicts with global temporary tables.
- Staging tables are easier to debug.
Cons:
- Requires cleanup (can be automated with a SQL Agent job or triggers).
Please let me know if any questions
Kindly accept answer if it helps
Thanks
Naveen