在存储过程中由IN运算符导致的数据类型nvarchar转换为bigint时出错

时间:2015-02-18 07:53:21

标签: sql-server stored-procedures nvarchar in-operator

存储过程:

ALTER PROCEDURE [dbo].[MyProcedure]
    @CommaSeperatedValues nvarchar(500)
AS
BEGIN
    SET NOCOUNT ON;
    SELECT  Col1, Col2, Col3
    FROM MyTable
    WHERE SomeCol_BigIntDataType IN (@CommaSeperatedValues) 

来自Code的值是字符串值:“9010073,9010074”

我尝试像这样运行SP:exec MyProcedure'9010073,9010074'

这是在运行SP

时出现错误'将数据类型nvarchar转换为bigint时出错“

如果我单独运行select查询,请执行以下操作:

SELECT  Col1, Col2, Col3
    FROM MyTable
    WHERE SomeCol_BigIntDataType IN (9010073,9010074)

然后我得到了预期的结果。

但我想从SP运行。

3 个答案:

答案 0 :(得分:5)

我们走了。您可以通过动态查询

执行此操作
DECLARE @mystring NVARCHAR(max)
DECLARE @UserId  NVARCHAR(max)
SET @UserId = '9010073,9010074'
SELECT @mystring = 'SELECT  Col1, Col2, Col3 FROM MyTable where UserId IN('+ @UserId +')'
EXEC sp_executesql @mystring

答案 1 :(得分:2)

在数据库中创建以下表格函数作为SplitString。当你想选择项目时;用这个:

Select Part from SplitString(@YourValues, ',')

您的查询中使用了以上的select语句。

SELECT  Col1, Col2, Col3
    FROM MyTable
    WHERE SomeCol_BigIntDataType IN (Select Part from SplitString(@CommaSeperatedValues, ',')) 

下面的函数在您的数据库中执行一次。

CREATE FUNCTION [dbo].[SplitString]
(
     -- Add the parameters for the function here
     @myString varchar(500),
     @deliminator varchar(10)
)
RETURNS
@ReturnTable TABLE
(
     -- Add the column definitions for the TABLE variable here
     [id] [int] IDENTITY(1,1) NOT NULL,
     [part] [varchar](50) NULL
)
AS
BEGIN
         Declare @iSpaces int
         Declare @part varchar(50)

         --initialize spaces
         Select @iSpaces = charindex(@deliminator,@myString,0)
         While @iSpaces > 0

         Begin
             Select @part = 
substring(@myString,0,charindex(@deliminator,@myString,0))

             Insert Into @ReturnTable(part)
             Select @part

     Select @myString = 
substring(@mystring,charindex(@deliminator,@myString,0)+ 
len(@deliminator),len(@myString) - charindex(' ',@myString,0))


             Select @iSpaces = charindex(@deliminator,@myString,0)
         end

         If len(@myString) > 0
             Insert Into @ReturnTable
             Select @myString

     RETURN
END

答案 2 :(得分:1)

您需要具有将逗号分隔的字符串拆分为单独行的函数。然后你调用这个函数:

SELECT  
    Col1, Col2, Col3
FROM MyTable
WHERE 
    SomeCol_BigIntDataType IN (
        SELECT CAST(Item AS BIGINT) 
        FROM dbo.[DelimitedSplitN4K](@CommaSeperatedValues, ',')
)

以下是Jeff Moden的DelimitedSplitN4K函数。有关详细信息:http://www.sqlservercentral.com/articles/Tally+Table/72993/

CREATE FUNCTION [dbo].[DelimitedSplitN4K](
    @pString NVARCHAR(4000), @pDelimiter NCHAR(1)
)
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
WITH E1(N) AS (
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
)
,E2(N) AS (SELECT 1 FROM E1 a, E1 b)
,E4(N) AS (SELECT 1 FROM E2 a, E2 b)
,cteTally(N) AS(
    SELECT TOP (ISNULL(DATALENGTH(@pString)/2,0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
)
,cteStart(N1) AS(
    SELECT 1 UNION ALL 
    SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
),
cteLen(N1,L1) AS(
SELECT 
    s.N1,
    ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,4000)
FROM cteStart s
)
SELECT 
    ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
    Item       = SUBSTRING(@pString, l.N1, l.L1)
FROM cteLen l
;
GO

免责声明:评论已从该功能中删除,格式已被修改。