从选择查询存储过程

时间:2017-10-30 13:28:14

标签: sql-server vb.net

将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 +   ''')'转换为整数数据类型“

我是编程新手,所以我真的需要一些帮助。感谢

1 个答案:

答案 0 :(得分:0)

如果您执行以下操作,这对您来说更容易:

  1. 使用拆分字符串函数从@LabSubject获取值,然后将它们存储在本地变量表中。
  2. 在条件查询中使用步骤1中的局部变量表。
  3. 例如(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)
    

    更新:

    • 我想你现在可以得到这些想法。基于此,您可以根据您的逻辑进行更新。
    • 如果您的SQL Server不支持内置拆分字符串功能,则可以创建一个T-SQL split string