0

在我的存储过程中,我面临以下问题 - 我该如何解决这个问题?

CREATE PROC RupeshTest(@QueID int, @Answer varchar(250)) as
BEGIN
   DECLARE @STR varchar(4000) 

   SELECT @STR = COALESCE(@str + ';','') + AnswerText     
   FROM SurveyQuestionAnswerTypes 
   WHERE AnswerType = (SELECT AnswerType 
                       FROM SurveyQuestions 
                       WHERE QuestionID = CAST(@QueId AS VARCHAR(4))) 
                         AND AnswerValue IN (REPLACE(@Answer,'^',','))
END

当我运行它

RupeshTest 25, '2^3^5^7^8' 

我收到以下错误

消息 245,级别 16,状态 1,过程 RupeshTest,第 4 行将
varchar 值“2,3,5,7,8”转换为数据类型 int 时转换失败。

虽然我理解这个问题,但无法解决它,有人可以告诉我如何解决它。

4

3 回答 3

2

这不起作用,因为最终形成的查询是:

SELECT @STR = COALESCE(@str + ';','') + AnswerText     
   FROM SurveyQuestionAnswerTypes 
   WHERE AnswerType = (SELECT AnswerType 
                       FROM SurveyQuestions 
                       WHERE QuestionID = CAST(@QueId AS VARCHAR(4))) 
                         AND AnswerValue IN ('2,3,5,7,8')

你想要这样的东西:

SELECT @STR = COALESCE(@str + ';','') + AnswerText     
   FROM SurveyQuestionAnswerTypes 
   WHERE AnswerType = (SELECT AnswerType 
                       FROM SurveyQuestions 
                       WHERE QuestionID = CAST(@QueId AS VARCHAR(4))) 
                         AND AnswerValue IN (2,3,5,7,8)

我建议您制作一个 UDF String_To_Table,将字符串传递给该 UDF 并用于 IN 子句。

String_To_Table UDF:

CREATE FUNCTION [dbo].[String_To_Table]
                    (@string      VARCHAR(4000),
                     @delimiter CHAR(1) = ';')
         RETURNS @tbl TABLE (ord INT IDENTITY(1, 1) NOT NULL,
                             token     VARCHAR(500)) AS

   BEGIN
      DECLARE @pos      int,
              @textpos  int,
              @chunklen smallint,
              @tmpstr   varchar(4000),
              @leftover varchar(4000),
              @tmpval   varchar(4000)

      SET @textpos = 1
      SET @leftover = ''
      WHILE @textpos <= datalength(@string)
      BEGIN
         SET @chunklen = 4000 - datalength(@leftover)
         SET @tmpstr = @leftover + substring(@string, @textpos, @chunklen)
         SET @textpos = @textpos + @chunklen

         SET @pos = charindex(@delimiter, @tmpstr)

         WHILE @pos > 0
         BEGIN
            SET @tmpval = ltrim(rtrim(left(@tmpstr, @pos - 1)))
            INSERT @tbl (token) VALUES(@tmpval)
            SET @tmpstr = substring(@tmpstr, @pos + 1, len(@tmpstr))
            SET @pos = charindex(@delimiter, @tmpstr)
         END

         SET @leftover = @tmpstr
      END

    IF ltrim(rtrim(@leftover)) <> ''
        INSERT @tbl(token) VALUES (ltrim(rtrim(@leftover)))


 RETURN
   END

然后你可以改变你的SP如下:

CREATE PROC RupeshTest(@QueID int, @Answer varchar(250)) as
BEGIN
   DECLARE @STR varchar(4000) 

   SELECT @STR = COALESCE(@str + ';','') + AnswerText     
   FROM SurveyQuestionAnswerTypes 
   WHERE AnswerType = (SELECT AnswerType 
                       FROM SurveyQuestions 
                       WHERE QuestionID = CAST(@QueId AS VARCHAR(4))) 
                         AND AnswerValue IN (SELECT Token FROM dbo.String_To_Table(@Answer,'^'))
END
于 2012-05-09T07:01:43.760 回答
1

您在这里面临的问题是部分 AnswerValue IN (2,3,5,7,8)以及如何使其有效 SQL,因为这些值是整数列表,并且您将它们作为字符串。对我来说,一个技巧是将所有 SQL 命令变成一个字符串并执行它:

DECLARE @SQLQuery AS varchar(4000)

SET SQLQuery = 
'SELECT AnswerText '
+ ' FROM SurveyQuestionAnswerTypes '
+ ' WHERE AnswerType = (SELECT AnswerType '
                   + ' FROM SurveyQuestions '
                   + ' WHERE QuestionID = ' + CAST(@QueId AS VARCHAR(4))) 
                    + ' AND AnswerValue IN (' + (REPLACE(@Answer,'^',',')) + ')'


EXECUTE(@SQLQuery)                  

顺便说一句,没有理由用 ^ 发送数字然后更改它。

于 2012-05-09T07:07:02.527 回答
0

您可以将最后一个条件替换为

AND CHARINDEX(CONCAT("^", CAST(AnswerValue AS CHAR), "^"), CONCAT("^", @Answer, "^"))

这似乎并不完全正确,但它可能会奏效。

已编辑

感谢您的更正。

于 2012-05-09T07:08:59.337 回答