0

i try a query that runs on mssql however does not run postgreSQL...

SQL Query is..

IF EXISTS (SELECT * FROM Kategoriler WHERE KategoriId = 119)
    BEGIN 
   SELECT * FROM Kategoriler
    END
    ELSE
   SELECT * FROM Adminler

i searched it and i found in stackoverflow

DO
   $BODY$
   BEGIN
        IF EXISTS (SELECT 1 FROM orders) THEN
              DELETE from orders;
        ELSE 
              INSERT INTO orders VALUES (1,2,3);
        END IF;
   END;
   $BODY$

but i do not want to use DO or, $body etc... I do not want to write any function or other etc...

i want to write only if else statement in postgreSQL... Please help me...

4
  • Your first block of code isnt a SQL query. It is a SQL procedure, like the second block. May 27, 2013 at 18:22
  • what you are asking for is not possible in PostgreSQL. However, if you can tell us what your goal is, what you are trying to ultimately accomplish, we can probably provide an alternative.
    – Flimzy
    May 27, 2013 at 20:56
  • In future please always mention your exact PostgreSQL version in questions; see select version(). May 28, 2013 at 0:42
  • In addition to the answers below, you might read about Postgresql's inheritance capabilities to see if they would be useful. May 28, 2013 at 17:45

3 Answers 3

3

T-SQL supports some procedural statement like IF. PostgreSQL doesn't support it, so you cannot rewrite your query to postgres simply. Sometime you can use Igor's solution, sometime you can use plpgsql (functions) and sometime you have to modify your application and move procedural code from server to client.

1

Try something like

SELECT * 
FROM Kategoriler
UNION ALL
SELECT * 
FROM Adminler
WHERE NOT EXIST (SELECT * FROM Kategoriler WHERE KategoriId = 119)

Will only work if Kategoriler and Adminler have same structure. Otherwise you need to specify list of fields instead of *

0

In my case I needed to know if a record existed.

I had to write a function

CREATE OR REPLACE FUNCTION public.pro_device_exists(vdn character varying)
 RETURNS boolean
 LANGUAGE plpgsql
AS $function$
BEGIN

    IF EXISTS (SELECT 1 FROM tags WHERE device_name = upper(vdn)) THEN
          return true;
    ELSE 
          return false;
    END IF;
END; $function$

Then I was able to call this function in my code ... just a portion of my code

            if pro_device_exists(vdn) then
                update tags
                set device_id = 11 where device_id = pro_device_id(vdn) and tag_type=10;            
                update tags
                set device_id = pro_device_id(vdn) where tag_id = vtag_id;
                vmsg = (select 'Device Now set to ' || first_name || ' ' || last_name from tags where tag_id=vtag_id);
                vaction = 'Refresh Device Data';
            else
                vmsg = 'Device is not registered on this system';
                vaction = 'No Nothing';
            end if;
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.