常用经典SQL语句大全(技巧)

三、技巧

1、1=1,1=2的使用,在SQL语句组合时用的较多

“where 1=1” 是表示选择全部    “where 1=2”全部不选,

如:

if

@strWhere !=‘‘

begin

set @strSQL = ‘select count(*) as Total from [‘ + @tblName + ‘] where ‘ [email protected]

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

3、压缩数据库

dbccshrinkdatabase(dbname)

4、转移数据库给新用户以已存在用户权限

execsp_change_users_login ‘update_one‘,‘newname‘,‘oldname‘ go

5、检查备份集

RESTORE VERIFYONLY fromdisk=‘E:\dvbbs.bak‘

6、修复数据库

ALTER DATABASE [dvbbs]SET SINGLE_USER

GO

DBCC CHECKDB(‘dvbbs‘,repair_allow_data_loss) WITH TABLOCK

GO

ALTER DATABASE [dvbbs] SET MULTI_USER

GO

7、日志清除

SET NOCOUNT ON DECLARE @LogicalFileName sysname,

@MaxMinutes INT,

@NewSize INT

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 notexpired  AND @OriginalSize = (SELECT size FROMsysfiles 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 pagesor ‘ +  CONVERT(VARCHAR(30),(size*8/1024)) +‘MB‘  FROM sysfiles  WHERE name = @LogicalFileName DROP TABLE DummyTrans SET NOCOUNT OFF

8、说明:更改某个表

exec sp_changeobjectowner‘tablename‘,‘dbo‘

9、存储更改全部表

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)[email protected] order by name

OPEN   curObject FETCH NEXT FROM curObject INTO @Name, @Owner WHILE(@@FETCH_STATUS=0) BEGIN      if @[email protected] 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 curObject GO

10、SQL SERVER中直接循环写入数据

declare @i int set @i=1 while @i<30 begin

insert into test (userid) values(@i)

set @[email protected]+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

where score<60

if (select min(score) from tb_table)>60

 break

 else

   continue

end

时间: 2024-12-14 01:51:31

常用经典SQL语句大全(技巧)的相关文章

常用经典SQL语句大全完整版--详解+实例 《来自网络,很全没整理,寄存与此》

常用经典SQL语句大全完整版--详解+实例 下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop data

常用经典SQL语句大全(基础)

一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sqlserver --- 创建 备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始 备份 BACKUP DATABASE pubs TO testBack 4.说明:创建新

常用经典SQL语句大全(提升)

二.提升 1.说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用) 法一:select * into b from a where 1<>1(仅用于SQlServer) 法二:select top 0 * into b from a 2.说明:拷贝表(拷贝数据,源表名:a 目标表名:b)(Access可用) insert into b(a, b, c)select d,e,f from b; 3.说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用) in

经典SQL语句大全以及50个常用的sql语句

经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数据的 deviceUSE masterEXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'--- 开始 备份BACKUP DATABASE pubs TO testBack 4.说明:

经典SQL语句大全一

<span style="font-size:14px;">一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始 备份

SQL之经典SQL语句大全

经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数据的 deviceUSE masterEXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'--- 开始 备份BACKUP DATABASE pubs TO testBack 4.说明:

经典SQL语句大全(绝对的经典-收藏)

原文连接 https://www.cnblogs.com/1234abcd/p/5530314.html 经典SQL语句大全(绝对的经典) 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数据的 deviceUSE masterEXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwin

经典SQL语句大全(网络资源共享)

下列语句部分是Mssql语句,不可以在access中使用.  SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)  DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)  DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 首先,简要介绍基础语句: 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname 3.说明:备份

经典SQL语句大全(转)

SQL语句参考,包含Access.MySQL 以及 SQL Server 基础 创建数据库 CREATE DATABASE database-name 删除数据库 drop database dbname 备份sql server 创建 备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' 开始 备份 BACKUP DATABASE pubs TO test