EXEC(' CREATE OR ALTER PROCEDURE TLXROWPOLICY.TLXAuditMoveAndInsertAndPurge @SourcePath…
- User
EXEC(' CREATE OR ALTER PROCEDURE TLX_ROW_POLICY.TLX_Audit_MoveAndInsertAndPurge @SourcePath NVARCHAR(255), @DestinationPath NVARCHAR(255), @DaysToKeep INT = 90, @UserName NVARCHAR(128) AS BEGIN SET NOCOUNT ON; DECLARE @CurrentRunID UNIQUEIDENTIFIER = NEWID(); -- Create AuditProcessLog table if it doesn''t exist IF OBJECT_ID(''TLX_ROW_POLICY.AuditProcessLog'', ''U'') IS NULL BEGIN CREATE TABLE TLX_ROW_POLICY.AuditProcessLog ( RunID UNIQUEIDENTIFIER, LogMessage NVARCHAR(MAX), LogTime DATETIME DEFAULT GETDATE(), IsError BIT ); -- Grant read (SELECT) permission on AuditProcessLog table using dynamic SQL DECLARE @GrantSQLAuditProcessLog NVARCHAR(MAX); SET @GrantSQLAuditProcessLog = N''GRANT SELECT ON TLX_ROW_POLICY.AuditProcessLog TO '' + QUOTENAME(@UserName); -- Execute dynamic SQL to grant permission EXEC sp_executesql @GrantSQLAuditProcessLog; INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Read Grants on Table AuditProcessLog added successfully to ''+@UserName+''.'', 0); END -- Log the start of the process INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Starting the audit data processing...'', 0); -- Conditionally create TLX_INGESTION_WATERMARK table and grant permissions BEGIN IF NOT EXISTS ( SELECT * FROM sys.tables WHERE name = ''TLX_INGESTION_WATERMARK'' AND schema_id = SCHEMA_ID(''TLX_ROW_POLICY'') ) BEGIN CREATE TABLE TLX_ROW_POLICY.TLX_INGESTION_WATERMARK ( PROCESS_NAME VARCHAR(100), LAST_INGESTED_AT DATETIME NOT NULL, UPDATED_AT DATETIME DEFAULT GETDATE(), CONSTRAINT PK_TLX_INGESTION_WATERMARK PRIMARY KEY (PROCESS_NAME, LAST_INGESTED_AT) ); INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Table TLX_INGESTION_WATERMARK created successfully.'', 0); -- Grant SELECT, INSERT, UPDATE on TLX_INGESTION_WATERMARK to @UserName DECLARE @GrantSQL_Watermark NVARCHAR(MAX); SET @GrantSQL_Watermark = N''GRANT SELECT, INSERT, UPDATE ON TLX_ROW_POLICY.TLX_INGESTION_WATERMARK TO '' + QUOTENAME(@UserName); EXEC sp_executesql @GrantSQL_Watermark; INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Permissions granted on TLX_INGESTION_WATERMARK to '' + @UserName + ''.'', 0); END ELSE BEGIN INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Table TLX_INGESTION_WATERMARK already exists.'', 0); END END BEGIN TRY -- Create the audit table if it doesn''t exist IF OBJECT_ID(''TLX_ROW_POLICY.tlx_sqlserver_audits'', ''U'') IS NULL BEGIN CREATE TABLE TLX_ROW_POLICY.tlx_sqlserver_audits ( event_time DATETIME NOT NULL, action_id INT, succeeded BIT, session_id INT, class_type NVARCHAR(50), server_principal_name NVARCHAR(255), database_principal_name NVARCHAR(255), server_instance_name NVARCHAR(255), database_name NVARCHAR(255), schema_name NVARCHAR(255), object_name NVARCHAR(255), statement NVARCHAR(MAX), client_ip NVARCHAR(50), application_name NVARCHAR(255), duration_milliseconds INT, response_rows INT, affected_rows INT, host_name NVARCHAR(255) ); INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Table TLX_Audit_FromLogs created successfully.'', 0); IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(''TLX_ROW_POLICY.tlx_sqlserver_audits'') AND name = ''idx_event_time'') BEGIN CREATE CLUSTERED INDEX idx_event_time ON TLX_POLICY_DB.TLX_ROW_POLICY.tlx_sqlserver_audits(event_time) ON ps_event_time(event_time); END; INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''NONCLUSTERED INDEX idx_event_time created on tlx_sqlserver_audits column event_time successfully.'', 0); -- Grant read (SELECT) permission on tlx_sqlserver_audits table using dynamic SQL DECLARE @GrantSQLAudits NVARCHAR(MAX); SET @GrantSQLAudits = N''GRANT SELECT ON TLX_ROW_POLICY.tlx_sqlserver_audits TO '' + QUOTENAME(@UserName); -- Execute dynamic SQL to grant permission EXEC sp_executesql @GrantSQLAudits; INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Read Grants on Table tlx_sqlserver_audits added successfully to ''+@UserName+''.'', 0); END IF OBJECT_ID(''TLX_ROW_POLICY.tlx_sqlserver_audits_staging'', ''U'') IS NULL BEGIN CREATE TABLE TLX_ROW_POLICY.tlx_sqlserver_audits_staging ( event_time DATETIME NOT NULL, action_id INT, succeeded BIT, session_id INT, class_type NVARCHAR(50), server_principal_name NVARCHAR(255), database_principal_name NVARCHAR(255), server_instance_name NVARCHAR(255), database_name NVARCHAR(255), schema_name NVARCHAR(255), object_name NVARCHAR(255), statement NVARCHAR(MAX), client_ip NVARCHAR(50), application_name NVARCHAR(255), duration_milliseconds INT, response_rows INT, affected_rows INT, host_name NVARCHAR(255) ) END IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(''TLX_ROW_POLICY.tlx_sqlserver_audits_staging'') AND name = ''idx_event_time'') BEGIN CREATE CLUSTERED INDEX idx_event_time ON TLX_POLICY_DB.TLX_ROW_POLICY.tlx_sqlserver_audits_staging(event_time) END; -- Check if the destination folder exists DECLARE @FolderExists INT; DECLARE @DirCommand NVARCHAR(500); SET @DirCommand = ''dir "'' + @DestinationPath + ''"''; CREATE TABLE #TempResult (Result NVARCHAR(255)); INSERT INTO #TempResult (Result) EXEC xp_cmdshell @DirCommand; SET @FolderExists = CASE WHEN EXISTS (SELECT * FROM #TempResult WHERE Result IS NOT NULL) THEN 1 ELSE 0 END; DROP TABLE #TempResult; IF @FolderExists = 0 BEGIN -- Create the directory if it doesn''t exist SET @DirCommand = ''mkdir "'' + @DestinationPath + ''"''; CREATE TABLE #CmdShellOutput (OutputMessage NVARCHAR(4000)); INSERT INTO #CmdShellOutput EXEC xp_cmdshell @DirCommand; SELECT COALESCE(OutputMessage, ''No output generated (possibly dir exists).'') AS CmdShellOutput FROM #CmdShellOutput; DROP TABLE #CmdShellOutput; INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Destination folder created successfully.'', 0); END -- Move files using PowerShell command DECLARE @PowerShellCommand NVARCHAR(1000); SET @PowerShellCommand = ''powershell.exe -Command "Get-ChildItem '''''' + @SourcePath + ''*'''''' + '' | ForEach-Object { '' + ''try { '' + ''Move-Item -Path .FullName + '''' - Error: '''' + $_.Exception.Message } '' + -- Generic error handling ''} '' + ''} '' + ''"''; -- Execute the PowerShell command CREATE TABLE #CmdShellOutput1 (OutputMessage NVARCHAR(4000)); INSERT INTO #CmdShellOutput1 EXEC xp_cmdshell @PowerShellCommand; SELECT COALESCE(OutputMessage, ''No output generated (possibly no files to move).'') AS CmdShellOutput FROM #CmdShellOutput1; DROP TABLE #CmdShellOutput1; INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''File movement process completed.'', 0); -- Select count of all records from TLX_Audit_FromLogs before INSERTING new rows SELECT COUNT() AS RecordCountFinal FROM TLX_ROW_POLICY.tlx_sqlserver_audits; -- Start purge of old watermark and audit log data INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Starting purge of TLX_INGESTION_WATERMARK and AuditProcessLog with minimum retention of 60 days.'', 0); DECLARE @PurgeCutoff60Days DATETIME = DATEADD(DAY, -60, GETDATE()); DECLARE @PurgeCutoffConfigured DATETIME = DATEADD(DAY, -@DaysToKeep, GETDATE()); DECLARE @FinalPurgeCutoff DATETIME = CASE WHEN @PurgeCutoffConfigured < @PurgeCutoff60Days THEN @PurgeCutoffConfigured ELSE @PurgeCutoff60Days END; -- Purge watermark table DELETE FROM TLX_ROW_POLICY.TLX_INGESTION_WATERMARK WHERE last_ingested_at < @FinalPurgeCutoff; -- Purge audit log table DELETE FROM TLX_ROW_POLICY.AuditProcessLog WHERE LogTime < @FinalPurgeCutoff; -- Log completion INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Purge completed: Deleted data older than '' + CONVERT(NVARCHAR, @FinalPurgeCutoff, 120) + '' from TLX_INGESTION_WATERMARK and AuditProcessLog.'', 0); PRINT ''Process SP: TLX_Audit_MoveAndInsertAndPurge completed successfully.''; END TRY BEGIN CATCH INSERT INTO TLX_ROW_POLICY.AuditProcessLog (RunID, LogMessage, IsError) VALUES (@CurrentRunID, ''Error occurred: '' + ERROR_MESSAGE(), 1); PRINT ''An error occurred during the process.''; END CATCH END ') this is a sqlserver procedure that is enclosed as a string we use this As part of our DSPM. so as you can see that there is code block where we move files from source to destination and then read all the files in destination and store the data in table. basically we read .sqlaudit files and based on that we fill up the table in the server so that we can pull and do the DSPM stuff. There is a problem here we are doing all the files dump into the table using a single insert which is causing the server overload. so i want to do the process in batches. we will take batch size 'n' files from source and move to destination then complete the insert of those files and then will see if there are any files left in source and process will continue until the source folder is completely empty and the inserts are completed.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.