再次问一下关于where 中用变量 的小问题,谢谢大家了!
DECLARE @state int,@pname char(5)
DECLARE @tj1 char(200)
SET @state =23
set @tj1='CASE WHEN @state > 0 then ProductID else 0 end=CASE WHEN @state > 0 then @state else 0 end'
select ProductID,ProductName from Products where @tj1
我想直接取出@tj1 的值放在where 语句里面,怎么报错呢? 应该怎么做呢?谢谢大家了!辛苦您了!
Server: Msg 170, Level 15, State 1, Line 7
Line 7: Incorrect syntax near '0'.
Server: Msg 170, Level 15, State 1, Line 9
Line 9: Incorrect syntax near '@tj1'.
问题点数:30、回复次数:7Top
1 楼yixiu121(追求界面美观)回复于 2004-05-03 14:45:52 得分 0
Where 子句中带判断条件的我第一次看到,不知是不是出现在这个问题上?
Top
2 楼honey888(踏实做人)回复于 2004-05-08 09:35:29 得分 0
其实我就是想只用一句话,实现一条件查询相当于,如果@state 为0,就全部出来,否则就查询出来相关的。Top
3 楼lxcc()回复于 2004-05-08 09:43:23 得分 10
DECLARE @state int,@pname char(5)
if @state=0
begin
select ProductID,ProductName from Products
end
else
begin
select ProductID,ProductName from Products WHERE ProductID = @state
end
Top
4 楼wen0914xiang()回复于 2004-05-08 09:55:06 得分 0
用一句实现最好了!Top
5 楼tflantian(蓝天)回复于 2004-05-08 09:57:38 得分 0
支持lxcc(虫子)Top
6 楼zjcxc(邹建)回复于 2004-05-09 09:20:07 得分 20
DECLARE @state int,@pname char(5)
DECLARE @tj1 char(200)
SET @state =23
set @tj1='CASE WHEN @state > 0 then ProductID else 0 end=CASE WHEN @state > 0 then @state else 0 end'
exec('select ProductID,ProductName from Products where '+@tj1 )Top
7 楼yuaf(yuaf)回复于 2004-05-09 09:50:49 得分 0
不用这么麻烦的,只需要一句查询:
SELECT ProductID,ProductName FROM Products WHERE ProductID = @state
OR @state=0
当然,需要ProductID的值是大于0的Top




