I had this problem on converting nvarchar datatype from a select query using stored procedure into Integer datatype and insert it into my database;
I already search the internet many times on how to solve my problem but, the codes i found was not able to solve my problem.
This is my code,
USE [SLCBRegistrarDB]
GO
/****** Object: StoredProcedure [dbo].[CountLabfees] Script Date:
10/30/2017 7:06:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[CountLabfees]
@LabSubject nvarchar(50)
as
BEGIN
SET NOCOUNT ON;
Declare @SQL NVARCHAr(MAX)
SET @LabSubject = REPLACE(@LabSubject,',',''',''')
SET @SQL='SELECT Distinct(TESTFeeSETUP.[Fee Code])
FROM TESTFeeSETUP INNER JOIN TESTFeeSETUPSubTable ON TESTFeeSETUP.[Fee
Specification] = TESTFeeSETUPSubTable.[Specification ID]
WHERE TESTFeeSETUPSubTable.Specification in (''' + @LabSubject + ''')'
Declare @Quantity NVARCHAr(MAX)
SET @Quantity='SELECT Count(FEESList.[Fee Description]) AS Quantity
FROM FEESList INNER JOIN TESTFeeSETUP ON FEESList.FeeID =
TESTFeeSETUP.FeeID INNER JOIN TESTFeeSETUPSubTable ON TESTFeeSETUP.[Fee
Specification] = TESTFeeSETUPSubTable.[Specification ID]
WHERE TESTFeeSETUPSubTable.Specification in (''' + @LabSubject + ''')'
Declare @Total NVARCHAr(MAX)
SET @Total='SELECT Distinct(TESTFeeSETUP.[Amount/Cost])
FROM TESTFeeSETUP INNER JOIN TESTFeeSETUPSubTable ON TESTFeeSETUP.[Fee
Specification] = TESTFeeSETUPSubTable.[Specification ID]
WHERE TESTFeeSETUPSubTable.Specification in (''' + @LabSubject + ''')'
EXEC (@SQL)
EXEC (@Total)
EXEC (@Quantity)
END
BEGIN
INSERT INTO FeesStudentBILLING VALUES(10,@SQL,@Quantity,@Total,1,1)
END
Everytime I execute the code, this error always appears,
"Cannot convert nvarchar datatype 'SELECT Count(FEESList.[Fee Description]) AS Quantity FROM FEESList INNER JOIN TESTFeeSETUP ON FEESList.FeeID = TESTFeeSETUP.FeeID INNER JOIN TESTFeeSETUPSubTable ON TESTFeeSETUP.[Fee Specification] = TESTFeeSETUPSubTable.[Specification ID] WHERE TESTFeeSETUPSubTable.Specification in (''' + @LabSubject + ''')' into integer datatype"
I am new to programming, so I really need some help on this please. thanks