如何在存储过程里生成where语句
这样的存储过程如何写
CREATE PROCEDURE sp_Forums_GetTopics
@ForumID int,
@Subject varchar(50),
@MemberName varchar(50),
@AddedDate datetime
AS
DECLARE @strwhere varchar(500)
set @strwhere='(1=1)'
if @ForumID is not null
begin
SET @strwhere=' and '+@strwhere+'ForumID = '+@ForumID
end
if @Subject is not null
begin
SET @strwhere=' and '+@strwhere+'Subject='+@Subject
end
if @MemberName is not null
begin
SET @strwhere=' and '+@strwhere+'MemberName='+@MemberName
end
if @AddedDate is not null
begin
SET @strwhere=' and '+@strwhere+'AddedDate='+@AddedDate
end
SELECT TopicID, Subject, AddedDate, TopicReplies, TopicLastReplyDate, TopicLastPostDate, MemberID, MemberName, Email, ShowEmail
FROM v_Forums_Topics WHERE @strwhere ORDER BY TopicLastPostDate Desc
GO
解析抱错:在关键字 'ORDER' 附近有语法错误。
请问那么如何根据传进来的变量,实现动态得到where语句?
CREATE PROCEDURE sp_Forums_GetTopics
@ForumID int,
@Subject varchar(50),
@MemberName varchar(50),
@AddedDate datetime
AS
DECLARE @strwhere varchar(500)
set @strwhere='(1=1)'
if @ForumID is not null
begin
SET @strwhere=' and '+@strwhere+'ForumID = '+@ForumID
end
if @Subject is not null
begin
SET @strwhere=' and '+@strwhere+'Subject='+@Subject
end
if @MemberName is not null
begin
SET @strwhere=' and '+@strwhere+'MemberName='+@MemberName
end
if @AddedDate is not null
begin
SET @strwhere=' and '+@strwhere+'AddedDate='+@AddedDate
end
SELECT TopicID, Subject, AddedDate, TopicReplies, TopicLastReplyDate, TopicLastPostDate, MemberID, MemberName, Email, ShowEmail
FROM v_Forums_Topics WHERE @strwhere ORDER BY TopicLastPostDate Desc
GO
解析抱错:在关键字 'ORDER' 附近有语法错误。
请问那么如何根据传进来的变量,实现动态得到where语句?
...全文
相关推荐
MySQL存储过程中实现执行动态SQL语句的方法
主要介绍了MySQL存储过程中实现执行动态SQL语句的方法,实例分析了MySQL中构造及执行动态SQL语句的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
基于MATLAB的IBFS最大流/最小割算法封装实现
最大流问题的核心是寻找在有向图中,从源点到汇点的最大流量。最小割问题是最大流问题的对偶问题,它寻求最小化边割集,以断开图中所有路径,使源点和汇点不再连通。两者在理论和实践中都有广泛的应用,如物流运输、电路设计、网络流量控制等。最大流/最小割问题不仅是图论中的经典问题,也是网络科学和运筹学的重要研究对象。随着研究的深入,出现了多种高效的算法,如Ford-Fulkerson方法、Dinic算法等,这些算法不仅在理论上具有重要意义,而且在实际中得到了广泛应用。
SQL语句创建存储过程
创建数据库的存储过程 要求: 实现增,删,查,改!!
mysql存储过程之if语句用法实例详解
主要介绍了mysql存储过程之if语句用法,结合实例形式详细分析了mysql存储过程中if语句相关原理、使用技巧与操作注意事项,需要的朋友可以参考下
sqlserver的存储过程与 where in 多值参数
自己总结的 关于sqlserver存储过程与多值参数的解决办法。


