USE [Elyse_DB] GO /****** Object: StoredProcedure [internal].[usp_INS_file] 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: 26-06-2023 -- Description: Initial creation /* Inserts files into the filestream container Input is a file, filename of the record to be saved, a transaction id and an optional full text content string.. The content hash is stored as a hex string. File size is determined and saved also. A hash of the file is stored for duplicate management purposes. The filename does not have to be the same as the original filename of the record to be saved. The transaction id is retrieved by the application from a different stored procedure and passed back to this one. For example, a group of files being added at the same time can be grouped by the same transaction id. Output is a status message, a transaction status, the id of the new record. Data validation is carried out by the calling procedure. */ /* COPYRIGHT NOTICE This database schema and stored procedures are protected by copyright. Copyright. Silkwood Software Pty. Ltd. 2023 */ -- ============================================= CREATE PROCEDURE [internal].[usp_INS_file] @f_ins_file_content varbinary(MAX), @f_ins_stored_filename nvarchar(255), @f_ins_plain_text_content nvarchar(max), @f_ins_thumbnail varbinary(max), @f_ins_transaction_group bigint = NULL, @f_ins_created_by_id bigint = NULL, @f_ins_retention_date datetime2(7) = NULL, @f_ins_message nvarchar(1000) = NULL OUTPUT, @f_ins_transaction_status nchar(50) = 'Transaction not attempted' OUTPUT, @f_ins_file_id bigint = NULL OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @file_hash varbinary(32), @file_size bigint, @tempmessage nvarchar(300) = '', @temptransmessage nvarchar(300) = '', @transactionmessage nvarchar(300) = '', @transactionmessage2 nvarchar(300) = '', @duplicatemessage nvarchar(200) = '', -- Communicates that a duplicate file exists @data_validation_status nchar(10) = 'Pass'; -- Create a file hash for duplicate management performed elsewhere. SET @file_hash = HASHBYTES('SHA2_256', @f_ins_file_content); -- Determine the size of the file. SET @file_size = DATALENGTH(@f_ins_file_content); /* Using OPENROWSET(BULK... is an unsuitable approach in this context because it requires BULK ADMIN privileges, which introduces security issues. Hence the approach used is that the application must open the file and place it into an object which is passed to the stored procedure. */ -- Check if a duplicate file exists. IF EXISTS (SELECT content_hash FROM base.file_metadata WHERE content_hash = @file_hash) EXEC internal.usp_SEL_message @message_id = 'DuplicateFile', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @duplicatemessage = @tempmessage; ELSE SET @transactionmessage = 'A database level message error occurred on DuplicateFile.'; -- Execute the insert query -- NOTE: This procedure does NOT manage its own transaction. -- The calling procedure (editing.usp_INS_file_binary_input) is -- responsible for BEGIN / COMMIT / ROLLBACK TRANSACTION. -- A savepoint is used so that errors here can be caught without -- rolling back the caller's outer transaction (which would cause -- Error 266: transaction count mismatch). BEGIN DECLARE @has_outer_transaction BIT = CASE WHEN @@TRANCOUNT > 0 THEN 1 ELSE 0 END; BEGIN TRY IF @has_outer_transaction = 1 SAVE TRANSACTION usp_INS_file_save; ELSE BEGIN TRANSACTION; INSERT INTO base.file_storage (file_guid, file_content) VALUES (NEWID(), @f_ins_file_content) -- Retrieve the new file id. SET @f_ins_file_id = SCOPE_IDENTITY(); INSERT INTO base.file_metadata (file_id, filename, content_hash, transaction_group_id, file_size, created_by_sid_id, retention_date) VALUES (@f_ins_file_id, @f_ins_stored_filename, @file_hash, @f_ins_transaction_group, @file_size, @f_ins_created_by_id, @f_ins_retention_date) INSERT INTO base.file_plain_text_content (file_id, plain_text_content) VALUES (@f_ins_file_id, @f_ins_plain_text_content) INSERT INTO base.file_thumbnails (file_id, thumbnail) VALUES (@f_ins_file_id, @f_ins_thumbnail) IF @has_outer_transaction = 0 COMMIT TRANSACTION; -- When called within an outer transaction, do NOT commit here; -- the caller will commit. EXEC internal.usp_SEL_message @message_id = 'FileInserted', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @transactionmessage = @tempmessage; ELSE SET @transactionmessage = 'A database level message error occurred on Success.'; SET @f_ins_transaction_status = 'Good'; END TRY BEGIN CATCH IF @has_outer_transaction = 1 BEGIN -- Roll back only to the savepoint, preserving the caller's transaction. -- This prevents Error 266 (transaction count mismatch). IF XACT_STATE() = 1 ROLLBACK TRANSACTION usp_INS_file_save; -- If XACT_STATE() = -1 the transaction is doomed and the caller's -- CATCH block will handle the full ROLLBACK. END ELSE BEGIN IF XACT_STATE() <> 0 ROLLBACK TRANSACTION; END SET @f_ins_transaction_status = 'Bad'; EXEC internal.usp_SEL_message @message_id = 'InsertError', @message_text = @tempmessage OUTPUT; IF @tempmessage IS NOT NULL SET @transactionmessage = @tempmessage + ' | ' + CONVERT(nvarchar(10),ERROR_NUMBER()) + ' | ' + ERROR_MESSAGE(); ELSE SET @transactionmessage = 'A database level message error occurred on InsertError.'; END CATCH END IF (@f_ins_message = '' OR @f_ins_message IS NULL) SET @f_ins_message = ' '; IF (@transactionmessage <> '') SET @f_ins_message = @transactionmessage; IF (@transactionmessage2 <> '') SET @f_ins_message = @f_ins_message + ' | ' + @transactionmessage2; IF (@duplicatemessage <> '') SET @f_ins_message = @f_ins_message + ' | ' + @duplicatemessage; -- This procedure is internal and does not use the cursor to return data. END GO