+ -
当前位置:首页 → 问答吧 → MSSQL 条件是变量的 存过过程该怎么写?

MSSQL 条件是变量的 存过过程该怎么写?

时间:2010-08-22

来源:互联网

StrWhere=" StateFlag=2"  '默认  然后后面跟好多条件判断。

IF ispic<>"0" Then     '还有其他 很多个条件。
StrWhere=StrWhere&" And pic<>'' "
End IF


Rsc=conn.execute("up_Statistics '"&StrWhere&"'")

存储过程 ,已经简化了。


REATE PROCEDURE up_Statistics
@StrWhere varchar(50)
AS
declare @Price int,@ReturnStr varchar(100)

select  @Price=count(*)  from  news  where @strWhere   ---这句 怎么写都是错误的 “下面还需要返回好几个数据结果”
select  @Price1=count(*)  from.............
select  @Price2=count(*)  from............

select @ReturnStr=Ltrim(str(@Price))  '这里吧 Price,Price1,Price2....拼接出来。输出去!这样我在ASP页面就可以直接调用
select @ReturnStr
GO

[ 本帖最后由 ldwz 于 2010-8-23 08:51 编辑 ]

作者: ldwz   发布时间: 2010-08-22

select  @Price=count(*)  from  news  where @strWhere

提示 错误156 关键字 select 有错误!把后面的  where @strWhere 去掉。就没问题。

作者: ldwz   发布时间: 2010-08-22

存储过程中判断,或者用 sp_executesql

if ispic <> 0
    select  @Price = count(*) from  news  where StateFlag = 2 and pic <> ''
eles
    select  @Price = count(*) from  news  where StateFlag = 2

作者: KarasCanvas   发布时间: 2010-08-23

引用:
原帖由 KarasCanvas 于 2010-8-23 07:31 发表
存储过程中判断,或者用 sp_executesql

if ispic  0
    select  @Price = count(*) from  news  where StateFlag = 2 and pic  ''
eles
    select  @Price = count(*) from  news  where StateFlag = 2
大哥。我上面已经说了(而且你这种写法要是可以的话。我就不问了!)。。有很多条件。而且条件都是(StrWhere=StrWhere&" ")累加。。
你这样写。 10多个条件。来回组合。那不是要写很多条!
sp_executesql 不知道怎么写!

[ 本帖最后由 ldwz 于 2010-8-23 08:59 编辑 ]

作者: ldwz   发布时间: 2010-08-23

sp_executesql 在SQLServer自带的帮助文件里有详细说明,MSDN上也有。
http://msdn.microsoft.com/zh-cn/library/ms188001.aspx

即使是动态拼接SQL,也建议在存储过程中实现拼接逻辑,要不存储过程没起到实际意义,也增加了维护难度。

作者: KarasCanvas   发布时间: 2010-08-23

最近正在研究 这个

作者: ylhua   发布时间: 2010-08-26

我是没研究明白!

作者: ldwz   发布时间: 2010-08-27

热门下载

更多