You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
sql-tools/查询一个服务中今天有多少个数据库营业.sql

84 lines
2.2 KiB

IF OBJECT_ID('Tempdb..#Database') IS NOT NULL
DROP TABLE #Database
CREATE TABLE #Database
(
DBName VARCHAR(100),
BillCount INT
)
DECLARE @database VARCHAR(200)
DECLARE @index int
SET @index = 1
DECLARE dbList cursor for
SELECT name
from master..sysdatabases
WHERE STATUS & 512 != 512
-- AND name IN
-- ( SELECT SUBSTRING(FConnectionString,
-- CHARINDEX('Initial Catalog=',FConnectionString) + LEN('Initial Catalog='),
-- CHARINDEX(';Integrated Security',FConnectionString) - (CHARINDEX('Initial Catalog=',FConnectionString) + LEN('Initial Catalog=')))
-- AS FDbName
-- FROM qd108.saidaodns.dbo.TClientServerConfig AS A,
-- qd108.saidaodns.dbo.TClientInfo AS C,
-- qd108.saidaodns.dbo.TServerInfo AS S
-- WHERE A.FClientId = C.FClientId
-- AND C.FServerId = S.FServerId
-- -- AND S.FHostId = 'qd11'
--)
OPEN dbList
FETCH dbList into @database
while @@FETCH_STATUS=0
BEGIN
DECLARE @CheckSql NVARCHAR(1000)
SELECT @CheckSql = 'SELECT @Biz = (SELECT count(1) FROM ' + @database + '..sysobjects WHERE name = ''TCompany'')'
DECLARE @biz BIT
EXEC sp_executesql @CheckSql,N'@biz BIT out ',@biz out;
SELECT @biz = 0 WHERE @biz IS NULL
IF @biz = 0
BEGIN
FETCH dbList into @database
CONTINUE
END
PRINT @database
DECLARE @sql VARCHAR(MAX)
EXEC ('use '+ @database +
'
DECLARE @Count INT
SELECT @Count = COUNT(1) FROM TConsumeBill WHERE FWorkDate = CONVERT(VARCHAR,GETDATE(),23)
IF ISNULL(@Count,0) > 0
INSERT #Database SELECT db_name(),@Count
--print 11
')
--DECLARE @cmd VARCHAR(2000)
--SELECT @cmd = 'osql -E -d' + @database + ' -iE:\update.sql'
--EXEC xp_cmdshell @cmd
PRINT ''
FETCH dbList INTO @database
SET @Index = @Index + 1
END
CLOSE dbList
DEALLOCATE dbList
PRINT 'END'
SELECT DBName AS <EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>,BillCount AS <EFBFBD><EFBFBD><EFBFBD>ѵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> FROM #Database
DROP TABLE #Database