经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
sql存储过程简单实例语句
来源:cnblogs  作者:不讲武德  时间:2020/12/8 9:01:36  对本文有异议
  1. 1 create database bookshop
  2. 2 go
  3. 3
  4. 4 use bookshop
  5. 5 go
  6. 6
  7. 7 /*员工人事表*/
  8. 8 CREATE TABLE employee(
  9. 9 emp_no char(5)not null primary key,
  10. 10 emp_name char(10) not null,
  11. 11 sex char(2) not null,
  12. 12 dept char(4) not null,
  13. 13 title char(6) not null,
  14. 14 date_hired datetime not null,
  15. 15 birthday datetime null,
  16. 16 salary int not null,
  17. 17 telephone varchar(20) null,
  18. 18 addr char(50) null,
  19. 19 )
  20. 20 /*客户表*/
  21. 21 create table customer(
  22. 22 cust_name varchar(20) not null primary key,
  23. 23 receiver varchar(20) ,
  24. 24 tel_no char(20),
  25. 25 cust_addr varchar(100)
  26. 26 )
  27. 27
  28. 28 /*产品名称表*/
  29. 29 create table books(
  30. 30 book_no char(6) not null primary key,
  31. 31 book_name varchar(50) not null,
  32. 32 price numeric(7,1) not null,
  33. 33 book_type varchar(20),
  34. 34 ISBN varchar(15)
  35. 35 )
  36. 36
  37. 37 /*销售主表*/
  38. 38 create table sales(
  39. 39 order_no char(6) not null primary key,
  40. 40 cust_name varchar(20) null,
  41. 41 total_amt numeric(9,2) null,
  42. 42 order_date datetime null,
  43. 43 sale_id char(5) null,
  44. 44 foreign key (cust_name) references customer(cust_name),
  45. 45 foreign key (sale_id) references employee(emp_no)
  46. 46 )
  47. 47 /*销货明细表*/
  48. 48 create table sale_item(
  49. 49 order_no char(6) not null,
  50. 50 book_no char(6) not null,
  51. 51 qty int not null,
  52. 52 unit_price numeric(7,1) null,
  53. 53 constraint pk_sale_item primary key
  54. 54 (order_no,book_no ),
  55. 55 foreign key (order_no) references sales(order_no),
  56. 56 foreign key (book_no) references books(book_no)
  57. 57 )
  58. 58
  59. 59 /*人事表(employee)数据:*/
  60. 60 insert employee values('E0001','王大华','','业务','经理','1976-10-13','1951-08-01',80000,'13107912134','上海市')
  61. 61 insert employee values('E0003','陈自强','','会计','科长','1986-09-15','1963-06-09',48000,'13307913451','南京市')
  62. 62 insert employee values('E0014','周小梅','','业务','职员','1996-03-01','1970-03-28',32000,'13579607879','上海市')
  63. 63 insert employee values('E0009','陈建国','','管理','科长','1987-04-15','1967-09-01',45000,'13879787765','天津市')
  64. 64 insert employee values('E0017','林光华','','业务','职员','1995-10-13','1973-08-17',30000,'13979765654','上海市')
  65. 65 insert employee values('E0006','李珠珠','','管理','经理','1988-01-01','1961-07-12',60000,'13679787667','北京市')
  66. 66 insert employee values('E0002','李庄敬','','人事','科长','1980-09-15','1958-05-13',80000,'13979962335','广州市')
  67. 67 insert employee values('E0010','王成功','','信息','职员','1993-02-15','1969-04-15',45000,'13723456789','北京市')
  68. 68 insert employee values('E0013','陈中华','','业务','职员','1993-02-15','1966-07-01',43000,'13978790987','天津市')
  69. 69 insert employee values('E0008','刘 刚','','业务','职员','1994-11-01','1968-08-01',40000,'13767654543','上海市')
  70. 70 insert employee values('E0005','李珊珊','','会计','职员','1990-03-20','1967-04-25',38000,'13890987876','上海市')
  71. 71 insert employee values('E0011','李小蓉','','人事','职员','1994-11-01','1970-11-18',30000,'13345432321','重庆市')
  72. 72 insert employee values('E0012','蔡文钦','','制造','厂长','1984-08-15','1960-07-21',50000,'13789876766','上海市')
  73. 73 insert employee values('E0015','张大山','','制造','职员','1993-12-15','1968-09-23',35000,'13567657889','上海市')
  74. 74 insert employee values('E0007','吴铁雄','','信息','科长','1989-10-01','1965-04-18',50000,'13389876765','武汉市')
  75. 75 insert employee values('E0016','方美美','','业务','职员','1992-05-20','1966-06-23',40000,'13167688877','上海市')
  76. 76 insert employee values('E0004','刘中兴','','制造','经理','1984-05-01','1960-05-23',60000,'13541586766','上海市')
  77. 77 insert employee values('E0019','王仁华','','信息','经理','1985-09-15','1959-03-24',60000,'13122334545','上海市')
  78. 78 insert employee values('E0020','陈火旺','','业务','职员','1992-08-01','1964-05-12',40000,'13978796622','天津市')
  79. 79 insert employee values('E0018','林中正','','管理','总经理','1974-10-01','1953-05-04',100000,'13879765667','上海市')
  80. 80
  81. 81
  82. 82
  83. 83 /* 客户(customer)表数据:*/
  84. 84 insert into customer values('syiyang','帅宜阳','0791-88120321','江西南昌')
  85. 85 insert into customer values('zbowen','钟博文','13564796754','广东广州')
  86. 86 insert into customer values('zhaoyi','赵毅','0791-88507321','湖南长沙')
  87. 87 insert into customer values('zxuda','张旭达','13674018787','广东广州')
  88. 88 insert into customer values('zhangtao','张淘','13807917103','江西南昌')
  89. 89 insert into customer values('zhangyu','张毓','13807910876','江西南昌')
  90. 90 insert into customer values('wming','王敏',null,'江西南昌')
  91. 91
  92. 92 /*产品名称表*/
  93. 93 insert into books values('20652','数据库系统概论',39,'计算机','978704040661')
  94. 94 insert into books values('22082','java编程思想',108,'计算机','978704040324')
  95. 95 insert into books values('22323','SQLServer入门',59.8,'计算机','978704040121')
  96. 96 insert into books values('95745','数据库基础教程',38,'计算机','978704040001')
  97. 97 insert into books values('95762','傲慢与偏见',33.8,'文学','9787532750849')
  98. 98 insert into books values('43748','哈利波特',225,'文学','23580693')
  99. 99 insert into books values('43760','神奇校车',132,'绘本','21005473')
  100. 100 insert into books values('83431','数据库_原理',28,'计算机','978732424321')
  101. 101 insert into books values('83422','数据库及原理',24,'计算机','978732424002')
  102. 102 insert into books values('541122','DBA mananging',24,'计算机','978710424213')
  103. 103 /*销售主表*/
  104. 104
  105. 105 insert into sales values('102893','zbowen',300,'2015-3-16','E0017')
  106. 106 insert into sales values('102894','zhaoyi',244,'2015-5-2','E0016')
  107. 107 insert into sales values('102895','zhaoyi',59,'2015-8-29','E0008')
  108. 108 insert into sales values('102896','zxuda',19,'2015-8-29','E0017')
  109. 109 insert into sales values('102897','zxuda',19,'2015-9-15','E0005')
  110. 110 insert into sales values('102898','syiyang',39,'2015-8-9','E0016')
  1.  
  1. --1、利用存储过程,给employee表添加一条业务部门员工的信息。
  2. create proc ins_emp @emp_no char(5),@emp_name char(10),@sex char(2),@dept char(4),@title char(6),@date_hired datetime,@birthday datetime,@salary int,@telephone varchar(20),@addr char(50)
  3. as
  4. insert into employee values(@emp_no ,@emp_name ,@sex ,@dept ,@title ,@date_hired ,@birthday ,@salary ,@telephone ,@addr )
  5.  
  6. exec ins_emp 'E0021','Jack','男','业务','职员','1990-12-12','1970-12-11',32000,123123445,'上海市'
  7.  
  8. --2、利用存储过程从employeesalescustomer表的连接中返回所有业务员的姓名、客户姓名、销售金额。
  9. create proc join_oper
  10. as
  11. select emp_name,b.cust_name,total_amt
  12. from employee a,sales b,customer c
  13. where a.emp_no=b.sale_id and b.cust_name=c.cust_name
  14. exec join_oper
  15. --3、创建带一个输入参数的存储过程,实现按员工姓名进行模糊查找,查找员工编号、订单编号、销售金额。
  16. create proc find_name @emp_name varchar(10)
  17. as
  18. select sale_id,order_no,total_amt
  19. from employee a,sales b
  20. where a.emp_no=b.sale_id and emp_name like @emp_name
  21. exec find_name '刘%'
  22. --4、创建带两个输入参数的存储过程,查找姓“李”并且职称为“职员”的员工的员工编号、订单编号、销售金额。
  23. create proc find_name_title @emp_name varchar(10) ,@title varchar(6)
  24. as
  25. select sale_id,order_no,total_amt
  26. from employee a,sales b
  27. where a.emp_no=b.sale_id and emp_name like @emp_name and title like @title
  28. exec find_name_title '李%','职员'
  29.  
  30. --5、利用存储过程计算出订单编号为102898的订单的销售金额。(带一输入参数和一输出参数)(提示:sales表中的total_amt应该等于sale_item表中的同一张订单的不同销售产品的qty*unit_price之和)
  31. create proc total_amt @order_no int,@sum_amt int output
  32. as
  33. select @sum_amt=SUM(qty*unit_price)
  34. from sale_item
  35. where order_no=@order_no
  36. declare @tot int
  37. exec total_amt '102898',@tot output
  38. select @tot
  39.  
  40.  
  41. --6、创建一存储过程,根据给出的职称,返回该职称的所有员工的平均工资。(带一输入参数和返回值)
  42. create proc avg_salary @title char(6)
  43. as
  44. declare @avg float
  45. select @avg=AVG(salary)
  46. from employee
  47. where title=@title
  48. return @avg
  49. declare @avg float
  50. exec @avg=avg_salary '职员'
  51. select @avg
  52.  
  53. --7、请创建一个存储过程,修改sales表中的订单金额total_amt,使之等于各订单对应的所有订单明细的数量与单价的总和。
  54. create proc update_sales_totamt
  55. as
  56. update sales
  57. set total_amt=(select sum(qty*unit_price)
  58. from sale_item
  59. where sales.order_no=sale_item.order_no)
  60.  
  61. exec update_sales_totamt
  62. select * from sales
  63. select * from sale_item
  1.  

  

  1.  

 

原文链接:http://www.cnblogs.com/spiderheroc/p/14060288.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号