Advertisement
irvan_herz

Untitled

Sep 23rd, 2024
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 5.01 KB | None | 0 0
  1.  
  2. DECLARE @sql NVARCHAR(MAX) = ''
  3. DECLARE @dbname NVARCHAR(255)
  4.  
  5. -- Cursor to iterate over each database
  6. DECLARE db_cursor CURSOR FOR
  7. SELECT name
  8. FROM sys.databases
  9. WHERE state_desc = 'ONLINE' AND name NOT IN ('master', 'tempdb', 'model', 'msdb') AND name LIKE 'ERASOFT_%'
  10.  
  11. OPEN db_cursor
  12. FETCH NEXT FROM db_cursor INTO @dbname
  13.  
  14. WHILE @@FETCH_STATUS = 0
  15. BEGIN
  16.     --tidak update batal
  17.     SET @sql = @sql + '
  18.     SELECT
  19.         ''Tidak Update Jadi Batal'' AS TYPE,
  20.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  21.         ''' + @dbname + ''' AS DBPATHERA,
  22.         a.TGL AS TGL_WEBHOOK,
  23.         a.CUST,
  24.         a.SHOPID,
  25.         c.TIDAK_HIT_UANG_R,
  26.         b.NO_BUKTI,
  27.         b.NO_REFERENSI,
  28.         b.STATUS_TRANSAKSI,
  29.         b.PRINT_COUNT,
  30.         a.JSON AS JSON
  31.     FROM [' + @dbname + ']..TABEL_WEBHOOK_SHOPEE a (nolock)
  32.     INNER JOIN [' + @dbname + ']..SOT01A b(nolock) on a.ORDERID=NO_REFERENSI and a.CUST=b.CUST  
  33.     LEFT JOIN [' + @dbname + ']..ARF01 c(nolock) ON a.CUST=c.CUST
  34.     WHERE status_transaksi not in (''11'',''12'') and
  35.         a.ORDER_STATUS in (''CANCELLED'') and b.NO_BUKTI like ''sp%''
  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_SHOPEE a(nolock)
  54.     INNER JOIN [' + @dbname + ']..SOT01A b(nolock) on a.ORDERID=NO_REFERENSI and a.CUST=b.CUST
  55.     LEFT JOIN [' + @dbname + ']..ARF01 c(nolock) ON a.CUST=c.CUST
  56.     WHERE ((ISNULL(al_cust, '''') <> '''' AND status_transaksi <> ''04'') OR (ISNULL(al_cust, '''') = '''' AND status_transaksi <> ''01'')) and
  57.         a.ORDER_STATUS in (''COMPLETED'') and b.NO_BUKTI like ''sp%''
  58.     UNION ALL'
  59.  
  60.     --tidak update siap kirim
  61.     SET @sql = @sql + '
  62.     SELECT
  63.         ''Tidak Update Jadi Siap Kirim'' AS TYPE,
  64.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  65.         ''' + @dbname + ''' AS DBPATHERA,
  66.         a.TGL AS TGL_WEBHOOK,
  67.         a.CUST,
  68.         a.SHOPID,
  69.         c.TIDAK_HIT_UANG_R,
  70.         b.NO_BUKTI,
  71.         b.NO_REFERENSI,
  72.         b.STATUS_TRANSAKSI,
  73.         b.PRINT_COUNT,
  74.         a.JSON AS JSON
  75.     FROM [' + @dbname + ']..TABEL_WEBHOOK_SHOPEE a(nolock)
  76.     INNER JOIN [' + @dbname + ']..SOT01A b(nolock) on a.ORDERID=NO_REFERENSI and a.CUST=b.CUST
  77.     LEFT JOIN [' + @dbname + ']..ARF01 c(nolock) ON a.CUST=c.CUST
  78.     WHERE isnull(b.STATUS_TRANSAKSI,'''') in (''0'') and
  79.         a.ORDER_STATUS in (''READY_TO_SHIP'',''PROCESSED'',''SHIPPED'',''TO_CONFIRM_RECEIVE'',''READY'') and b.NO_BUKTI like ''sp%''
  80.     UNION ALL'
  81.  
  82.     --tidak update sudah bayar
  83.     SET @sql = @sql + '
  84.     SELECT
  85.         ''Tidak Update Jadi Sudah Bayar'' AS TYPE,
  86.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  87.         ''' + @dbname + ''' AS DBPATHERA,
  88.         a.TGL AS TGL_WEBHOOK,
  89.         a.CUST,
  90.         a.SHOPID,
  91.         c.TIDAK_HIT_UANG_R,
  92.         b.NO_BUKTI,
  93.         b.NO_REFERENSI,
  94.         b.STATUS_TRANSAKSI,
  95.         b.PRINT_COUNT,
  96.         a.JSON AS JSON
  97.     FROM [' + @dbname + ']..TABEL_WEBHOOK_SHOPEE a(nolock)
  98.     INNER JOIN [' + @dbname + ']..SOT01A b(nolock) on a.ORDERID=NO_REFERENSI and a.CUST=b.CUST
  99.     LEFT JOIN [' + @dbname + ']..ARF01 c(nolock) ON a.CUST=c.CUST
  100.         WHERE isnull(STATUS_TRANSAKSI,'''') in (''0'') and
  101.         a.ORDER_STATUS in (''READY_TO_SHIP'',''PROCESSED'',''SHIPPED'',''TO_CONFIRM_RECEIVE'',''READY'') and b.NO_BUKTI like ''sp%''
  102.     UNION ALL'
  103.  
  104.     --tidak masuk
  105.     SET @sql = @sql + '
  106.     SELECT
  107.         ''Tidak Masuk'' AS TYPE,
  108.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  109.         ''' + @dbname + ''' AS DBPATHERA,
  110.         a.TGL AS TGL_WEBHOOK,
  111.         a.CUST,
  112.         a.SHOPID,
  113.         c.TIDAK_HIT_UANG_R,
  114.         b.NO_BUKTI,
  115.         a.ORDERID,
  116.         b.STATUS_TRANSAKSI,
  117.         b.PRINT_COUNT,
  118.         a.JSON AS JSON
  119.     FROM [' + @dbname + ']..TABEL_WEBHOOK_SHOPEE a(nolock)
  120.     LEFT JOIN [' + @dbname + ']..SOT01A b(nolock) ON a.ORDERID=b.NO_REFERENSI and a.CUST=b.CUST
  121.     LEFT JOIN [' + @dbname + ']..ARF01 c(nolock) ON a.CUST=c.CUST
  122.     WHERE a.tgl > ''2024-08-01'' and a.ORDER_STATUS=''UNPAID'' and b.NO_BUKTI is null
  123.     UNION ALL'
  124.    
  125.     --dobel
  126.     SET @sql = @sql + '
  127.     SELECT
  128.         ''Dobel'' AS TYPE,
  129.         (SELECT Email from MO..Account WHERE DatabasePathErasoft='''+ @dbname +''') AS EMAIL,
  130.         ''' + @dbname + ''' AS DBPATHERA,
  131.         a.TGL AS TGL_WEBHOOK,
  132.         a.CUST,
  133.         a.SHOPID,
  134.         NULL,
  135.         NULL,
  136.         c.NO_REFERENSI,
  137.         NULL,
  138.         NULL,
  139.         a.JSON AS JSON
  140.     FROM [' + @dbname + ']..TABEL_WEBHOOK_SHOPEE a(nolock)
  141.     INNER JOIN (
  142.         SELECT CUST, NO_REFERENSI, COUNT(*) AS DUP
  143.         FROM [' + @dbname + ']..SOT01A b(nolock)
  144.         GROUP BY CUST, NO_REFERENSI
  145.     ) c ON a.ORDERID=c.NO_REFERENSI and a.CUST=c.CUST
  146.     WHERE a.tgl > ''2024-08-01'' and a.ORDER_STATUS=''UNPAID'' AND c.DUP > 1
  147.  
  148.     UNION ALL'
  149.     FETCH NEXT FROM db_cursor INTO @dbname
  150. END
  151.  
  152. -- Remove the last 'UNION ALL'
  153. SET @sql = LEFT(@sql, LEN(@sql) - 10)
  154.  
  155. -- Execute the dynamic SQL
  156. EXEC sp_executesql @sql
  157.  
  158. -- Cleanup
  159. CLOSE db_cursor
  160. DEALLOCATE db_cursor
  161.  
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement