国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

整理的一些數(shù)據(jù)庫不容易想到的SQL語句實(shí)例一

2024-07-21 02:47:28
字體:
供稿:網(wǎng)友
整理的一些數(shù)據(jù)庫不容易想到的SQL語句實(shí)例一

1、行轉(zhuǎn)列SQL語句

SELECT  *FROM    ( SELECT    [FID] ,                    [Weeks] ,                    [Qty]          FROM      dbo.TempTable where Weeks is not null         ) p PIVOT( SUM([Qty]) FOR [Weeks] IN ([1],[2],[3],[4],[5],[6])) AS pvt

2、游標(biāo)

declare P_PRoduct_Main CURSOR FAST_FORWARD FORSELECT PID FROM [表名] order by PID-- 打開游標(biāo).   OPEN P_Product_Main   --填充數(shù)據(jù).   FETCH NEXT FROM P_Product_Main INTO @PID   --假如檢索到了數(shù)據(jù),才處理.   WHILE @@fetch_status = 0   begin      print @PID;      end   fetch next from P_Product_Main into @PID;   end   -- 關(guān)閉游標(biāo)   CLOSE P_Product_Main   --釋放游標(biāo).   DEALLOCATE P_Product_Main

3、查看當(dāng)前進(jìn)程,或死鎖進(jìn)程,并能自動殺掉死進(jìn)程,因?yàn)槭轻槍λ赖?所以如果有死鎖進(jìn)程,只能查看死鎖進(jìn)程,當(dāng)然,你可以通過參數(shù)控制,不管有沒有死鎖,都只查看死鎖進(jìn)程

create proc [dbo].[sp_lock2]@kill_lock_spid bit=1,  --是否殺掉死鎖的進(jìn)程,1 殺掉, 0 僅顯示@show_spid_if_nolock bit=1 --如果沒有死鎖的進(jìn)程,是否顯示正常進(jìn)程信息,1 顯示,0 不顯示asdeclare @count int,@s nvarchar(1000),@i intselect id=identity(int,1,1),標(biāo)志, 進(jìn)程ID=spid,線程ID=kpid,塊進(jìn)程ID=blocked,數(shù)據(jù)庫ID=dbid, 數(shù)據(jù)庫名=db_name(dbid),用戶ID=uid,用戶名=loginame,累計CPU時間=cpu, 登陸時間=login_time,打開事務(wù)數(shù)=open_tran, 進(jìn)程狀態(tài)=status, 工作站名=hostname,應(yīng)用程序名=program_name,工作站進(jìn)程ID=hostprocess, 域名=nt_domain,網(wǎng)卡地址=net_addressinto #t from( select 標(biāo)志='死鎖的進(jìn)程',  spid,kpid,a.blocked,dbid,uid,loginame,cpu,login_time,open_tran,  status,hostname,program_name,hostprocess,nt_domain,net_address,  s1=a.spid,s2=0 from master..sysprocesses a join (  select blocked from master..sysprocesses group by blocked  )b on a.spid=b.blocked where a.blocked=0 union all select '|_犧牲品_>',  spid,kpid,blocked,dbid,uid,loginame,cpu,login_time,open_tran,  status,hostname,program_name,hostprocess,nt_domain,net_address,  s1=blocked,s2=1 from master..sysprocesses a where blocked<>0)a order by s1,s2 select @count=@@rowcount,@i=1 if @count=0 and @show_spid_if_nolock=1begin insert #t select 標(biāo)志='正常的進(jìn)程',  spid,kpid,blocked,dbid,db_name(dbid),uid,loginame,cpu,login_time,  open_tran,status,hostname,program_name,hostprocess,nt_domain,net_address from master..sysprocesses set @count=@@rowcountend if @count>0begin create table #t1(id int identity(1,1),a nvarchar(30),b Int,EventInfo nvarchar(255)) if @kill_lock_spid=1 begin  declare @spid varchar(10),@標(biāo)志 varchar(10)  while @i<=@count  begin   select @spid=進(jìn)程ID,@標(biāo)志=標(biāo)志 from #t where id=@i   insert #t1 exec('dbcc inputbuffer('+@spid+')')   if @標(biāo)志='死鎖的進(jìn)程' exec('kill '+@spid)   set @i=@i+1  end end else  while @i<=@count  begin   select @s='dbcc inputbuffer('+cast(進(jìn)程ID as varchar)+')' from #t where id=@i   insert #t1 exec(@s)   set @i=@i+1  end select a.*,進(jìn)程的SQL語句=b.EventInfo from #t a join #t1 b on a.id=b.idend
create Procedure [dbo].[sp_who_lock]asbegindeclare @spid int,@bl int,@intTransactionCountOnEntry int,        @intRowcount int,        @intCountProperties int,        @intCounter intset nocount oncreate table #tmp_lock_who (id int identity(1,1),spid smallint,bl smallint)IF @@ERROR<>0 RETURN @@ERRORinsert into #tmp_lock_who(spid,bl) select  0 ,blocked  from (select * from master.dbo.sysprocesses where  blocked>0 ) a   where not exists(select * from (select * from master.dbo.sysprocesses where  blocked>0 ) b   where a.blocked=spid)  union select spid,blocked from master.dbo.sysprocesses where  blocked>0 IF @@ERROR<>0 RETURN @@ERROR -- 找到臨時表的記錄數(shù)select @intCountProperties = Count(*),@intCounter = 1from #tmp_lock_whoIF @@ERROR<>0 RETURN @@ERRORif@intCountProperties=0select '現(xiàn)在沒有阻塞和死鎖信息' as message -- 循環(huán)開始while @intCounter <= @intCountPropertiesbegin-- 取第一條記錄select @spid = spid,@bl = blfrom #tmp_lock_who where Id = @intCounter begin if @spid =0             select '引起數(shù)據(jù)庫死鎖的是: '+ CAST(@bl AS VARCHAR(10)) + '進(jìn)程號,其執(zhí)行的SQL語法如下' else            select '進(jìn)程號SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '進(jìn)程號SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其當(dāng)前進(jìn)程執(zhí)行的SQL語法如下' DBCC INPUTBUFFER (@bl )end -- 循環(huán)指針下移set @intCounter = @intCounter + 1end  drop table #tmp_lock_whoset nocount offreturn 0end

4、 臨時表

select top(100) *into #temp_tablefrom WOTable  as awhere StartTime=@startdate and not exists (select ID from temp_WOCode where ID=a.ID and Status=0) and [Status]=0 and CurrentStatus<>999update a set a.BurdeningStatus=20,a.BurdeningDateTime=GETDATE(),a.BurdeningPerson=@userid  from   WOTable    as a  where exists (select ID from  #temp_table where ID=a.ID and Status=0)

5、事務(wù)

begin tran  --開始事務(wù)BEGIN TRY        if @@trancount <>0beginprint 'has data, commit tran'commit tranendEND TRYBEGIN CATCHif @@trancount <>0beginrollback tranendEND CATCH

6、索引

//普通索引CREATE INDEX <索引的名字> ON tablename (列名);//唯一索引CREATE UNIQUE INDEX <索引的名字> ON tablename (列名);drop index <索引的名字>


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 梁河县| 新乡市| 南木林县| 安徽省| 留坝县| 玛纳斯县| 永定县| 东海县| 江西省| 敖汉旗| 长兴县| 自贡市| 日土县| 富民县| 怀集县| 井冈山市| 淮南市| 区。| 桓台县| 祁连县| 长泰县| 呈贡县| 日喀则市| 盐池县| 瓦房店市| 内丘县| 石城县| 蒙山县| 隆安县| 沙坪坝区| 兴安县| 惠来县| 南澳县| 鹿邑县| 伊金霍洛旗| 宜州市| 绥中县| 克山县| 东兰县| 元谋县| 伊宁市|