设为首页 加入收藏

TOP

SQLServer存储过程(一)
2014-11-24 02:59:44 来源: 作者: 【 】 浏览:9
Tags:SQLServer 存储 过程

--有输入参数的存储过程--
create proc GetComment
(@commentid int)
as
select * from Comment where CommentID=@commentid

--有输入与输出参数的存储过程--
create proc GetCommentCount
@newsid int,
@count int output
as
select @count=count(*) from Comment where NewsID=@newsid


--返回单个值的函数--
create function MyFunction
(@newsid int)
returns int
as
begin
declare @count int
select @count=count(*) from Comment where NewsID=@newsid
return @count
end

--调用方法--
declare @count int
exec @count=MyFunction 2
print @count

--返回值为表的函数--
Create function GetFunctionTable
(@newsid int)
returns table
as
return
(select * from Comment where NewsID=@newsid)

--返回值为表的函数的调用--
select * from GetFunctionTable(2)


-----------------------------------------------------------------------------------------------------------------------------------
SQLServer 存储过程中不拼接SQL字符串实现多条件查询

 以前拼接的写法
  set @sql=' select * from table where 1=1 '
  if (@addDate is not null)
   set @sql = @sql+' and addDate = '+ @addDate + ' '
  if (@name <>'' and is not null)
   set @sql = @sql+ ' and name = ' + @name + ' '
  exec(@sql)
下面是 不采用拼接SQL字符串实现多条件查询的解决方案
  第一种写法是 感觉代码有些冗余
  if (@addDate is not null) and (@name <> '')
   select * from table where addDate = @addDate and name = @name
  else if (@addDate is not null) and (@name ='')
   select * from table where addDate = @addDate
  else if(@addDate is null) and (@name <> '')
   select * from table where and name = @name
  else if(@addDate is null) and (@name = '')
  select * from table
  第二种写法是
  select * from table where (addDate = @addDate or @addDate is null) and (name = @name or @name = '')
  第三种写法是
  SELECT * FROM table where
  addDate = CASE @addDate IS NULL THEN addDate ELSE @addDate END,
  name = CASE @name WHEN '' THEN name ELSE @name END

-----------------------------------------------------------------------------------------------------------------------------------
SQLSERVER存储过程基本语法

一、定义变量
--简单赋值
declare @a int
set @a=5
print @a

--使用select语句赋值
declare @user1 nvarchar(50)
select @user1= '张三'
print @user1
declare @user2 nvarchar(50)
select @user2 = Name from ST_User where ID=1
print @user2

--使用update语句赋值
declare @user3 nvarchar(50)
update ST_User set @user3 = Name where ID=1
print @user3

二、表、临时表、表变量
--创建临时表1
create table #DU_User1
(
[ID] [ int ] NOT NULL ,
[Oid] [ int ] NOT NULL ,
[Login] [nvarchar](50) NOT NULL ,
[Rtx] [nvarchar](4) NOT NULL ,
[ Name ] [nvarchar](5) NOT NULL ,
[ Password ] [nvarchar]( max ) NULL ,
[State] [nvarchar](8) NOT NULL
);
--向临时表1插入一条记录
insert into #DU_User1 (ID,Oid,[Login],Rtx, Name ,[ Password ],State) values (100,2, 'LS' , '0000' , '临时' , '321' , '特殊' );

--从ST_User查询数据,填充至新生成的临时表
select * into #DU_User2 from ST_User where ID<8

--查询并联合两临时表
select * from #DU_User2 where ID<3 union select * from #DU_User1

--删除两临时表
drop table #DU_User1
drop table #DU_User2

--创建临时表
CREATE TABLE #t
(
[ID] [ int ] NOT NULL ,
[Oid] [ int ] NOT NULL ,
[Login] [nvarchar](50) NOT NULL ,
[Rtx] [nvarchar](4) NOT NULL ,
[ Name ] [nvarchar](5) NOT NULL ,
[ Password ] [nvarchar]( max ) NULL ,
[State] [nvarchar](8) NOT NULL ,
)

--将查询结果集(多条数据)插入临时表
insert into #t select * from ST_User
--不能这样插入
--select * into #t from dbo.ST_User

--添加一列,为int型自增长子段
alter table #t add [myid] int NOT NULL IDENTITY(1,1)
--添加一列,默认填充全球唯一标识
alter table #t add [myid1] uniqueidentifier NOT NULL default (newid())

select * from #t
drop table #t
--给查询结果集增加自增长列

--无主键时:
select IDENTITY( int ,1,1) as

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用实SQL语句 下一篇sql server使用for xml path将一..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)