快速入門SQL定義預存程序

來源:互聯網
上載者:User

  SQL預存程序在SQL資料庫中用途廣泛,下面為您介紹如何定義SQL預存程序,如果您是剛接觸SQL資料庫的使用者,不妨一看,希望對您學習SQL預存程序有所協助。

 

  1. CREATE PROCEDURE get_tableinfo AS     
  2.      
  3. if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablespaceinfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)     
  4. create table  tablespaceinfo                         --建立結果儲存表     
  5.               (nameinfo varchar(50) ,      
  6.                rowsinfo int , reserved varchar(20) ,      
  7.                datainfo varchar(20)  ,      
  8.                index_size varchar(20) ,      
  9.                unused varchar(20) )     
  10.      
  11.      
  12. delete from tablespaceinfo --清空資料表     
  13.      
  14. declare @tablename varchar(255)  --表名稱     
  15.      
  16. declare @cmdsql varchar(500)     
  17.      
  18. DECLARE Info_cursor CURSOR FOR      
  19. select o.name       
  20. from dbo.sysobjects o where OBJECTPROPERTY(o.id, N'IsTable') = 1      
  21.      and o.name not like N'#%%'  order by o.name     
  22.      
  23. OPEN Info_cursor     
  24.      
  25. FETCH NEXT FROM Info_cursor      
  26. INTO @tablename      
  27.      
  28. WHILE @@FETCH_STATUS = 0     
  29. BEGIN     
  30.      
  31.   if exists (select * from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1)     
  32.   execute sp_executesql      
  33.          N'insert into tablespaceinfo  exec sp_spaceused @tbname',     
  34.           N'@tbname varchar(255)',     
  35.           @tbname = @tablename     
  36.      
  37.   FETCH NEXT FROM Info_cursor      
  38.   INTO @tablename      
  39. END     
  40.      
  41. CLOSE Info_cursor     
  42. DEALLOCATE Info_cursor     
  43. GO  




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.