1、1=1,1=2的使用,在SQL语句组合时用的较多“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 SHRINKFILE2、压缩数据库
dbcc shrinkdatabase(dbname) 3、转移数据库给新用户以已存在用户权限
exec sp_change_users_login 'update_one','newname','oldname'
go 4、检查备份集
RESTORE VERIFYONLY from disk='E:\dvbbs.bak' 5、修复数据库
ALTER DATABASE [dvbbs] SET SINGLE_USER
GO
DBCC CHECKDB('dvbbs',repair_allow_data_loss) WITH TABLOCK
GO
ALTER DATABASE [dvbbs] SET MULTI_USER
GO 6、说明:更改某个表
exec sp_changeobjectowner 'tablename','dbo'7、存储更改全部表CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch
@OldOwner as NVARCHAR(128),
@NewOwner as NVARCHAR(128)
ASDECLARE @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 nameOPEN 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,@OldOwnerFETCH NEXT FROM curObject INTO @Name, @Owner
ENDclose curObject
deallocate curObject
GO 8、SQL SERVER中直接循环写入数据
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 69while((select min(score) from tb_table)<60)beginupdate tb_table set score =score*1.01where score<60if (select min(score) from tb_table)>60 break else continueend
标签:语句,set,Name,SQL,128,curObject,where,select,大全 From: https://blog.51cto.com/u_15333581/7397312