USE [Elyse_DB] GO /****** Object: StoredProcedure [reading].[usp_SEL_all_release_ids] Script Date: Sun 22-11-2026 9:04:49 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Silkwood Software -- Create date: 05-07-2026 -- Selects all the release identifiers for all document IDs. -- The search is restricted to document IDs which the user has viewing rights to. /* COPYRIGHT NOTICE This database schema and stored procedures are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2026 */ -- ============================================= CREATE PROCEDURE [reading].[usp_SEL_all_release_ids] @message nvarchar(1000) = NULL OUTPUT, @transaction_status nvarchar(50) = NULL OUTPUT, @numrows bigint = NULL OUTPUT, -- This is the number of rows in the output table of this procedure. @numdocs bigint = NULL OUTPUT -- This is the number of documents. Noting that each document will have zero or more associated files. AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @tempmessage nvarchar(300) = '', @connectedusersid varbinary(100) = SUSER_SID(ORIGINAL_LOGIN()), -- The SID of the connected user @username nvarchar(150) = ORIGINAL_LOGIN(), @userauthentication_status nchar(10) = 'Fail', -- The outcome of the authentication check of the user @temp_userauth_status nchar(10) = 'Fail', @documentid bigint, @fileid bigint, @transaction_ready nchar(10) = 'Ready', @data_validation_status nchar(10) = 'Pass'; DECLARE @AttData TABLE ( doc_count bigint, documentid nvarchar(50), release_type_id bigint, release_type nvarchar(50), release_id_pk bigint, release_identifier nvarchar(50), list_position int, notes nvarchar(max), created datetime2(7), createdby nvarchar(255), state_name_id bigint, state_name nvarchar(50), state_type_id nvarchar(20), effective_from datetime2(7), effective_until datetime2(7), made_authoritative datetime2(7), made_historical datetime2(7) ) -- Parameters which have been initialised at declaration but not explicitly set might be output as null to calling functions. SET @transaction_status = 'Transaction not attempted'; SET @numrows = 0; SET @numdocs = 0; -- Connected user authentication -- Authenticate the connected user for the role EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Controller', @user_authentication_result = @temp_userauth_status OUTPUT; IF @temp_userauth_status = 'Pass' SET @userauthentication_status = 'Pass'; EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Editor', @user_authentication_result = @temp_userauth_status OUTPUT; IF @temp_userauth_status = 'Pass' SET @userauthentication_status = 'Pass'; EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Reviewer', @user_authentication_result = @temp_userauth_status OUTPUT; IF @temp_userauth_status = 'Pass' SET @userauthentication_status = 'Pass'; EXEC [internal].[usp_AUTHENTICATE_user_role] @role_to_check = 'Reader', @user_authentication_result = @temp_userauth_status OUTPUT; IF @temp_userauth_status = 'Pass' SET @userauthentication_status = 'Pass'; IF @userauthentication_status = 'Fail' BEGIN -- The user does not have permission for this action SET @transaction_ready = 'Fail'; EXEC internal.usp_SEL_message @message_id = 'NoPermission', @message_text = @tempmessage OUTPUT; IF (@tempmessage IS NOT NULL) SET @message = CONCAT(@message, ' | ', ISNULL(@username, ''), ' ', @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on NoPermission'); END -- End user authentication. -- ================================================================== IF @transaction_ready = 'Ready' BEGIN BEGIN TRY WITH docidsCTE AS ( SELECT searchtable.doc_id AS docid FROM base.release_identifiers AS searchtable --### The search criteria table INNER JOIN internal.ufn_SEL_docs_permission_filtr_ITVF(@connectedusersid) AS perm -- Apply the permission filter ON searchtable.doc_id = perm.doc_id ) INSERT INTO @AttData (doc_count, documentid, release_type_id, release_type, release_id_pk, release_identifier, list_position, notes, created, createdby, state_name_id, state_name, state_type_id, effective_from, effective_until, made_authoritative, made_historical) (SELECT DISTINCT DENSE_RANK() OVER (ORDER BY docidsCTE.docid) AS doc_count, docidsCTE.docid, rid.release_type_id, ISNULL(ridt.release_type, ''), rid.release_id_pk, rid.release_identifier, rid.list_position, rid.notes, rid.created, rid.created_by, rid.release_state_name_id, (SELECT release_state_name FROM base.release_state_names AS rsn WHERE rsn.release_state_name_id = rid.release_state_name_id) , rid.release_state_type_id, rid.effective_from, rid.effective_until, rid.when_made_authoritative, rid.when_made_historical FROM base.release_identifiers AS rid LEFT JOIN base.release_id_types AS ridt ON ridt.release_type_id = rid.release_type_id RIGHT JOIN docidsCTE ON docidsCTE.docid = rid.doc_id); SELECT @numdocs = MAX(doc_count) from @AttData; -- ================================================================================ SELECT ad.documentid AS 'Document ID', ad.release_type_id AS 'Release Type ID', ISNULL(ad.release_type, '') AS 'Release Type', ad.release_id_pk AS 'Release Primary Key ID', ad.release_identifier AS 'Release', ad.list_position AS 'List Position', ad.notes AS 'Notes', ad.created AS 'Created', ad.createdby AS 'Created By', ad.state_name_id AS 'Release State Name ID', ad.state_name AS 'Release State Name', ad.state_type_id AS 'Release State Type', ad.effective_from AS 'Effective From', ad.effective_until AS 'Effective Until', ad.made_authoritative AS 'Made Authoritative', ad.made_historical AS 'Made Historical' FROM @AttData AS ad ORDER BY 'List Position' DESC; -- =================================================== SET @numrows = @@ROWCOUNT; EXEC internal.usp_SEL_message @message_id = 'Success', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT_WS(' | ', @message, @tempmessage); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on Success'); SET @transaction_status = 'Good'; END TRY BEGIN CATCH SET @transaction_status = 'Bad'; EXEC internal.usp_SEL_message @message_id = 'SelectError', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @message = CONCAT_WS(' | ', @message, @tempmessage, CONVERT(nvarchar(10),ERROR_NUMBER()), ERROR_MESSAGE()); ELSE SET @message = CONCAT_WS(' | ', @message, 'A database level message error occurred on SelectError'); END CATCH END -- END IF @transaction_ready = 'Ready' SET @numrows = ISNULL(@numrows, 0); SET @numdocs = ISNULL(@numdocs, 0); END GO