当前位置:   article > 正文

【SQL Sever】 函数的创建_sql sever 中函数的建立方式

sql sever 中函数的建立方式
  1. --函数的创建
  2. create function getname (@stunumber char(6))
  3. returns varchar(20)
  4. with encryption
  5. begin
  6. declare @stuname varchar(20)
  7. select @stuname=stuname from student
  8. where stunumber=@stunumber
  9. return @stuname
  10. end
  11. select * from student
  12. create function getstudent(@claid int)
  13. returns table
  14. with encryption
  15. return
  16. select * from student
  17. where claid=@claid
  18. select dbo.getname('200401') as '学生姓名'
  19. select * from getstudent(6)
  20. drop function getstudent
  21. CREATE TABLE bank
  22. (
  23. customerName CHAR(10), --顾客姓名
  24. currentMoney MONEY --当前余额
  25. )
  26. ALTER TABLE bank
  27. ADD CONSTRAINT CK_currentMoney
  28. CHECK(currentMoney>=1)
  29. INSERT INTO bank(customerName,currentMoney)
  30. VALUES('张三',1000)
  31. INSERT INTO bank(customerName,currentMoney)
  32. VALUES('李四',1)
  33. select * from bank
  34. declare @err1 int,@err2 int
  35. begin transaction
  36. update bank set currentMoney=currentMoney-1
  37. set @err1=@err1+@@ERROR
  38. save transaction transfer
  39. UPDATE bank SET currentMoney=currentMoney-1000 WHERE customerName='张三'
  40. set @err2=@err2+@@ERROR
  41. UPDATE bank SET currentMoney=currentMoney+1000 WHERE customerName='李四'
  42. set @err2=@err3+@@ERROR
  43. if @err1 <> 0
  44. begin
  45. rollback transaction
  46. print '操作失败。。'
  47. end
  48. else
  49. begin
  50. if @err2 <> 0
  51. begin
  52. rollback transaction transfer
  53. print '转账失败。。'
  54. end
  55. end

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号