Advertisement
irvan_herz

Untitled

Sep 23rd, 2024
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.41 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.     --tidak update batal
  16.     SET @sql = @sql + '
  17.     SELECT
  18.         ''Tidak Update Jadi Batal'' AS TYPE,
  19.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  20.         ''' + @dbname + ''' AS DBPATHERA,
  21.         a.TGL AS TGL_WEBHOOK,
  22.         a.CUST,
  23.         a.SHOPID,
  24.         c.TIDAK_HIT_UANG_R,
  25.         b.NO_BUKTI,
  26.         b.NO_REFERENSI,
  27.         b.STATUS_TRANSAKSI,
  28.         b.PRINT_COUNT,
  29.         a.JSON AS JSON
  30.     FROM [' + @dbname + ']..TABEL_WEBHOOK_TOKPED a(nolock)
  31.     INNER JOIN [' + @dbname + ']..SOT01A b(nolock) ON a.ORDERID=substring(no_referensi,0, charindex('';'',no_referensi)) and a.CUST=b.CUST
  32.     LEFT JOIN [' + @dbname + ']..ARF01 c(nolock) ON a.CUST=c.CUST
  33.     WHERE
  34.         status_transaksi not in (''11'',''12'') and
  35.         a.ORDER_STATUS in (''0'',''3'',''5'',''6'',''10'',''15'') and b.NO_BUKTI like ''%TP%''
  36.     UNION ALL'
  37.  
  38.     --tidak update selesai
  39.     SET @sql = @sql + '
  40.     SELECT
  41.         ''Tidak Update Jadi Selesai'' AS TYPE,
  42.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  43.         ''' + @dbname + ''' AS DBPATHERA,
  44.         a.TGL AS TGL_WEBHOOK,
  45.         a.CUST,
  46.         a.SHOPID,
  47.         c.TIDAK_HIT_UANG_R,
  48.         b.NO_BUKTI,
  49.         b.NO_REFERENSI,
  50.         b.STATUS_TRANSAKSI,
  51.         b.PRINT_COUNT,
  52.         a.JSON AS JSON
  53.     FROM [' + @dbname + ']..TABEL_WEBHOOK_TOKPED a(nolock)
  54.     INNER JOIN [' + @dbname + ']..SOT01A b(nolock) ON a.ORDERID=substring(no_referensi,0, charindex('';'',no_referensi)) and a.CUST=b.CUST
  55.     LEFT JOIN [' + @dbname + ']..ARF01 c(nolock) ON a.CUST=c.CUST
  56.     WHERE
  57.         ((ISNULL(al_cust, '''') <> '''' AND status_transaksi <> ''04'') OR (ISNULL(al_cust, '''') = '''' AND status_transaksi <> ''01'')) and
  58.         a.ORDER_STATUS in (''700'',''701'') and b.NO_BUKTI like ''%tp%''
  59.     UNION ALL'
  60.  
  61.     --tidak update siap kirim
  62.     SET @sql = @sql + '
  63.     SELECT
  64.         ''Tidak Update Jadi Siap Kirim'' AS TYPE,
  65.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  66.         ''' + @dbname + ''' AS DBPATHERA,
  67.         a.TGL AS TGL_WEBHOOK,
  68.         a.CUST,
  69.         a.SHOPID,
  70.         c.TIDAK_HIT_UANG_R,
  71.         b.NO_BUKTI,
  72.         b.NO_REFERENSI,
  73.         b.STATUS_TRANSAKSI,
  74.         b.PRINT_COUNT,
  75.         a.JSON AS JSON
  76.     FROM [' + @dbname + ']..TABEL_WEBHOOK_TOKPED a(nolock)
  77.     INNER JOIN [' + @dbname + ']..SOT01A b(nolock) ON a.ORDERID=substring(no_referensi,0, charindex('';'',no_referensi)) and a.CUST=b.CUST
  78.     LEFT JOIN [' + @dbname + ']..ARF01 c(nolock) ON a.CUST=c.CUST
  79.     WHERE
  80.         isnull(print_count,'''') not in (''1'',''2'') and
  81.         a.ORDER_STATUS in (''400'',''450'') and b.NO_BUKTI like ''%tp%''
  82.     UNION ALL'
  83.  
  84.     --tidak masuk
  85.     SET @sql = @sql + '
  86.     SELECT
  87.         ''Tidak Masuk'' AS TYPE,
  88.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  89.         ''' + @dbname + ''' AS DBPATHERA,
  90.         a.TGL AS TGL_WEBHOOK,
  91.         a.CUST,
  92.         a.SHOPID,
  93.         c.TIDAK_HIT_UANG_R,
  94.         b.NO_BUKTI,
  95.         b.NO_REFERENSI,
  96.         b.STATUS_TRANSAKSI,
  97.         b.PRINT_COUNT,
  98.         a.JSON AS JSON
  99.     FROM [' + @dbname + ']..TABEL_WEBHOOK_TOKPED a(nolock)
  100.     LEFT JOIN [' + @dbname + ']..SOT01A b(nolock) ON a.ORDERID=substring(no_referensi,0, charindex('';'',no_referensi)) and a.CUST=b.CUST
  101.     LEFT JOIN [' + @dbname + ']..ARF01 c(nolock) ON a.CUST=c.CUST
  102.     WHERE a.tgl > ''2024-08-01'' and a.ORDER_STATUS=''220'' and b.NO_BUKTI is null
  103.     UNION ALL'
  104.  
  105.     --dobel
  106.     SET @sql = @sql + '
  107.     SELECT
  108.         ''Dobel'' AS TYPE,
  109.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  110.         ''' + @dbname + ''' AS DBPATHERA,
  111.         a.TGL AS TGL_WEBHOOK,
  112.         a.CUST,
  113.         a.SHOPID,
  114.         NULL,
  115.         NULL,
  116.         c.NO_REFERENSI,
  117.         NULL,
  118.         NULL,
  119.         a.JSON AS JSON
  120.     FROM [' + @dbname + ']..TABEL_WEBHOOK_TOKPED a(nolock)
  121.     INNER JOIN (
  122.         SELECT CUST, NO_REFERENSI, COUNT(*) AS DUP
  123.         FROM [' + @dbname + ']..SOT01A b(nolock)
  124.         GROUP BY CUST, NO_REFERENSI
  125.     ) c ON a.ORDERID=substring(c.no_referensi,0, charindex('';'',c.no_referensi)) and a.CUST=c.CUST
  126.     WHERE a.tgl > ''2024-08-01'' and a.ORDER_STATUS=''220'' AND c.DUP > 1
  127.  
  128.     UNION ALL'
  129.     FETCH NEXT FROM db_cursor INTO @dbname
  130. END
  131.  
  132. -- Remove the last 'UNION ALL'
  133. SET @sql = LEFT(@sql, LEN(@sql) - 10)
  134.  
  135. -- Execute the dynamic SQL
  136. EXEC sp_executesql @sql
  137.  
  138. -- Cleanup
  139. CLOSE db_cursor
  140. DEALLOCATE db_cursor
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement