經典SQL語句大全
經典SQL語句大全
包括:基礎、提升及技巧等幾個部分。
[@more@]
1’,’值2’,’值4’,’值6’)
、說明:一條sql 語句搞定資料庫分頁select top 10 b.* from (select top 20 主鍵欄位,排序欄位 from 表名 order by 排序欄位 desc) a,表名 b where b.主鍵欄位 = a.主鍵欄位 order by a.排序欄位具體實現:關於資料庫分頁:
declare @start int,@end int
@sql nvarchar(600)
set @sql=’select top’+str(@end-@start+1)+’+from T where rid not in(select top’+str(@str-1)+’Rid from T where Rid>-1)’
exec sp_executesql @sql
注意:在top後不能直接跟一個變數,所以在實際應用中只有這樣的進行特殊的處理。Rid為一個標識列,如果top後還有具體的欄位,這樣做是非常有好處的。因為這樣可以避免 top的欄位如果是邏輯索引的,查詢的結果後實際表中的不一致(邏輯索引中的資料有可能和資料表中的不一致,而查詢時如果處在索引則首先查詢索引)
alter table tablename--新增一個自增列add column_b int identity(1,1)
delete from tablename where column_b not in(
select max(column_b) from tablename group by column1,column2,...)
alter table tablename drop column column_b
TRUNCATE TABLE table1
“where 1=1” 是表示選擇全部 “where 1=2”全部不選,如:if @strWhere !=''
begin
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where ' + @strWhere
end
else
begin
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'
end
我們可以直接寫成
錯誤!未找到目錄項。
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 安定 '+ @strWhere 2、收縮資料庫--重建索引
DBCC REINDEX
DBCC INDEXDEFRAG
--收縮資料和日誌
DBCC SHRINKDB
DBCC SHRINKFILE
USE tablename -- 要操作的資料庫名
SELECT @LogicalFileName = 'tablename_log', -- 日誌檔名
@MaxMinutes = 10, -- Limit on time allowed to wrap log.
@NewSize = 1 -- 你想設定的日誌檔案的大小(M)
Setup / initialize
DECLARE @OriginalSize int
SELECT @OriginalSize = size
FROM sysfiles
WHERE name = @LogicalFileName
SELECT 'Original Size of ' + db_name() + ' LOG is ' +
CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' +
CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB'
FROM sysfiles
WHERE name = @LogicalFileName
CREATE TABLE DummyTrans
(DummyColumn char (8000) not null)
DECLARE @Counter INT,
@StartTime DATETIME,
@TruncLog VARCHAR(255)
SELECT @StartTime = GETDATE(),
@TruncLog = 'BACKUP LOG ' + db_name() + ' WITH TRUNCATE_ONLY'
DBCC SHRINKFILE (@LogicalFileName, @NewSize)
EXEC (@TruncLog)
-- Wrap the log if necessary.
WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired
AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName)
AND (@OriginalSize * 8 /1024) > @NewSize
BEGIN -- Outer loop.
SELECT @Counter = 0
WHILE ((@Counter < @OriginalSize / 16) AND (@Counter < 50000))
BEGIN -- update
INSERT DummyTrans VALUES ('Fill Log') DELETE DummyTrans
SELECT @Counter = @Counter + 1
END
EXEC (@TruncLog)
END
SELECT 'Final Size of ' + db_name() + ' LOG is ' +
CONVERT(VARCHAR(30),size) + ' 8K pages or ' +
CONVERT(VARCHAR(30),(size*8/1024)) + 'MB'
FROM sysfiles
WHERE name = @LogicalFileName
DROP TABLE DummyTrans
SET NOCOUNT OFF
CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch
@OldOwner as NVARCHAR(128),
@NewOwner as NVARCHAR(128)
AS
DECLARE @Name as NVARCHAR(128)
DECLARE @Owner as NVARCHAR(128)
DECLARE @OwnerName as NVARCHAR(128)
DECLARE curObject CURSOR FOR
select 'Name' = name,
'Owner' = user_name(uid)
from sysobjects
where user_name(uid)=@OldOwner
order by name
OPEN curObject
FETCH NEXT FROM curObject INTO @Name, @Owner
WHILE(@@FETCH_STATUS=0)
BEGIN
if @Owner=@OldOwner
begin
set @OwnerName = @OldOwner + '.' + rtrim(@Name)
exec sp_changeobjectowner @OwnerName, @NewOwner
end
-- select @name,@NewOwner,@OldOwner
FETCH NEXT FROM curObject INTO @Name, @Owner
END
close curObject
deallocate curObjectGO
declare @i int
set @i=1
while @i<30
begin insert into test (userid) values(@i) set @i=@i+1
end案例:有如下表,要求就裱中所有沒有及格的成績,在每次增長0.1的基礎上,使他們剛好及格:
Name score
Zhangshan 80
Lishi 59
Wangwu 50
Songquan 69
while((select min(score) from tb_table)<60)
begin
update tb_table set score =score*1.01
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14663377/viewspace-1034857/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Sa提權Sql語句大全SQL
- 經典語句(L篇)
- 資料庫常用的sql語句大全--sql資料庫SQL
- “四書五經”經典語句
- SQL語句大全,你需要的SQL在這裡SQL
- 15000 字的 SQL 語句大全,收藏落灰款!SQL
- Java中如何解析SQL語句、格式化SQL語句、生成SQL語句?JavaSQL
- SQL語句SQL
- 【SQL】9 SQL INSERT INTO 語句SQL
- 【SQL】10 SQL UPDATE 語句SQL
- 【SQL】11 SQL DELETE 語句SQLdelete
- SQL SELECT 語句SQL
- sql常用語句SQL
- Oracle資料庫語句大全Oracle資料庫
- 經典程式設計書籍大全程式設計
- 1.3. SQL 語句SQL
- Oracle基本SQL語句OracleSQL
- Sql語句小整理SQL
- SQL語句優化SQL優化
- SQL語句IN的用法SQL
- SQL 語句學習SQL
- 標準SQL語句大全【持續更新】(navicat12版親測有效)SQL
- mysql建表常用sql語句個人經驗分享MySql
- PHP+MySQL 手工注入語句大全PHPMySql
- Oracle SQL精妙SQL語句講解OracleSQL
- 【SQL】14 UNION 操作符、SELECT INTO 語句、INSERT INTO SELECT 語句、CREATE DATABASE 語句、CREATE TABLE 語句SQLDatabase
- 執行一條sql語句都經歷了什麼?SQL
- 工作中,我們經常用到哪些SQL語句呢?SQL
- SQL語言基礎(SELECT語句)SQL
- MYSQL SQL語句優化MySql優化
- springboot 中列印 sql 語句Spring BootSQL
- 【資料庫】SQL語句資料庫SQL
- sql查詢語句流程SQL
- SQL mother查詢語句SQL
- 常用sql進階語句SQL
- SQL 常用語句一覽SQL
- SQL查詢語句 (Oracle)SQLOracle
- SQL語句最佳化SQL
- 統計介面sql語句SQL