rewrite below without dynamic sql string CREATE Procedure…
- User
rewrite below without dynamic sql string
CREATE Procedure [Barrington].[ContractorSearchByPage] ( @CompanyId uniqueidentifier, @Status nvarchar(100), @Search nvarchar(100), @PageNumber int, @PageSize int, @Alphabet nvarchar(10), @ManagedBy uniqueidentifier, @CompliantStatus nvarchar(100), @TradeName nvarchar(100)='', @LocationId uniqueidentifier ) AS
BEGIN DECLARE @SQL nvarchar(max);
codeSET @SQL = 'select Distinct CC.Id as ContractorCompanyId, C.Id as ContractorId, C.Name as ContractorName, C.LegalName as ContractorLegalName, CC.RedeemCode, CC.CompanyId as CustomerId, CO.Name as CustomerName, CO.Phone as CustomerPhone, CO.Email as CustomerEmail, C.ABN, CC.StartDate, CC.EndDate, CC.IsPrimary, CC.LinkType, CC.LinkModel, dbo.fn_GetAddress(C.Id, ''CompanyAddress'', C.Id) as CompanyAddress, dbo.fn_GetAddress(C.Id, ''CompanyMailAddress'', C.Id) as MailingCompanyAddress, C.Phone, C.Email, C.Website, C.TimeZoneOffset, CC.Relationship, CC.Status AS [Status], CC.StatusChangeDate, CC.CompliantStatus, CC.CompliantStatusDate, CC.CreatedDate,
codeC.IsSoleTrader, [dbo].[fn_GetCompanyRole] (CC.ContractorId, CC.CompanyId) as DisplayCompanyTradeRole, CO.Status as CompanyStatus, dbo.fn_GetManagedBy(CC.Id) as ManagedByName, Count(*) over (partition by CC.CompanyId) AS TotalCountFrom Barrington.Company C inner join Barrington.CompanyContractor CC on CC.ContractorId = C.Id inner join Barrington.Company CO on CO.Id = CC.CompanyId ';
codeIF @LocationId != 0x0 BEGIN SET @SQL = @SQL + ' INNER JOIN [dbo].[vwContractorAccessLocation] VCAL ON CC.ContractorId = VCAL.ContractorId AND VCAL.LocationId = @LocationId AND VCAL.status = ''Approved'' '; END SET @SQL = @SQL + ' left join Barrington.ContactAddress CA on CA.ReferenceId = CC.Id and CA.IsDeleted = 0where (CC.Relationship = ''Contractor'' or CC.Relationship = ''Supplier'') and CC.CompanyId = @CompanyId AND ((SUBSTRING(C.Name, 1, 1) = @Alphabet) OR @Alphabet = ''All'') and (@CompliantStatus =''All'' OR CC.CompliantStatus = @CompliantStatus) and (@ManagedBy = 0x0 OR CA.EmployeeId = @ManagedBy) and (@Status =''All'' OR CC.Status = @Status) and C.IsDeleted = 0 and CC.IsDeleted = 0 and CO.IsDeleted = 0 and C.Status != ''Archived'' and CO.Status != ''Archived''
- Assistant