SQL - 不正确的语法 - 使用带斜杠的字符串作为变量

时间:2014-09-29 20:34:40

标签: sql sql-server

尝试进行此查询,但语法不正确,不确定原因。斜杠是否需要转义?

with x as (
    select
        job_id,
        avg_runtime,
        id,
        row_number() over (partition by ja.job_id order by ja.id desc) rn
    from
        job_activity as ja 
            join 
        job as j 
            on ja.job_id = j.id 
    where
        j.name in ('/THE/Name/goes/here')
        and
        ja.avg_runtime <> 0 and
        ja.avg_runtime is not null        
) select
    job_id,
    avg_runtime,
    id
from
    x
where
    rn = 1;

1 个答案:

答案 0 :(得分:0)

您的查询应按原样运行。你使用斜线作为占位符吗?如果是这样,您可能想尝试这个:

Declare @JobToFind VarChar(30)

Set @jobtofind = 'Search string (job name)'--modify the value in the quotes.

with x as (
    select
        job_id,
        avg_runtime,
        j.ID,
        row_number() over (partition by ja.job_id order by ja.id desc) rn
    from
        job_activity as ja 
            join 
        job as j 
            on ja.job_id = j.id 
    where
        j.name =@JobToFind
        and
        ja.avg_runtime <> 0 and
        ja.avg_runtime is not null        
) select
    job_id,
    avg_runtime,
    id
from
    x
where
    rn = 1;