传递varchar类型参数并在set @SQL中使用它

时间:2012-06-25 08:38:44

标签: sql sql-server

我有一行存储过程:

SET @SQL = 'SELECT path,title,tags
            FROM (
                  SELECT ROW_NUMBER() OVER(ORDER BY file_number) AS Row, *
                  FROM  files) AS tbl 
            WHERE file_number IN (SELECT tag_file_number 
                                  FROM tags 
                                  WHERE tag LIKE ' + @Conditions + '), Row >= '
                   + CONVERT(varchar(9), @StartIdx) + ' AND
                   Row <= ' + CONVERT(varchar(9), @EndIdx)

当我将'bus'作为参数值传递时,我收到错误。我的目标是传递一个SQL查询并将其替换为@conditions

我也可以传递'%bu%或标签像%time%或标签像%公交车时间%'我觉得这不难做但我简直无法弄明白。这是一个非常具体的问题,因此我甚至无法谷歌。请帮我。如果您需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

您错过了引号,请尝试这样:

SET @SQL = 'SELECT path,title,tags
        FROM (
        SELECT  ROW_NUMBER() OVER(ORDER BY file_number) AS Row, *
              FROM  files ) AS tbl WHERE file_number in(select tag_file_number from tags where tag like "' + @Conditions + '"), Row >= '
                            + CONVERT(varchar(9), @StartIdx) + ' AND
               Row <=  ' + CONVERT(varchar(9), @EndIdx)

现在发生的事情是where tag like bus,它必须是where tag like "bus"