出处:http://www.jb51.net/article/54730.htm

--有输入参数的存储过程--
create proc GetComment
(@commentid int)
as
select * from Comment where CommentID=@commentid
 
调用方式:exec GetComment 3
  
--有输入与输出参数的存储过程--
create proc GetCommentCount
@newsid int,
@count int output
as
select @count=count(*) from Comment where NewsID=@newsid
 
调用方式:
declare @cnt int
exec GetCommentCount 1,@cnt  output
print @cnt
 
--返回单个值的函数--
create function MyFunction
(@newsid int)
returns int
as
begin
declare @count int
select @count=count(*) from Comment where NewsID=@newsid
return @count
end
 
调用方式:
declare @cnt int
exec @cnt = MyFunction 1
print @cnt
 
--返回值为表的函数--
Create function GetFunctionTable
(@newsid int)
returns table
as
return
(select * from Comment where NewsID=@newsid)
go
  
调用方式:
select * from GetFunctionTable(2)
 
CREATE proc func_withconditions
(
 @firstName varchar(20),
 @lastName varchar(20)
)
AS
begin
    declare @sql varchar(500)
    set @sql = 'select * from employee where 1=1  '
    if(@firstName is not null)
          set @sql = @sql+' and first_name='+''''+@firstName+''''
    if(@lastName <> ' ' and @lastName is not null)
          set @sql = @sql+' and last_name='+''''+@lastName+''''
    exec(@sql)
end
GO
 
调用方式:
exec func_withconditions 'ahg',''
exec func_withconditions 'ahg',NULL
exec func_withconditions NULL,'jhg'
 
 
 

SQL Server 2008 存储过程示例的更多相关文章

  1. SQL Server 2008 存储过程,带事务的存储过程(创建存储过程,删除存储过程,修改存储过

    SQL Server 2008 存储过程,带事务的存储过程(创建存储过程,删除存储过程,修改存储过     存储过程 创建存储过程 use pubs --pubs为数据库 go create proc ...

  2. Delphi 调用SQL Server 2008存储过程

    1.表结构如下(预算数据明细表): CREATE TABLE [dbo].[BA_FeeDetail]( [ID] [int] IDENTITY(1,1) NOT NULL, [FeeDeptID] ...

  3. SQL SERVER 2008 存储过程传表参数

      最近项目使用到了存储过程传入表类型参数. --定义表类型 create type t_table_type as table ( id int, name varchar(32), sex var ...

  4. SQL Server 2008空间数据应用系列十:使用存储过程生成GeoRSS聚合空间信息

    原文:SQL Server 2008空间数据应用系列十:使用存储过程生成GeoRSS聚合空间信息 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2 ...

  5. SQL Server 2008 R2执行存储过程sp_MailItemResultSets引起大量PREEMPTIVE_OS_WAITFORSINGLEOBJEC等待

      从监控工具DPA中发现一个数据库(SQL Server 2008 R2)的等待事件突然彪增,下钻分析发现数据库执行存储过程sp_MailItemResultSets时,引起了非常严重的等待(Hig ...

  6. SQL Server 2008 R2——T-SQL 存储过程 返回表

    ==================================声明================================== 本文原创,转载在正文中显要的注明作者和出处,并保证文章的完 ...

  7. SQL server 2008里面通过sys.dm_exec_procedure_stats得到存储过程的执行信息--转

    --转自:http://blogs.msdn.com/b/apgcdsd/archive/2011/05/13/sql-server-2008-sys-dm-exec-procedure-stats. ...

  8. 如何在SQL Server 2008下轻松调试T-SQL语句和存储过程

    一.回顾早期的SQL SERVER版本:早在SQL Server 2000时代,查询分析器的功能还很简陋,远不如VS那么强大.到SQL Server 2005时代,代码高亮.SQL优化等功能逐渐加强, ...

  9. SQL SERVER 2008向ORACLE 11G迁移示例

    来源于:http://www.cnblogs.com/hiizsk/ 由SQL SERVER 2008向ORACLE 11G迁移过程记录之一-表 使用Oracle Sql Developer将SQL ...

随机推荐

  1. Get與Post的區別--總結隨筆

    關於Get與Post的區別的文章,在網上太多了:有優點有缺點,今天我給各位大哥做一個總結性的隨筆,還請多多包涵~ 首先是W3School上的答案,請查收: GET在浏览器回退时是无害的,而POST会再 ...

  2. 用uliweb 创建项目

    创建项目 ***@Android:~# uliweb makeproject ablog ***@Android:~# cd ablog/ ***@Android:~/ablog# ls apps f ...

  3. 查看git提交细节

    git log git show fdf39277f54dd0484a9fefc012463924544e07af

  4. 洛咕 P3965 [TJOI2013]循环格

    同tjoi2010 打扫房间,每个点入度,出度都为1,可以向相邻4个点连边,但只有原来存在的边费用为0. // luogu-judger-enable-o2 #include<bits/stdc ...

  5. Windows:打开MSDTC,恢复Windows任务栏,查看windows日志,打开远程桌面,打开Services,资源监控

    Windows 服务器系列: Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程 Windows:使用Dos命令管理服务(Services) Windows:任务调 ...

  6. Navigation - How to define the structure of the navigation tree via the NavigationItemAttribute

    In the meantime, you should use the Model Editor to create such a navigation structure. There are se ...

  7. 沧桑巨变中焕发青春活力-记极1s HC5661A 打怪升级之路

    最近发现一个新货umaxhosting年付10美元的便宜VPS.2杯喜茶的价格可以让你在国外拥有一个1024MB (1GB) DDR3 RAM.1024MB (1GB) vSwap.70GB RAID ...

  8. 模拟websocket推送消息服务mock工具二

    模拟websocket推送消息服务mock工具二 在上一篇博文中有提到<使用electron开发一个h5的客户端应用创建http服务模拟后端接口mock>使用electron创建一个模拟后 ...

  9. [WPF]解决模板中ContextMenu绑定CommandParameter的问题

    直接上代码,首先是一个ContextMenu的模板: <ContextMenu x:Key="Menu" BorderThickness="0.3" Fo ...

  10. python继承与多继承

    1.类与对象里的父类与子类(继承): 类的继承主要是指自子类对于之前父类的方法的继承,如果子类里面写了父类里的方法,则它会将父类里的方法覆盖掉,从而不能再调用到父类的方法. 2.为了解决父类与子类里的 ...