Frederik Grut
Frederik Grut

Reputation: 21

Declaring variables in HeidiSQL

Regarding HeidiSQL I would like to set a variable to the maxdate of a table in order to use this variable as a 'WHERE'-criteria in another query.

I am using the following code

DECLARE @maxdate timestampz
SET @maxdate = (SELECT MAX("Date") FROM transactions)
SELECT maxdate

However, I get a syntax error at or near "@" in line 1. Any help is much appreciated - thank you!

Upvotes: 1

Views: 633

Answers (1)

Enkode
Enkode

Reputation: 4783

DO 
'
DECLARE 
    maxdate timestamptz;
BEGIN
    SELECT MAX("Date") INTO maxdate FROM transactions;
    
    -- To see the result, we''ll use a temp table
    CREATE TEMP TABLE IF NOT EXISTS temp_results (maxdate timestamptz);
    INSERT INTO temp_results VALUES (maxdate);
END
' LANGUAGE plpgsql;

-- View the result SELECT * FROM temp_results; Note the key differences from T-SQL:

No @ symbol for variables in PostgreSQL Use := or INTO instead of SET for assignment Need double quotes for case-sensitive column names like "Date" Double single quotes ('') for string literals inside the DO block

With HeidiSQL you cannot use the variable directly in TSQL

Upvotes: 0

Related Questions