“where 1= 1” 是表示选择全部
如:
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
3、压缩数据库
dbcc shrinkdatabase(dbname)
4、转移数据库给新用户以已存在用户权限
exec sp_change_users_login 'update_one','newname','oldname'
go
5、检查备份集
RESTORE VERIFYONLY from disk='E:\dvbbs.bak'
7、日志清除
SET NOCOUNT ON
DECLARE @LogicalFileName sysname,
USE tablename -- 要操作的数据库名
SELECT
@MaxMinutes = 10, -- Limit on time allowed to wrap log.
Setup / initialize
DECLARE @OriginalSize int
SELECT @OriginalSize = size
SELECT 'Original Size of ' + db_name() + ' LOG is ' +
CREATE TABLE DummyTrans
DECLARE @Counter
SELECT @StartTime = GETDATE(),
DBCC SHRINKFILE (@LogicalFileName, @NewSize)
EXEC (@TruncLog)
-- Wrap the log if necessary.
WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired
SELECT @Counter = 0
SELECT 'Final Size of ' + db_name() + ' LOG is ' +
DROP TABLE DummyTrans
SET NOCOUNT OFF
8、说明:更改某个表
exec sp_changeobjectowner 'tablename','dbo'
CREATE PROCEDURE dbo.User_ChangeObjectOwnerBatch
@OldOwner as NVARCHAR(128),
@NewOwner as NVARCHAR(128)
AS
DECLARE @Name
DECLARE @Owner
DECLARE @OwnerName
DECLARE curObject CURSOR FOR
select 'Name'
from sysobjects
where user_name(uid)=@OldOwner
order by name
OPEN
FETCH NEXT FROM curObject INTO @Name, @Owner
WHILE(@@FETCH_STATUS=0)
BEGIN
if @Owner=@OldOwner
begin
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
end
案例:
有如下表,要求就裱中所有沒有及格的成績,在每次增長0.1的基礎上,使他們剛好及格:
while((select min(score) from tb_table)<60)
begin
update tb_table set score =score*1.01
where score<60
if
end