Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qOLGP-007XgO-Qy for pgsql-sql@arkaria.postgresql.org; Tue, 25 Jul 2023 16:51:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qOLGO-00HLI4-6E for pgsql-sql@arkaria.postgresql.org; Tue, 25 Jul 2023 16:51:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qOLGN-00HLHw-Tp for pgsql-sql@lists.postgresql.org; Tue, 25 Jul 2023 16:51:55 +0000 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qOLGL-000IVz-Hx for pgsql-sql@postgresql.org; Tue, 25 Jul 2023 16:51:55 +0000 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2b74310566cso85111731fa.2 for ; Tue, 25 Jul 2023 09:51:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690303912; x=1690908712; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=VMHsdmRZNpI0GznfRqOJKleL0rNcGpH4MGBZdtz8QHs=; b=qkGD1v0Fu3yxfVABYT+X7rlwVRx7jjpx0d7C2hxj+pfZae+Sl5HCe56Lk5s2UWOlwy UW2gqDtP4iZkf732rj2z6TTKG8E0YhRElwFrSLNhHRM7gxh3pKGREjcsaqkHBOntkF/K tEuCt3Zan0/ro1Vq5/u26cEVKyui9Ny3EgvCxjnGFo8hPK6KlQG/o06jZlIqTqCIZNYu HF+7nqr+16+ObsXlTHN+8Dr+1SThsDjt4SYnbM+6TQYCz8DhsUyp3VqwqoPo7cm6UF0U s83UcJJnY+yYPyypdA+mMa5M6gdIcBZtlT8zvKSg1XKDeGFw7tbK3N4ZdgSKybPlKdnX 53RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690303912; x=1690908712; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VMHsdmRZNpI0GznfRqOJKleL0rNcGpH4MGBZdtz8QHs=; b=O6bIY+vT0K14LPicgKLr/s5uoqySF7d3dc3J1S+HhJdmQ/kIsijuX+xil6M0MYXIYr 8cKala7gz1qvxmaiJrhs2rZYls0rX/BQ8dCpODPIa76qziKVm5sipc4iG6PukXZNVlQZ sv8GGdCUYiOXo729+N7ZoX83M4rtarrc1oufjRcJI+tH7vhOqyAAsJognXJTkiBfmkZH mDTyZgxpJHQJbK5Oe26NUowZJlAKCXS20BI/73YykDGNzP0Otv9srfMwKBFQH8xObrGA V9fPyd6M+SgD/GAAVIIWGzCwV+gj4FFF0pO2ytWSm/iaFwABqqgEMzkEclkDAo5CMK9p KW1A== X-Gm-Message-State: ABy/qLb7N79t2xBnwlJMzCsKP5baXIeiJAFApLu3bnXPRWIZSAiMCvYV Dnuo299biQ90N9jePS9as9CoMLhFB6hybq2UzkY= X-Google-Smtp-Source: APBJJlH8CjbEh2yvqRLtVX5pb0Q9dPEJJ7NmFi+S79ylczXkOUij2Jth/fXg2Tt7cPZ0jn826ojmt9ZMPzu+HyY7EY0= X-Received: by 2002:a2e:b0e2:0:b0:2b6:e13f:cfd7 with SMTP id h2-20020a2eb0e2000000b002b6e13fcfd7mr9778454ljl.4.1690303911864; Tue, 25 Jul 2023 09:51:51 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Greg Sabino Mullane Date: Tue, 25 Jul 2023 12:51:14 -0400 Message-ID: Subject: Re: Declaring a constant or variable in a query To: JORGE MALDONADO Cc: pgsql-sql@postgresql.org Content-Type: multipart/alternative; boundary="0000000000003aaeca0601528c7f" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003aaeca0601528c7f Content-Type: text/plain; charset="UTF-8" Not sure how exactly DataGrip works for prompting input, but a CTE should allow you to declare the constant once: WITH x AS (SELECT :myinput AS alias5) SELECT fld1 as alias1, fld2 as alias2, fld3 as alias3, fld4 as alias4, alias5 FROM table1, x WHERE condition1 UNION SELECT fld1 as alias1, fld2 as alias2, fld3 as alias3, fld4 as alias4, alias5 FROM table2, x WHERE condition2 UNION SELECT fld1 as alias1, fld2 as alias2, fld3 as alias3, fld4 as alias4, alias5 FROM table3, x WHERE condition3 UNION SELECT fld1 as alias1, fld2 as alias2, fld3 as alias3, fld4 as alias4, alias5 FROM table4, x WHERE condition4 Cheers, Greg --0000000000003aaeca0601528c7f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Not sure how exactly DataGrip works for prompting input, b= ut a CTE should allow you to declare the constant once:

= WITH x AS (SELECT :myinput AS alias5)
=
SELECT fld1 as alias1, fld2 as alias2, fld3= =C2=A0as alias3, fld4=C2=A0as alias4, alias5=C2=A0FROM table1, x WHERE cond= ition1
UNION
SELECT fld1 as alias1, fld2 as alias2, fld3=C2=A0as al= ias3, fld4=C2=A0as alias4,=C2=A0alias5=C2=A0FROM table2, x WHERE condition2=
UNION
SELECT fld1 as alias1, fld2 as alias2, = fld3=C2=A0as alias3, fld4=C2=A0as alias4,=C2=A0alias5=C2=A0FROM table3, x W= HERE condition3
UNION
SELECT fld1 as alias1, f= ld2 as alias2, fld3=C2=A0as alias3, fld4=C2=A0as alias4,=C2=A0alias5=C2=A0F= ROM table4, x WHERE condition4

Cheers,
Greg

--0000000000003aaeca0601528c7f--