求一存储过程写法

骑猪看海 2010-07-30 05:43:08

CREATE PROCEDURE [dbo].[sp_Users_List]
@userName nvarchar(50),
@realName nvarchar(50)
AS
BEGIN
SELECT top 10 * FROM Users where1=1 ? and id not in(select top 0 from Users where1=1 ?)

@userName 和 @realName是 查询条件

本来应该这样写的:
SELECT top 10 * FROM Users where1=1 and userName=@userName and realName=@realName and id not in(select top 0 from Users where and userName=@userName and realName=@realName)

但有时传进来的可能是空,所以不能写死,值为空时and条件应该去除,不然就找不出数据,应该怎么写。
以前一直不用存储过程,直接在程序中用字符串把条件拼起来的,现在用下就不知道怎么做了,各位看看怎么应该做
...全文
202 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
骑猪看海 2010-07-30
  • 打赏
  • 举报
回复
上面的都是高手,明天试下。看来自己真应该学习下存储过程,写了半年多程序,全是在程序里拼字符串写的sql语句
linhaoran0010 2010-07-30
  • 打赏
  • 举报
回复
CREATE PROCEDURE [dbo].[sp_Users_List]
@userName nvarchar(50),
@realName nvarchar(50)
AS
declare @sql varchar(500)
declare @where varchar(2000)
set @where='1=1'
if(@userName is not null and @userName !='')
set @where=@where+'and clumn0='+@userName
if(@realName is not null and @realName !='')
set @where=@where+'and clumn1='+@realName
set @sql='SELECT top 10 * FROM Users '+@where
exec @sql

pt1314917 2010-07-30
  • 打赏
  • 举报
回复
CREATE PROCEDURE [dbo].[sp_Users_List] 
@userName nvarchar(50),
@realName nvarchar(50)
AS
declare @where varchar(200)
set @where =''
set @where =@where+ case isnull(@username,'') when '' then '' else ' and userName='''+@userName+'''' end
set @where =@where+ case isnull(@realName,'') when '' then '' else ' and realName='''+@realName+'''' end
exec('SELECT top 10 * FROM Users where 1=1 '+@where+' and id not in(select top 0 id from Users where 1=1 '+@where+'))')
AuC 2010-07-30
  • 打赏
  • 举报
回复
转化一下
guozhaoyou1 2010-07-30
  • 打赏
  • 举报
回复

CREATE PROCEDURE [dbo].[sp_Users_List]
@userName nvarchar(50),
@realName nvarchar(50)
AS
declare @sql varchar(500)
declare @where varchar(2000)
set @where='1=1'
if(@userName is not null and @userName !='')
set @where=@where+'and clumn0='+@userName
if(@realName is not null and @realName !='')
set @where=@where+'and clumn1='+@realName
set @sql='SELECT top 10 * FROM Users '+@where
exec @sql
wuyq11 2010-07-30
  • 打赏
  • 举报
回复
declare @sql varchar(1000)
set @sql='SELECT top 10 * FROM Users where 1=1'
if(@userName<>'')
set @sql=@sql+ ' and username='''+@username+''''
分页存储过程
http://topic.csdn.net/u/20091204/21/722689e1-7824-497c-b709-4b1118264633.html
hexiaoweiy 2010-07-30
  • 打赏
  • 举报
回复
可以转化一下,oracle有一个nvl吧,sqlservice应该有一个isnull之类的吧,把null转化成''
骑猪看海 2010-07-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yqyqyoyo 的回复:]
SQL code

SELECT top 10 * FROM Users where1=1 and (userName=@userName or @userName='') and (realName=@realName or @realName = '') and id not in(select top 0 from Users where and (userName=@userName ……
[/Quote]

select * from [user] where (username='' or username='')

试了,我前一个传过来是空 ,一条记录都没有了
骑猪看海 2010-07-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hexiaoweiy 的回复:]
在里面写if语句判断段,每一个下面都要有begin end
[/Quote]

这方法我早试过,报错
declare @a nvarchar(100)
set @a=''
select * from [user] where 1=1 if @a<>'' begin username =@a end
yqyqyoyo 2010-07-30
  • 打赏
  • 举报
回复


SELECT top 10 * FROM Users where1=1 and (userName=@userName or @userName='') and (realName=@realName or @realName = '') and id not in(select top 0 from Users where and (userName=@userName or @userName = '') and (realName=@realName or @realName='')

改成这样就可以了。
自己去试一下吧。
hexiaoweiy 2010-07-30
  • 打赏
  • 举报
回复
在里面写if语句判断段,每一个下面都要有begin end
Xiaoyuan245437 2010-07-30
  • 打赏
  • 举报
回复
可以在程序里面做判断吗 不一定要一条sql语句拉
if @realName is null
....
else
...
yqyqyoyo 2010-07-30
  • 打赏
  • 举报
回复
写成 and (? or @userName='') 后面一样

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧