Kimossab

SIBD - S16.2

Jun 1st, 2015
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.82 KB | None | 0 0
  1. IF OBJECT_ID('fn_GetDiv') IS NOT NULL
  2.         DROP FUNCTION fn_GetDiv
  3. GO
  4. CREATE FUNCTION fn_GetDiv(@idforn INT)
  5. RETURNS MONEY
  6. BEGIN
  7.         DECLARE @ret MONEY;
  8.                 SET @ret = (SELECT SUM(TotalFactura-Pagamento-Crédito) FROM CópiaFacturas WHERE Fornecedor = @idforn)
  9.         RETURN @ret
  10. END
  11. GO
  12. DECLARE Cursor_2Ponto3 CURSOR
  13. FORWARD_ONLY DYNAMIC READ_ONLY
  14. FOR
  15.     SELECT IDFornecedor, Nome, Localidade
  16.     FROM Fornecedores
  17.     WHERE dbo.fn_GetDiv(IDFornecedor) > 0
  18.  
  19. DECLARE @id INT, @n VARCHAR(150), @loc VARCHAR(150)
  20. OPEN Cursor_2Ponto3
  21. FETCH NEXT FROM Cursor_2Ponto3 INTO @id, @n, @loc
  22. WHILE @@FETCH_STATUS = 0
  23. BEGIN
  24.     PRINT @n+'(' +@loc+')'
  25.     PRINT 'Divida: ' + CONVERT(VARCHAR,dbo.fn_GetDiv(@id))
  26.     PRINT ' '
  27.     FETCH NEXT FROM Cursor_2Ponto3 INTO @id, @n, @loc
  28. END
  29. CLOSE Cursor_2Ponto3
  30. DEALLOCATE Cursor_2Ponto3
Advertisement
Add Comment
Please, Sign In to add comment