Ошибка преобразования при преобразовании значения varchar в тип данных int
ПРИВЕТ,
Я пытаюсь отфильтровать представление, основанное на "кодклиента".Но я получаю вышеуказанную ошибку .
"Ошибка преобразования в тип varchar значение 'и VU_CRM_Customers.CustomerID=' для типа данных int."
Что я уже пробовал:
CREATE PROCEDURE SP_Customer(@FromDate date, @ToDate date,@AccessLevelCode nvarchar(30)=NULL,@UserID nvarchar(30)=NULL ,@UserOrgBranchID int=NULL,@UserGroupID int=NULL,@CustomerID int=0) AS BEGIN SET NOCOUNT ON; DECLARE @CUSTOMERIDS varchar(30)=null DECLARE @SQL nvarchar(max); If(@CustomerID IS NOT NULL) BEGIN SET @CUSTOMERIDS= 'AND VU_CRM_Customers.CustomerID='+@CustomerID END ELSE BEGIN SET @CUSTOMERIDS= '' END SET @SQL=N' DECLARE @Temp TABLE ( [Row] INT, Customer NVARCHAR(MAX), CustomerID int, TransactionType NVARCHAR(MAX), VoucherDate smalldatetime, VoucherNo NVARCHAR(MAX), AccountingDate datetime, Amount numeric(18,3), Debit numeric(18,3), Credit numeric(18,3), [OrgID] INT ); INSERT INTO @Temp SELECT ROW_NUMBER() OVER(PARTITION BY Customer ORDER BY CreatedDate) AS Row,* FROM (SELECT CASE WHEN ISNULL(ISNULL(Mobile,Phone),'''')='''' THEN Customer ELSE CONCAT(Customer,''('',ISNULL(Mobile,Phone),'')'') END AS Customer, ''Opening Balance'' AS TransactionType,AccountingDate AS VoucherDate,COALESCE(ACC_FinOpeningBalance.ReferenceNo, CONVERT(nvarchar,OpeningBalanceID)) AS VoucherNo,AccountingDate AS CreatedDate,CASE WHEN Dr>0 THEN Dr ELSE Cr END AS Amount,Dr AS Debit,Cr AS Credit,ACC_FinOpeningBalance.OrgID FROM ACC_FinOpeningBalance INNER JOIN VU_CRM_Customers ON ACC_FinOpeningBalance.SLGroupCode=''C'' AND ACC_FinOpeningBalance.SubledgerID=VU_CRM_Customers.CustomerID WHERE ACC_FinOpeningBalance.CreatedBy' +[dbo].[fnGetAccessFilter]( @AccessLevelCode,@UserID ,@UserOrgBranchID,@UserGroupID) + @CUSTOMERIDS + ' UNION SELECT CASE WHEN ISNULL(ISNULL(Mobile,Phone),'''')='''' THEN Customer ELSE CONCAT(Customer,''('',ISNULL(Mobile,Phone),'')'') END AS Customer, ''Advance'' AS TransactionType,AdvanceDate AS VoucherDate,COALESCE(ACC_AdvanceReceived.ReferenceNo, CONVERT(nvarchar,AdvanceReceivedID)) AS VoucherNo,AdvanceDate AS CreatedDate,NetReceivable AS Amount,NetReceivable AS Debit,0 AS Credit,ACC_AdvanceReceived.OrgID FROM ACC_AdvanceReceived INNER JOIN VU_CRM_Customers ON ACC_AdvanceReceived.AccountID=VU_CRM_Customers.CustomerID WHERE Customer IS NOT NULL AND ACC_AdvanceReceived.CreatedBy' +[dbo].[fnGetAccessFilter]( @AccessLevelCode,@UserID ,@UserOrgBranchID,@UserGroupID) + @CUSTOMERIDS+' SELECT *,(M.OpeningBalance+M.Debit-M.Credit) AS ClosingBalance FROM (SELECT L.Row,L.Customer,L.TransactionType,L.VoucherDate,L.VoucherNo,L.AccountingDate,L.Amount,L.OrgID, ISNULL((SELECT SUM(Debit)-SUM(Credit) FROM @Temp AS O WHERE O.AccountingDate<=L.AccountingDate AND O.Customer=L.Customer AND O.Row<L.Row),0) AS OpeningBalance, L.Debit,L.Credit FROM @Temp AS L WHERE L.AccountingDate BETWEEN '''+CONVERT(nvarchar,@FromDate)+''' AND DATEADD(minute,59,DATEADD(hour,23,CONVERT(datetime, '''+CONVERT(nvarchar,@ToDate)+''')))) AS M ;'; EXEC sp_executesql @SQL; print @SQL; END