For a bit of context: I've written a stored procedure that uses two tables, products
(product_id, product_name, description, price, category_id) and supplies
(shipment_id, suppliers_id, product_id (FK to products
), supply_date, quantity).
The procedure takes a product_id
and prints the description and certain info about the product's supply within the give time range (from date1 to date2).
This is the T-SQL code:
CREATE OR ALTER PROCEDURE productInfo
(@product_id INT,
@date1 DATE,
@date2 DATE)
AS
BEGIN
DECLARE @description VARCHAR(100),
@shipment_id INT,
@supply_date DATE,
@quantity INT;
DECLARE product_cursor CURSOR LOCAL FOR
SELECT description
FROM products
WHERE product_id = @product_id
OPEN product_cursor;
FETCH NEXT FROM product_cursor INTO @description
CLOSE product_cursor
DECLARE product_supply_cursor CURSOR LOCAL FOR
SELECT shipment_id, supply_date, quantity
FROM supplies
WHERE product_id = @product_id
OPEN product_supply_cursor;
FETCH NEXT FROM product_supply_cursor INTO @shipment_id, @supply_date, @quantity;
WHILE @@FETCH_STATUS = 0
BEGIN
IF @supply_date >= @date1 AND @supply_date <= @date2
BEGIN
PRINT 'DESCRIPTION: ' + @description
PRINT 'SHIPMENT_ID: ' + STR(@shipment_id)
PRINT 'SUPPLY_DATE: ' + convert(varchar, @supply_date, 23)
PRINT 'QUANTITY: ' + STR(@quantity)
PRINT ' '
END;
FETCH NEXT FROM product_supply_cursor INTO @shipment_id, @supply_date, @quantity;
END;
CLOSE product_supply_cursor;
END;
Obviously this procedure doesn't run in pgSQL and I have no experience on the matter so I'd like either a translation of this code or (if I'm asking too much) a hint about what things would have to change ( syntax or logic wise) to fit pgSQL language.