QuickStart SQL defines stored procedures

Source: Internet
Author: User

SQL stored procedures are widely used in SQL databases, and here is how to define SQL stored procedures, and if you are a user who has just contacted a SQL database, you may want to look at it to help you learn about SQL stored procedures.

 
  1. CREATE PROCEDURE Get_tableinfo as
  2. If not EXISTS (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Tablespaceinfo] and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
  3. CREATE TABLE Tablespaceinfo--Creating a result store table
  4. (Nameinfo varchar (50),
  5. rowsinfo int, reserved varchar (20),
  6. Datainfo varchar (20),
  7. Index_size varchar (20),
  8. Unused varchar (20))
  9. Delete from Tablespaceinfo--empty data table
  10. DECLARE @tablename varchar (255)--table name
  11. DECLARE @cmdsql varchar (500)
  12. DECLARE info_cursor Cursor FOR
  13. Select O.name
  14. From Dbo.sysobjects o where ObjectProperty (o.id, N ' istable ') = 1
  15. and o.name not like N ' #%% ' ORDER by O.name
  16. OPEN Info_cursor
  17. FETCH NEXT from Info_cursor
  18. Into @tablename
  19. While @ @FETCH_STATUS = 0
  20. BEGIN
  21. if exists (SELECT * from dbo.sysobjects WHERE id = object_id (@tablename) and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
  22. Execute sp_executesql
  23. N ' INSERT into Tablespaceinfo exec sp_spaceused @tbname ',
  24. N ' @tbname varchar (255) ',
  25. @tbname = @tablename
  26. FETCH NEXT from Info_cursor
  27. Into @tablename
  28. End
  29. Close Info_cursor
  30. Deallocate info_cursor
  31. Go




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.