Advertisement
irvan_herz

Untitled

Jul 15th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. DECLARE @sql NVARCHAR(MAX) = ''
  2. DECLARE @dbname NVARCHAR(255)
  3.  
  4. -- Cursor to iterate over each database
  5. DECLARE db_cursor CURSOR FOR
  6. SELECT name
  7. FROM sys.databases
  8. WHERE state_desc = 'ONLINE' AND name NOT IN ('master', 'tempdb', 'model', 'msdb') AND name LIKE 'ERASOFT_%'
  9.  
  10. OPEN db_cursor
  11. FETCH NEXT FROM db_cursor INTO @dbname
  12.  
  13. WHILE @@FETCH_STATUS = 0
  14. BEGIN
  15. SET @sql = @sql + 'SELECT ''' + @dbname + ''' AS DatabaseName, COUNT(*) AS TOTAL, SUM(CASE WHEN a.NAMA=''15'' THEN 1 ELSE 0 END) AS TOKOPEDIA, SUM(CASE WHEN a.NAMA=''17'' THEN 1 ELSE 0 END) AS SHOPEE, SUM(CASE WHEN a.NAMA=''2021'' THEN 1 ELSE 0 END) AS TIKTOK, SUM(CASE WHEN a.NAMA=''7'' THEN 1 ELSE 0 END) AS LAZADA, SUM(CASE WHEN a.NAMA=''16'' THEN 1 ELSE 0 END) AS BLIBLI, SUM(CASE WHEN a.NAMA<>''15'' AND a.NAMA<>''17'' AND a.NAMA<>''16'' AND a.NAMA<>''7'' AND a.NAMA<>''2021'' THEN 1 ELSE 0 END) AS OTHER FROM [' + @dbname + ']..SOT01A s LEFT JOIN [' + @dbname + ']..ARF01 a ON s.CUST=a.CUST WHERE s.TGL >= ''2024-07-10 00:00:00'' AND s.TGL < ''2024-07-11 00:00:00'' UNION ALL '
  16.  
  17. FETCH NEXT FROM db_cursor INTO @dbname
  18. END
  19.  
  20. -- Remove the last 'UNION ALL'
  21. SET @sql = LEFT(@sql, LEN(@sql) - 10)
  22.  
  23. -- Execute the dynamic SQL
  24. EXEC sp_executesql @sql
  25.  
  26. -- Cleanup
  27. CLOSE db_cursor
  28. DEALLOCATE db_cursor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement