SQL 常用預存程序

來源:互聯網
上載者:User

常用預存程序集錦,都是一些mssql常用的一些,大家可以根據需要選擇使用。

  =================分頁==========================

  /*分頁尋找資料*/
  CREATE PROCEDURE [dbo].[GetRecordSet]
  @strSql varchar(8000),--查詢sql,如select * from [user]
  @PageIndex int,--查詢當頁號
  @PageSize int--每頁顯示記錄
  AS
  set nocount on
  declare @p1 int
  declare @currentPage int
  set @currentPage = 0
  declare @RowCount int
  set @RowCount = 0
  declare @PageCount int
  set @PageCount = 0
  exec sp_cursoropen @p1 output,@strSql,@scrollopt=1,@ccopt=1,@rowcount=@rowCount output --得到總記錄數
  select @PageCount=ceiling(1.0*@rowCount/@pagesize) --得到總頁數
  ,@currentPage=(@PageIndex-1)*@PageSize+1
  select @RowCount,@PageCount
  exec sp_cursorfetch @p1,16,@currentPage,@PageSize
  exec sp_cursorclose @p1
  set nocount off
  GO
  =========================使用者註冊============================
  /*
  使用者註冊,也算是添加吧
  */
  Create proc [dbo].[UserAdd]
  (
  @loginID nvarchar(50),     --登入帳號
  @password nvarchar(50), --密碼
  @email nvarchar(200) --電子信箱
  )
  as
  declare @userID int --使用者編號
  --登入帳號已經被註冊
  if exists(select loginID from tableName where loginID = @loginID)
  begin
  return -1;
  end
  --郵箱已經被註冊
  else if exists(select email from tableName where email = @email)
  begin
  return -2;
  end
  --註冊成功
  else
  begin
  select @userID = isnull(max(userID),100000)+1 from tableName
  insert into tableName
  (userID,loginID,[password],userName,linkNum,address,email,createTime,status)
  values
  (@userID,@loginID,@password,'','','',@email,getdate(),1)
  return @userID
  end

==========================sql server系統預存程序===================

  –1.給表中欄位添加描述資訊
  Create table T2 (id int , name char (20))
  GO
  EXEC sp_addextendedproperty 'MS_Description', 'Employee ID', 'user', dbo, 'table', T2, 'column', id
  EXEC sp_updateextendedproperty 'MS_Description', 'this is a test', 'user', dbo, 'table', T2, 'column', id
  –2.修改資料庫名稱
  EXEC sp_renamedb 'old_db_name', 'new_db_name'
  –3.修改資料表名稱和欄位名稱
  EXEC sp_rename 'old_table_name', 'new_table_name'–修改資料表名稱
  EXEC sp_rename 'table_name.[old_column_name]', 'new_column_name', 'COLUMN'–修改欄位名稱
  –4.給定預存程序名,擷取預存程序內容
  exec sp_helptext sp_name
  /*以下是有關安全控制的系統預存程序或 SQL 陳述式,詳細文法查閱《聯機叢書》相關內容*/
  –建立新的 SQL Server 登入,使使用者得以串連使用 SQL Server 身分識別驗證的 SQL Server。
  EXEC sp_addlogin @loginame = '', @passwd = '', @defdb = '', @deflanguage = NULL, @sid = NULL, @encryptopt = NULL
  –使 Windows NT 使用者或群組帳戶得以使用 Windows 身分識別驗證串連到 SQL Server。
  EXEC sp_grantlogin @loginame = ''
  –刪除 SQL Server 登入,以阻止使用該登入名稱訪問 SQL Server。
  EXEC sp_droplogin @loginame = ''
  –阻止 Windows NT 使用者或組串連到 SQL Server。
  EXEC sp_denylogin @loginame = ''
  –從 SQL Server 中刪除用 sp_grantlogin 或 sp_denylogin 建立的 Windows NT 使用者或組的登入項。
  EXEC sp_revokelogin @loginame = ''
  –更改登入的預設資料庫。
  EXEC sp_defaultdb @loginame = '', @defdb = ''
  –更改登入的預設語言。
  EXEC sp_defaultlanguage @loginame = '', @language = ''
  –添加或更改 SQL Server 登入密碼。
  EXEC sp_password @old = '', @new = '', @loginame = ''
  –添加伺服器角色新成員。
  EXEC sp_addsrvrolemember @loginame = '', @rolename = ''
  –添加伺服器角色某成員。
  EXEC sp_dropsrvrolemember @loginame = '' , @rolename = ''
  –為 SQL Server 登入或 Windows NT 使用者或組在當前資料庫中添加一個安全帳戶,並使其能夠被授予在資料庫中執行活動的許可權(授予預設的“public”資料庫角色)。
  EXEC sp_grantdbaccess @loginame = '', @name_in_db = NULL
  –或
  EXEC sp_adduser @loginame = '', @name_in_db = NULL, @grpname = ''
  –從當前資料庫中刪除安全帳戶。
  EXEC sp_revokedbaccess @name_in_db = ''
  –或
  EXEC sp_dropuser @name_in_db = ''
  –在當前資料庫建立新資料庫角色。
  EXEC sp_addrole @rolename = '', @ownername = ''
  –在當前資料庫刪除某資料庫角色。
  EXEC sp_droprole @rolename = ''
  –在當前資料庫中添加資料庫角色新成員。
  EXEC sp_addrolemember @rolename = '', @membername = ''
  –在當前資料庫中刪除資料庫角色某成員。
  EXEC sp_droprolemember @rolename = '', @membername = ''
  –許可權分配給資料庫角色、表、預存程序等對象
  –1、授權訪問
  GRANT
  –2、拒絕訪問
  DENY
  –3、取消授權或拒絕
  REVOKE
  –4、Sample(pubs):
  GRANT SELECT ON authors TO Limperator
  DENY SELECT ON authors TO Limperator
  REVOKE SELECT ON authors TO Limperator

====================資料庫還原的預存程序============

  SQL code
  create proc killspid (@dbname varchar(20))
  as
  begin
  declare @sql nvarchar(500)
  declare @spid int
  set @sql='declare getspid cursor for
  select spid
  from sysprocesses
  where dbid=db_id('''+@dbname+''')'
  exec (@sql)
  open getspid
  fetch next from getspid
  into @spid
  while @@fetch_status <>-1
  begin
  exec('kill '+@spid)
  fetch next from getspid
  into @spid
  end
  close getspid
  deallocate getspid
  end
  GO

  作用:殺掉傳入資料庫中的活動進程以進行備份還原等獨佔操作

  ===================阿拉伯數字轉大寫中文=============

  例:輸入12345,程式給出:壹萬貳仟三佰肆拾伍

  例:輸入10023040,程式給出:壹仟另貳萬三仟另肆拾

  解決方案之一(在SqlServer2000中測試通過):

  SQL code
  CREATE FUNCTION fun_cgnum
  (@num INT)
  RETURNS VARCHAR(100)
  AS
  BEGIN
  DECLARE @temp INT,@res INT,@i TINYINT
  DECLARE @str VARCHAR(100),@no VARCHAR(20),@unit VARCHAR(16)
  SELECT @str='',@no='另壹貳三肆伍陸柒捌玖',@unit='拾佰仟萬拾佰仟億'
  SET @temp=@num

常用預存程序集錦,都是一些mssql常用的一些,大家可以根據需要選擇使用。

  SELECT @i=0,@res=@temp%10,@temp=@temp/10
  WHILE @temp>0
  BEGIN
  IF @i=0
  SET @str=SUBSTRING(@no,@res+1,1)
  ELSE
  SET @str=SUBSTRING(@no,@res+1,1)+SUBSTRING(@unit,@i,1)+@str
  SELECT @res=@temp%10,@temp=@temp/10
  SET @i=@i+1
  END
  SET @str=SUBSTRING(@no,@res+1,1)+SUBSTRING(@unit,@i,1)+@str
  SET @str=REPLACE(@str,'另拾','另')
  SET @str=REPLACE(@str,'另佰','另')
  SET @str=REPLACE(@str,'另仟','另')
  SET @str=REPLACE(@str,'另拾','另')
  SET @str=REPLACE(@str,'另萬','萬')
  WHILE @i>0
  BEGIN
  SET @str=REPLACE(@str,'另另','另')
  SET @i=CHARINDEX('另另',@str)
  END
  SET @str=REPLACE(@str,'另萬','萬')
  SET @str=REPLACE(@str,'億萬','億')
  IF RIGHT(@str,1)='另'
  SET @str=LEFT(@str,LEN(@str)-1)
  RETURN @str
  END
  GO

  --測試:有0和沒有0的情況

  SELECT dbo.fun_cgnum(900000000),dbo.fun_cgnum(903002051),dbo.fun_cgnum(903002050)

  PS:有興趣的朋友可以繼續考慮有小數點以及添加單位(元/角/分)的情況

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.