如果在fromdate和todate之间的日期时间字段传递空值,如何返回存储过程中的所有记录。 [英] How to Return all records in stored procedure if null value passed for a date time field in between fromdate and todate..

查看:133
本文介绍了如果在fromdate和todate之间的日期时间字段传递空值,如何返回存储过程中的所有记录。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在fromdate和todate之间的日期时间字段传递空值,如何返回存储过程中的所有记录。

How to Return all records in stored procedure if null value passed for a date time field in between fromdate and todate..

推荐答案





仅限简单....检查谷歌中的逻辑运营商(OR,AND)。

我给了样品程序......

Hi,

Its Simple only.... check Logical Operators (OR, AND) in Google.
I gave Sample Procedure...
CREATE PROCEDURE test
@StartDate DATETIME=NULL,
@EndDate DATETIME=NULL
AS
BEGIN
	SELECT Column_List
	FROM Table_Name
	WHERE (Date_Field BETWEEN @StartDate AND @EndDate) OR (@StartDate IS NULL AND @EndDate IS NULL)
	-- If Input is having some values or NULL
	-- @StartDate and @EndDate having values or both the Inputs must NULL.	 
END



问候,

GVPrabu


Regards,
GVPrabu


解决方案1由 gvprabu [ ^ ]非常好。另一种解决方案是使用动态查询。



在存储过程中构建动态SQL [ ^ ]

http://www.sqlteam.com/article/using-dynamic-sql-in-stored-procedures [ ^ ]

http://www.sommarskog.se/dynamic_sql.html [ ^ ]
Solution1 by gvprabu[^] is very good. Another solution is to use dynamic queries.

Building Dynamic SQL In a Stored Procedure[^]
http://www.sqlteam.com/article/using-dynamic-sql-in-stored-procedures[^]
http://www.sommarskog.se/dynamic_sql.html[^]


这篇关于如果在fromdate和todate之间的日期时间字段传递空值,如何返回存储过程中的所有记录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆