首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

在过程中用like '%' 取数据可以,但是当用变量来代替'%'时取不出数据解决思路

2012-01-20 
在过程中用like % 取数据可以,但是当用变量来代替%时取不出数据ALTERproceduredbo.przcbnew(@nian1cha

在过程中用like '%' 取数据可以,但是当用变量来代替'%'时取不出数据
ALTER               procedure   dbo.przcbnew   (@nian1   char(4),@yue1   char(2),@ri1   char(2),@nian2   char(4),@yue2   char(2),@ri2   char(2),@dbm   char(4),
@chex   char(4),@cheh   char(7),@pinm   varchar(20),@pinl   char(4),@zydd   char(6),@daoz   char(9),@daoj   char(4)   )

as

select   @nian1=ltrim(rtrim(@nian1))
select   @yue1=ltrim(rtrim(@yue1))
select   @ri1=ltrim(rtrim(@ri1))
select   @nian2=ltrim(rtrim(@nian2))
select   @yue2=ltrim(rtrim(@yue2))
select   @ri2=ltrim(rtrim(@ri2))
select   @chex=ltrim(rtrim(@chex))
select   @cheh=ltrim(rtrim(@cheh))
select   @pinm=ltrim(rtrim(@pinm))
select   @daoz=ltrim(rtrim(@daoz))

if   len(@nian1)=0   select   @nian1= '1999 '
if   len(@nian2)=0   select   @nian2=   LTRIM(RTRIM(STR(year(GETDATE()))))
if   len(@yue1)=0   select   @yue1= '01 '
if   len(@yue2)=0   select   @yue2=   LTRIM(RTRIM(STR(month(GETDATE()))))
if   len(@ri1)=0   select   @ri1= '01 '
if   len(@ri2)=0   select   @ri2=   LTRIM(RTRIM(STR(DAY(GETDATE()))))
if   @dbm= '不填 '   select   @dbm= '% '
if   len(@chex)=0   select   @chex= '% '
if   len(@cheh)=0   select   @cheh= '% '
if   len(@pinm)=0   select   @pinm= '% '
if   @pinl= '不填 '   select   @pinl= '% '
if   @zydd= '不填 '   select   @zydd= '% '
if   @daoj= '不填 '   select   @daoj= '% '
if   len(@daoz)=0   select   @daoz= '% '

if   len(@yue1)=1   select   @yue1= '0 '+@yue1

if   len(@ri1)=1   select   @ri1= '0 '+@ri1

if   len(@yue2)=1   select   @yue2= '0 '+@yue2

if   len(@ri2)=1   select   @ri2= '0 '+@ri2

select   @nian1,@yue1,@ri1,@nian2,@yue2,@ri2,@dbm,@chex,@cheh   as   车号,@pinm,@pinl,@zydd,@daoz,@daoj
select  
    substring(a.riqixuhao,1,8),
    case   bb   when   'A '   then   '甲班 '
                      when   'B '   then   '乙班 '
                      when   'C '   then   '丙班 '
                      else   '丁班 '   end,
    cheh,     chex,     replace(fhr,char(10), ' '),     pinm,     pinl,     daoz,     daoj,
    shiz,     zydd,     kssj,     wlsj,     hyy,     zyb,dbm
from   zcb   a
where
            cast(substring(riqixuhao,1,8)   as   datetime)> =cast(@nian1+@yue1+@ri1   as   datetime)   and  
            cast(substring(riqixuhao,1,8)   as   datetime) <=cast(@nian2+@yue2+@ri2   as   datetime)   and  
            (dbm   like   @dbm   or   dbm   is   null)
            and   chex   like   @chex  


            and   cheh   like   @cheh
屏蔽and   cheh   like   @cheh就可以取出数据,加上就取不出任何数据,改成and   cheh   like   '% '也行   郁闷!请高手告知

[解决办法]
我记得LIKE不是这样写法的。
[解决办法]
and cheh like @cheh
这样写是不正确的
需要使用动态sql
例如;
declare @sql varchar(8000)
declare @chex varchar(20)
set @sql= 'select * from tablename where chex like '+char(39)+ '% '+@chex+ '% '+char(39)
exec(@sql)

热点排行
Bad Request.