1、行轉列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、游標
declare P_PRoduct_Main CURSOR FAST_FORWARD FORSELECT PID FROM [表名] order by PID-- 打開游標. OPEN P_Product_Main --填充數據. FETCH NEXT FROM P_Product_Main INTO @PID --假如檢索到了數據,才處理. WHILE @@fetch_status = 0 begin print @PID; end fetch next from P_Product_Main into @PID; end -- 關閉游標 CLOSE P_Product_Main --釋放游標. DEALLOCATE P_Product_Main
3、查看當前進程,或死鎖進程,并能自動殺掉死進程,因為是針對死的,所以如果有死鎖進程,只能查看死鎖進程,當然,你可以通過參數控制,不管有沒有死鎖,都只查看死鎖進程
create proc [dbo].[sp_lock2]@kill_lock_spid bit=1, --是否殺掉死鎖的進程,1 殺掉, 0 僅顯示@show_spid_if_nolock bit=1 --如果沒有死鎖的進程,是否顯示正常進程信息,1 顯示,0 不顯示asdeclare @count int,@s nvarchar(1000),@i intselect id=identity(int,1,1),標志, 進程ID=spid,線程ID=kpid,塊進程ID=blocked,數據庫ID=dbid, 數據庫名=db_name(dbid),用戶ID=uid,用戶名=loginame,累計CPU時間=cpu, 登陸時間=login_time,打開事務數=open_tran, 進程狀態=status, 工作站名=hostname,應用程序名=program_name,工作站進程ID=hostprocess, 域名=nt_domain,網卡地址=net_addressinto #t from( select 標志='死鎖的進程', 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 標志='正常的進程', 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),@標志 varchar(10) while @i<=@count begin select @spid=進程ID,@標志=標志 from #t where id=@i insert #t1 exec('dbcc inputbuffer('+@spid+')') if @標志='死鎖的進程' exec('kill '+@spid) set @i=@i+1 end end else while @i<=@count begin select @s='dbcc inputbuffer('+cast(進程ID as varchar)+')' from #t where id=@i insert #t1 exec(@s) set @i=@i+1 end select a.*,進程的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 -- 找到臨時表的記錄數select @intCountProperties = Count(*),@intCounter = 1from #tmp_lock_whoIF @@ERROR<>0 RETURN @@ERRORif@intCountProperties=0select '現在沒有阻塞和死鎖信息' as message -- 循環開始while @intCounter <= @intCountPropertiesbegin-- 取第一條記錄select @spid = spid,@bl = blfrom #tmp_lock_who where Id = @intCounter begin if @spid =0 select '引起數據庫死鎖的是: '+ CAST(@bl AS VARCHAR(10)) + '進程號,其執行的SQL語法如下' else select '進程號SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '進程號SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其當前進程執行的SQL語法如下' DBCC INPUTBUFFER (@bl )end -- 循環指針下移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、事務
begin tran --開始事務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 <索引的名字>
新聞熱點
疑難解答