将nvarchar数据类型从使用存储过程的select查询转换为Integer数据类型并将其插入到我的数据库中时出现此问题;
我已经多次在互联网上搜索如何解决我的问题但是,我找到的代码无法解决我的问题。
这是我的代码,
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
每次执行代码时,都会出现此错误,
“无法转换nvarchar数据类型'SELECT Count(FEESList。[费用 说明])AS FROM FEESList INNER JOIN TESTFeeSETUP ON FEESList.FeeID = TESTFeeSETUP.FeeID INNER JOIN TESTFeeSETUPSubTable ON TESTFeeSETUP。[费用规格] = TESTFeeSETUPSubTable。[规格 ID] WHERE TESTFeeSETUPSubTable.Specification in('''+ @LabSubject + ''')'转换为整数数据类型“
我是编程新手,所以我真的需要一些帮助。感谢
答案 0 :(得分:0)
如果您执行以下操作,这对您来说更容易:
例如(SQL Server 2016)
DECLARE @LabSubjectTable TABLE(Value NVARCHAR(50))
INSERT INTO @LabSubjectTable
SELECT value FROM STRING_SPLIT(@LabSubject, ',')
SELECT Distinct(TESTFeeSETUP.[Fee Code]) AS FeeCode
FROM TESTFeeSETUP AS t1
INNER JOIN TESTFeeSETUPSubTable ON TESTFeeSETUP.[Fee Specification] = TESTFeeSETUPSubTable.[Specification ID]
WHERE TESTFeeSETUPSubTable.Specification in (SELECT value FROM @LabSubjectTable)
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 (SELECT value FROM @LabSubjectTable)
SELECT Distinct(TESTFeeSETUP.[Amount/Cost]) AS AmountCost
FROM TESTFeeSETUP INNER JOIN TESTFeeSETUPSubTable ON TESTFeeSETUP.[Fee Specification] = TESTFeeSETUPSubTable.[Specification ID]
WHERE TESTFeeSETUPSubTable.Specification in (SELECT value FROM @LabSubjectTable)
更新: