---擷取SQL Server資料庫裡表的佔用容量大小 CREATE PROCEDURE get_tableinfo AS if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablespaceinfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) create table tablespaceinfo
1.所有記錄的分頁:SELECT TOP 頁大小 *FROM UsersWHERE (ID NOT IN (SELECT TOP (頁大小*(頁數-1)) ID FROM Users ORDER BY ID DESC)) //skip(頁大小*(頁數-1)) 條記錄ORDER BY ID DESC 2.符合條件記錄的分頁(注意此時你的查詢條件要分布在兩個查詢語句中,謹記)SELECT TOP 頁大小 *FROM UsersWHERE+你的查詢條件 AND ( ID NOT IN
需求貼:http://topic.csdn.net/u/20120204/10/8b902fd2-8909-4ed9-b534-4a1a72454eff.html#r_77443331需求簡介:比如:YYYYN, 結果處理為:人1,人2,人3,人4分析:貌似就是一個字串的替換,如果是'Y'替換成'人'+Y的所在位置的編號。解決方案:declare @T table (id int,col varchar(5))insert into @Tselect 1,'YYYYN' union
最佳化的ms sql server分頁sql語句特點:一次查詢,資料庫只返回一頁的資料。而不是取出所有的資料。說明:pagesize: 每頁顯示記錄數cureentpage:當前頁數select * from ( select TOP pagesize * FROM ( SELECT TOP pagesize*cureentpage * from user_table ORDER BY id ASC ) as aSysTable ORDER BY id DESC ) as
1.對查詢進行最佳化,應盡量避免全表掃描,首先應考慮在 where 及 order by 涉及的列上建立索引。2.應盡量避免在 where 子句中對欄位進行 null 值判斷,否則將導致引擎放棄使用索引而進行全表掃描,如:select id from t where num is null可以在num上設定預設值0,確保表中num列沒有null值,然後這樣查詢:select id from t where num=03.應盡量避免在 where
==產生數字區間隨機數==--建立視圖 create view myview as select re=rand() --自訂函數:取得指定範圍的隨機數 create function mydata( @a int, @b int) returns decimal(38,0) as begin declare @r decimal(38,0) select @r=cast(re*(@b-@a)+@a as decimal(38,0)) from myview return(@r) end go