Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- USE [ntl-7.16.1]
- GO
- /****** Object: StoredProcedure [dbo].[tsp_FindAccountDuplicateByName] Script Date: 09.07.2020 10:26:21 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- ALTER PROCEDURE [dbo].[tsp_FindAccountDuplicateByName] (
- @parsedConfig CreatingObjectInfo READONLY,
- @sysAdminUnit UNIQUEIDENTIFIER,
- @ruleId UNIQUEIDENTIFIER
- )
- AS
- BEGIN
- DECLARE @parsedConfigRowsCount INT = (SELECT COUNT(*) FROM @parsedConfig);
- IF OBJECT_ID('tempdb..#searchAccount') IS NOT NULL
- BEGIN
- DROP TABLE #searchAccount
- END
- CREATE TABLE #searchAccount (
- [Name] NVARCHAR(128) COLLATE database_default,
- [SortDate] DATETIME
- );
- CREATE NONCLUSTERED INDEX IX_#searchAccount_NameSortDate ON #searchAccount([Name], [SortDate]);
- IF @parsedConfigRowsCount = 0
- BEGIN
- INSERT INTO #searchAccount ([Name], [SortDate])
- SELECT
- [dedup].[Name],
- MAX([dedup].[SortDate]) [SortDate]
- FROM (
- SELECT [Id],
- [Name] AS [Name],
- MAX([ModifiedOn]) [SortDate]
- FROM [VwAccountCleanDataValues] WITH (NOEXPAND)
- GROUP BY [Id], [Name]
- ) AS [dedup]
- GROUP BY [dedup].[Name]
- HAVING COUNT(*) > 1;
- END;
- ELSE
- BEGIN
- INSERT INTO #searchAccount ([Name], [SortDate])
- SELECT
- [dbo].[fn_NormalizeString]([Name], N'0-9a-zа-яא-ת') AS [Name],
- GETDATE() AS [SortDate]
- FROM @parsedConfig
- END;
- INSERT INTO [AccountDuplicateSearchResult] ([AccountId], [GroupId], [RuleId], [SysAdminUnitId])
- SELECT
- [vr].[Id],
- DENSE_RANK() OVER (ORDER BY [vr].[SortDate] DESC, [vr].[Name] ASC),
- @ruleId [RuleId],
- @sysAdminUnit
- FROM (
- SELECT
- [v].[Id],
- [v].[Name],
- [r].[SortDate]
- FROM (
- SELECT [Id], [ModifiedOn], [Name]
- FROM [VwAccountCleanDataValues] WITH (NOEXPAND)
- ) [v], #searchAccount [r]
- WHERE [v].[Name] = [r].[Name]
- GROUP BY [v].[Id], [v].[Name], [r].[SortDate]
- ) [vr];
- END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement