与存储过程中输入参数的运算符类似

时间:2012-11-26 21:10:21

标签: sql sql-server tsql stored-procedures

我刚刚开始在我的SQL保留节目中添加编写存储过程,我确信这是一个noob问题。在这种情况下,有人能指出我在正确的方向上如何使用like运算符而不是=运算符吗?这样做的目的是找到潜在的5位数字中的前3个公共数字

存储过程:

create proc dbo.MultipleDrugs (@condition varchar(50))
as
SELECT  *, 100.0 * round(SUM(numwithanxiety) OVER (partition BY sex) / cast(TotalSexCounts AS float), 4) overAllPercentByGender
FROM (SELECT x.sex, injurylevel, SUM(sexandlevelcounts) OVER (partition BY x.sex) numByInjury, sum(sexandlevelcounts) AS numWithAnxiety, 
      100.0 * round(cast(sexandlevelcounts AS float) / SUM(sexandlevelcounts) OVER (partition BY x.sex), 4) AS percentWith, y.TotalSexCounts
      FROM (SELECT sex, injurylevel, COUNT(*) AS sexAndLevelCounts
             FROM (SELECT DISTINCT m.patid, m.sex, m.injurylevel
              FROM  members AS m INNER JOIN
               icdClm AS ic ON ic.patid = m.PATID
               --*****when I leave this operator as like and use
               --*****equals in the exec statement it works
               WHERE ic.icd LIKE @condition) t
              GROUP BY sex, injurylevel) x INNER JOIN
              (SELECT m.sex, COUNT(DISTINCT patid) TotalSexCounts
                FROM members m
                GROUP BY m.sex) y ON y.sex = x.sex
               GROUP BY x.sex, x.injuryLevel, x.sexAndLevelCounts, y.TotalSexCounts) rr
 go

这会运行,但我无法使用like运算符

exec dbo.MultipleDrugs  N'70700'

这就是我想做的事情

exec dbo.MultipleDrugs  like '707%'

4 个答案:

答案 0 :(得分:2)

exec dbo.MultipleDrugs N'707%'

答案 1 :(得分:1)

您的exec调用中不需要提供LIKE运算符 - 您的SPROC中已经有like。请考虑更简单的情况:

create proc FindObjects @Name NVARCHAR(50)
as
    select * 
        from sys.objects
        where name like @Name

然后称之为:

exec FindObjects 'sys%'     

返回

sysrscols
sysrowsets
sysallocunits
sysfiles1
syspriorities
sysfgfrag

答案 2 :(得分:0)

更改您的SP,以便发生以下情况:

LIKE '%' + @condition + '%'

答案 3 :(得分:0)

您也可以在程序中使用这样的方法。 设置@ SearchString ='%'+ @ SearchString +'%'