SQL Server 2008 存储过程示例
<div class="jb51code"> --有输入参数的存储过程-- --有输入与输出参数的存储过程-- --返回单个值的函数-- --调用方法-- --返回值为表的函数-- --返回值为表的函数的调用-- SQLServer 存储过程中不拼接SQL字符串实现多条件查询 '' and is not null) set @sql = @sql+ ' and name = ' + @name + ' ' exec(@sql)下面是 不采用拼接SQL字符串实现多条件查询的解决方案 '') 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 ENDSQLSERVER存储过程基本语法 一、定义变量 --使用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语句赋值 二、表、临时表、表变量 --从ST_User查询数据,填充至新生成的临时表select * into #DU_User2 from ST_User where ID<8 --查询并联合两临时表 --删除两临时表 --创建临时表 --将查询结果集(多条数据)插入临时表 --添加一列,为int型自增长子段 select * from #t --无主键时: --有主键时: 三、循环 四、条件语句 --when then条件分支declare @today int declare @week nvarchar(3) set @today=3 set @week= case when @today=1 then '星期一' when @today=2 then '星期二' when @today=3 then '星期三' when @today=4 then '星期四' when @today=5 then '星期五' when @today=6 then '星期六' when @today=7 then '星期日' else '值错误' end print @week 五、游标 --定义一个游标declare user_cur cursor for select ID,[Login] from ST_User --打开游标 open user_cur while @@fetch_status=0 begin --读取游标 fetch next from user_cur into @ID,@Oid,@Login print @ID --print @Login end close user_cur --摧毁游标 deallocate user_cur 五、游标 --定义一个游标declare user_cur cursor for select ID,@Login print @ID --print @Login end close user_cur --摧毁游标 deallocate user_cur 六、触发器 触发器中的临时表: drop trigger User_OnUpdate 七、存储过程 --创建Return返回值存储过程CREATE PROCEDURE PR_Sum2 @a int,@b int AS BEGIN Return @a+@b END --执行存储过程获取output型返回值 --执行存储过程获取Return型返回值 八、自定义函数 函数的分类: create function FUNC_UserTab_1 ( @myId int ) returns table as return ( select * from ST_User where ID<@myId) --新建多语句表值函数 --调用表值函数 --删除标量值函数 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- SQLSERVER2008 18456错误
- 新手学习 MS SqlServer ,连接不上服务器的解决方法(第一次
- SQLSERVER的版本信息和SP补丁信息查看方法
- sql-server – 如何将数据从数据库推送到应用程序?
- 当使用窗口函数并且谓词包含变量时,SQL Server使用scan而不
- ASP.NET下向SQLServer2008导入文件实例操作方法
- sql-server – 填充SQL数据库的事务日志设置为Simple
- sql-server – 需要帮助恢复损坏的SQL数据库
- sqlserver2008 端口占用 8080 ReportingServicesService.ex
- sqlserver中如何提取datetime格式数据中的日期或时间?