1

我刚刚开始将编写存储过程添加到我的 SQL 曲目​​中,我确信这是一个菜鸟问题。有人可以指出我在这种情况下如何使用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

4 回答 4

2
exec dbo.MultipleDrugs N'707%'
于 2012-11-26T21:17:15.967 回答
1

你不需要LIKE在你的 exec 调用中提供操作员——你已经like在你的 SPROC 中了。考虑一个更简单的情况:

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

然后调用这个:

exec FindObjects 'sys%'     

返回

sysrscols
sysrowsets
sysallocunits
sysfiles1
syspriorities
sysfgfrag

等等

于 2012-11-26T21:19:30.173 回答
0

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

LIKE '%' + @condition + '%'
于 2012-11-26T21:14:19.840 回答
0

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

于 2014-04-08T11:57:50.953 回答