存储过程中变量为空应当如何?
SET @sql_word= 'insert into '+@ResultTable +'
select distinct EXITSTATION,11,exitdate,RECORDTYPE,vehicleclass,totaltoll
from '+@BaseTable+'
where EXITSTATION = '+@Station+'
GROUP BY exitdate,EXITSTATION,RECORDTYPE,vehicleclass,totaltoll
order by EXITSTATION,exitdate,RECORDTYPE,vehicleclass,totaltoll'
print(@sql_word)
exec(@sql_word)
如果我的@Station 为空,这个代码便会报错,请问怎样表现才会让我@Station 变量为空时跳过这行?
[解决办法]
就这样改。再试试
if @Station <>''
begin
SET @sql_word= 'insert into '+@ResultTable +'
select distinct EXITSTATION,11,exitdate,RECORDTYPE,vehicleclass,totaltoll
from '+@BaseTable+'
where EXITSTATION = '+@Station+'
GROUP BY exitdate,EXITSTATION,RECORDTYPE,vehicleclass,totaltoll
order by EXITSTATION,exitdate,RECORDTYPE,vehicleclass,totaltoll'
end
[解决办法]
try this,
SET @sql_word= 'insert into '+@ResultTable
+' select distinct EXITSTATION,11,exitdate,RECORDTYPE,vehicleclass,totaltoll '
+' from '+@BaseTable
+' where 1=1 '+case when @Station<>'' then ' and EXITSTATION='''+@Station+''' ' else '' end
+' GROUP BY exitdate,EXITSTATION,RECORDTYPE,vehicleclass,totaltoll '
+' order by EXITSTATION,exitdate,RECORDTYPE,vehicleclass,totaltoll '
print(@sql_word)
exec(@sql_word)
[解决办法]
你这个不是null而是等于空的字符串。。。
改一下就可以了
SET @sql_word= 'insert into '+@ResultTable +'
select distinct EXITSTATION,11,exitdate,RECORDTYPE,vehicleclass,totaltoll
from '+@BaseTable+'
where EXITSTATION = '+ CASE ISNULL(@Station,'')
WHEN '' THEN 'EXITSTATION'
ELSE @Station END +'
GROUP BY exitdate,EXITSTATION,RECORDTYPE,vehicleclass,totaltoll
order by EXITSTATION,exitdate,RECORDTYPE,vehicleclass,totaltoll'