sql server2000数据库单表查询问题,存储过程的sql语句,会者不难(20分)

  • 主题发起人 主题发起人 nywjx
  • 开始时间 开始时间
N

nywjx

Unregistered / Unconfirmed
GUEST, unregistred user!
SQL Server2000中,一个表:历史记录表
包括字段:卡号、起始日期时间、截止日期时间三个字段。
现在我想在应用程序中给用户输入卡号、调整起始日期时间和截止日期时间,
其中,如果用户不输入卡号,就表示用户想查询在起始截止日期时间之间的所有记录,
如果输入卡号,就表示用户想查询在起始截止日期时间之间的该卡号的所有记录。
可是我的存储过程怎么写呢?要根据用户输入不输入卡号,存储过程的内容不一样,
怎么写呢?
 
SELECT * FROM TName
WHERE 卡号 LIKE ':p_卡号%'
...
 
什么意思?能解释一下吗?
 
if @ic>0
set @str='select * from tablename where ic= '+@ic+'rq between .. '
else
set @str='select * from tablename where rq between....'
exec(@str)
 
if @卡号=''
begin
select * from 历史记录表
end else
begin
select * from 历史记录表
where 卡号=@卡号
end
 
xianguo的意思很简单呀,就是查找卡号与输入的卡号相似的所有记录,模糊查询你不懂吗?
 
if 输入卡号 then
select * from tablename where (date<截止日期)and(date>起始日期)and(卡号=你输入的卡号)
else if 不输入卡号 then
select * from tablename where (date<截止日期)and(date>起始日期)
 
to:pw201
我不是要模糊查询啊。

我其他几位回答得不错,可是你们想过没有,如果字段再多点,怎么办?还用if?
 
CREATE PROCEDURE 查询固定卡
@CardNo int=null,
@BeginingDate datetime,
@EndingDate datetime
AS
if @CardNo is null
select * from 固定卡有效期表
where Date between @BeginningDate and @EndingDate
else
select * from 固定卡有效期表
where Date between @BeginningDate and @EndingDate
and CardNo=@CardNO
GO

 
可是如果再多一个字段 @Operator,怎么办?
即:表中字段:卡号、起始日期时间、截止日期时间、操作者四个字段。
怎么写呢?
总不能一直用if。。。else吧
 
你要是综合判断就是and
要是一个一个判断就用if...else吧
 
LeeChange说得很对。
多一个 @Operator 字段是什么意思?
 
我的意思是,如果有两个参数的话,你可以用
if 参数1=null
if 参数2=null
...
else ...
else
if 参数2=null
...
else ...
这样。
那么如果 有三个参数,怎么办?if else嵌套下去?
 
多人接受答案了。
 
后退
顶部