zoukankan      html  css  js  c++  java
  • SQL Server- 存储过程(2)

    --================================
    -- ylb:存储过程创建与操作
    --================================
    use pubs
     
    go
    --一、无参存储过程
    --1,创建存储过程
    create procedure PTitles
    as
    select * from titles
     
    go
    --2,执行存储过程
    execute PTitles
     
    go
    --3,移除存储过程
    --drop procedure PTitles
    go
     
     
    --==============================
    -- ylb:存储过程-入参
    -- 16:44 2011/12/14
    --==============================
    use pubs
     
    go
    --1,创建带入参存储过程
    select * from titles where type='business'
     
    go
    create proc P_Titles_ByType
    @type char(12) --入参
    as
    select * from titles where type=@type
     
     
    go
    --2,执行带参数的存储过程
    --a)方式一
    exec P_Titles_ByType @type='business'
    go
    --b)方式二
    exec P_Titles_ByType 'business'
     
    go
    --P1:写一个存储过程,要求图书类型是business且单价大于10的所有信息
    --P1_1,创建存储过程
    select * from titles
    where type='business' and price>10
     
    go
    create proc P_Titles_ByTypeAndPrice
    @type char(12), --入参
    @price money --入参
    as
    select * from titles
    where type=@type and price>@price
     
    go
    --P1_2,执行存储过程
    exec P_Titles_ByTypeAndPrice
    @type='business',@price=10
     
    go
    exec P_Titles_ByTypeAndPrice
    @price=10,@type='business'
     
    go
    exec P_Titles_ByTypeAndPrice 'business',10
     
    go
    --是错的,当你直接给值时,一定注意参数的顺序和类型。
    --exec P_Titles_ByTypeAndPrice 10,'business'
     
    --================================
    -- ylb:存储过程-带入参和出参
    -- 16:44 2011/12/14
    --================================
    use pubs
     
    go
    select * from titles
    --P1:查图书编号是“BU1032”的图书的单价是多少?
    select price from titles where title_id='BU1032'
     
    go
    --P1_1,创建
    create proc P_Titles_ByTitleID_SelectPrice
    @title_id varchar(6) --入参
    as
    select price from titles where title_id=@title_id
     
    go
    --P1_2,执行
    exec P_Titles_ByTitleID_SelectPrice 'BU1032'
     
    go
    --P2_1,创建
    create proc P_Titles_ByTitleID_SelectPrice2
    @title_id varchar(6), --入参
    @price money output   --出参【出参加标识(output)】
    as
    select @price=price from titles where
    title_id=@title_id
    --出参的@在=左边
     
    go
    --1,先声明变量
    declare @price2 money
    --2,之后在调用
    exec P_Titles_ByTitleID_SelectPrice2
    @title_id='BU1032',
    @price=@price2 output
    --3,再之后在查声明变量
    select @price2
    --出参要声明,配参后面要加output标识,之后再查声明变量。
  • 相关阅读:
    inux按照CPU、内存、磁盘IO、网络性能监测
    监控数据库
    NMON监控工具
    AJAX
    性能经验之谈【转】
    内存/硬盘/io关系
    testng中添加案例失败重试次数
    如何使用beanshell写入数据到文件(txt、csv)
    Docker 容器中配置nginx后报403 Forbidden 解决办法
    Centos7创建支持ssh服务器的docker容器
  • 原文地址:https://www.cnblogs.com/wuzehao/p/5450226.html
Copyright © 2011-2022 走看看