正常运行时只有要判断@pixes @pType两个参数,现在想再加一个参数@sort;
要是再用if...else if... else if ... else 那还要写多长啊?
救命啊..............
要是再用if...else if... else if ... else 那还要写多长啊?
救命啊..............
解决方案 »
- sql删除一条记录,提示超时已过期,是怎么回事?
- SQL语句: where tname='aa' or tname='bb' 和 where tname in ('aa','bb') 哪个效率高?
- 怎样实现sql server自动读取EXCEL中的记录插入其表中
- SQL 查询超时,怎样优化?
- 常量如何取得查询数值!!
- group by rollup 使用能指定列汇总小计吗?请教!
- 服务器上装了sql server,客户端怎样才能看到服务器上的数据库中的表
- 文本文件导入数据库的问题,急!
- 怎摸创建一个新的数据库?
- 数据更新的问题,谢谢!
- 存储过程怎么把表名做为一个参数数据,动态传递
- 有没有办法给每一行编号啊?
select count(pid) from m_pics
WHERE pPub=1
AND pType=isnull(@pType,pType)
AND pPixes=isnull(@pixes,pPixes)
AND ........你再加!
简单 CASE 函数:CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
ENDUSE pubs
GO
SELECT Category =
CASE type
WHEN 'popular_comp' THEN 'Popular Computing'
WHEN 'mod_cook' THEN 'Modern Cooking'
WHEN 'business' THEN 'Business'
WHEN 'psychology' THEN 'Psychology'
WHEN 'trad_cook' THEN 'Traditional Cooking'
ELSE 'Not yet categorized'
END,
CAST(title AS varchar(25)) AS 'Shortened Title',
price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
是不是这个意思?
sql语句中的ISNull(@var,yourRows)会自动判断参数情况,没有的话系统会不报错?
呵呵,先测试一下啊..........
case语句对应于同一条件的不同值,而我的应该是不同条件下的吧;好象不好用哦;
also tks (;-
如果没有参数进去,它的值就是NULL
where yourRows=ISNull(@var,yourRows)
--当@VAR IS NULL 相当于 where yourRows=yourRows 也就是1=1 条件永远成立.
--当@VAR is not NULL 相当于 where yourRows=@VAR
按照你的提示,我懂了点isull(@var,row)函数,以前的if..else..也优化了;
可现在还有点问题:
比如,我默认按id排序,怎样通过客户的请求在存储过程中改变排序方式呢?
这个问题应该简单吧,我做数据库开发很少,还是不会啊,怎么解决?这就是我存储过程中的@sort变量了。
还有就是isnull()函数对于,数字、时间、字符的操作都一样吗,要不要转换?(这次学就学完全点,(-0: )
像普通sql语句中的:
----------------------
数字 id=myVarInt
字符 temprow='+rowchars+'
---------------------------
有没有这个规则呢...........
insert into #pageindex(nid,sizeid)
select pid,pPixes from m_pics
WHERE pPub=1
AND pPixes=isnull(@pixes,pPixes)
AND pType=isnull(@pType,pType)
----------????????????????
--@sort
----------????????????????????......
(case when @pixes is null then '' else ' AND pPixes='+@pixes end )+
(case when @pType is null then '' else ' AND pPType='+@pType end )+
(case when @sort is null then '' else ' AND psort='+@sort end )
DROP PROCEDURE joyes_temp
GOCREATE procedure joyes_temp
@pixes Int=null,
@pType Int=null,
@sort varchar(50),
@pagesize int,
@pageindex int,
@docount bit
as
set nocount on
declare @tj nvarchar(1000),@s nvarchar(4000)
set @tj=' where pPub=1'
+case when @pixes is null then ''
else ' and pPixes=@pixes' end
+case when @pType is null then ''
else ' and pType=@pType' end
--如果还有其他字段,照这样的规律加上去就行了
if(@docount=1)
begin
set @s='SELECT count(pid) FROM [m_pics]'+@tj
exec sp_executesql @s
,N'@pixes Int,@pType'
,@pixes,@pType
end
else
begin
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize create table #pageindex(id int identity(1,1) not null,nid int,sizeid int) set @s='set rowcount @PageUpperBound
insert into #pageindex(nid,sizeid) select pid,pPixes from m_pics
'+@tj+' order by '+@sort
exec sp_executesql @s
,N'@pixes Int,@pType,@PageUpperBound int'
,@pixes,@pType,@PageUpperBound select O.pid,O.pTitle,O.pPath,O.pSize,m.name
from [m_pics] O,#pageindex p,[member] m
where O.pid=p.nid and p.id>@PageLowerBound and p.id<=@PageUpperBound
AND m.id=O.Uploader order by p.id
end
set nocount off
GO
WHERE 列名=ISNULL(变量,列名)
对于NULL值一样.2.我默认按id排序,怎样通过客户的请求在存储过程中改变排序方式呢?
动态SQL.由客户端传入ORDER BY字符串.
/*--------------------------十分感谢各位的帮助,虽然还有些其他问题:
就像动态sql语句中用 in (id1,id2,id3)语句等等.........
以后再请各位帮忙叻 现在得赶紧补C#,有兴趣可以跟我一起交流哦~~
------------------------*/(_+_):(_+_) 哈哈
"动态SQL语句中创建的临时表在存储过程中是无法执行的"
谢谢 记着了