SQL Script 雜記

一、提交sql server中未提交的事務sql

commit select   @@TRANCOUNT函數

二、查詢存儲過程當中包含某個字符串的全部存儲過程server

SELECT *
FROM   INFORMATION_SCHEMA.ROUTINES
WHERE  ROUTINE_DEFINITION LIKE '%Fetion%'
ORDER BY SPECIFIC_NAME事務

三、刪除主鍵-更改類型-添加主鍵資源

ALTER TABLE entry_baseinfo drop pk_entry_baseinfo字符串

ALTER TABLE entry_baseinfo ALTER COLUMN ID NVARCHAR(36) NOT NULLstring

ALTER TABLE entry_baseinfo ADD CONSTRAINT pk_entry_baseinfo PRIMARY KEY (ID,,UPDATE_ITEM)it

四、遊標io

-聲明一個遊標
DECLARE MyCursor CURSOR FOR
SELECT A.Id
FROM dbo.tmp_qiye_final A
LEFT JOIN dbo.School B
ON A.Name = B.Name
WHERE A.SchoolCode IS NULL
AND EducationType = '大學'table

DECLARE @SchoolCode varchar(50)

--打開一個遊標 
OPEN MyCursor

--循環一個遊標
DECLARE @Id int
 FETCH NEXT FROM MyCursor INTO @Id
WHILE @@FETCH_STATUS = 0
 BEGIN
  SELECT @SchoolCode = RIGHT('000000000' + CONVERT(varchar, (MAX(Code) + 1)), 9) FROM dbo.School WHERE [Level] = 1 AND AlumniSchoolType = 0  
  UPDATE dbo.tmp_qiye_final SET SchoolCode = @SchoolCode WHERE Id = @Id  
  FETCH NEXT FROM  MyCursor INTO @Id
 END 

--關閉遊標
CLOSE MyCursor
--釋放資源
DEALLOCATE MyCursor

五、查看當前鏈接用戶

SP_WHO

SP_WHO 'YOUR DATABASE USER NAME'

六、查看統計信息

SET STATISTICS IO ON

SET STATISTICS TIME ON

SET STATISTICS PROFILE ON

七、表值函數,經過分隔符將字符串轉換成表

Create function [dbo].[SplitToTable]
(
  @String varchar(8000),
  @Seprate varchar(10)
)
returns @TempTable table(F1 varchar(100))
as
begin
  declare @i int
   set @String=rtrim(ltrim(@String))
   set @i=charindex(@Seprate,@String)
   while @i>0
   begin
     insert @TempTable values(left(@String,@i-1))
     set @String=substring(@String,@i+1,len(@String)-@i)
     set @i=charindex(@Seprate,@String)
   end
   if (LEN(@String) > 0)
     insert @TempTable values(@String)
   return
end

八、。。。。。。

相關文章
相關標籤/搜索