how to convert this MsSQL query into postgreSQL query - postgresql

createDB #"if not exists(select * from sys.databases where name = '{0}') create DATABASE [{0}]" --------
createLogin #"if not Exists (SELECT name FROM sys.server_principals
WHERE name = '{0}') BEGIN CREATE LOGIN [{0}] FROM WINDOWS END"
How to convert this to Postgres?

Related

When does this query gets generated?

When does this query get generated by the system?
DELETE FROM LBACSYS.LBAC$POLT WHERE TBL_NAME = UPPER(:B2 ) AND OWNER = UPPER(:B1 )
I googled it and found out that it is related to label security and nothing more.
Deletes the entry from the table if both the table name and owner name match.
It deletes all rows from LBAC$POLT table owned by LBACSYS whose
column tbl_name is equal to value passed through the b2 parameter (or a bind variable), converted to upper case
column owner is eqal to value passed through b1, converted to upper case
For example:
SQL> select * from lbac$polt;
OWNER TABLE_NAME
----- ----------
SCOTT EMP
SCOTT DEPT
(this is a SQL*Plus client which uses substitution variables, e.g. &b2; your code uses bind variables; at the end, the result is just the same):
SQL> delete from lbac$polt where table_name = upper('&b2') and owner = upper('&b1');
Enter value for b2: emp
Enter value for b1: scott
old 1: delete from lbac$polt where table_name = upper('&b2') and owner = upper('&b1')
new 1: delete from lbac$polt where table_name = upper('emp') and owner = upper('scott')
1 row deleted.
Result:
SQL> select * from lbac$polt;
OWNER TABLE_NAME
----- ----------
SCOTT DEPT
SQL>

Postgres Alternative to \du to view group role membership

Is there an alternative to using psql \du to retrieve the Member of column using a builtin Postgres function or view? Thank you.
In psql you can echo the actual queries generated by backslash commands.
Use the --echo-hidden parameter
psql --echo-hidden ...
or set the variable ECHO_HIDDEN in psql, example:
test=# \set ECHO_HIDDEN on
test=# \du
********* QUERY **********
SELECT r.rolname, r.rolsuper, r.rolinherit,
r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
r.rolconnlimit, r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
WHERE m.member = r.oid) as memberof
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
ORDER BY 1;
******************************
Read about psql in the documentation.
I figured it out and here's my answer:
SELECT
pg_roles.rolname,
pg_auth_members.member,
(SELECT pg_roles.rolname FROM pg_roles WHERE oid = pg_auth_members.member)
FROM
pg_roles, pg_auth_members
WHERE
pg_auth_members.roleid = ( SELECT oid FROM pg_roles WHERE rolname = 'my_group') and pg_roles.rolname = 'my_group';
So, this query would return something like this:
rolname | member | rolname
------------+--------+---------
my_group | 18068 | first_member
my_group | 18069 | second_member
(2 rows)

How to make sql query on table name with space separated in Postgres?

I'm using postgres database and it has table with name say 'System Tenant'. Now I want to make query on it, I do -
select * from "System Tenant";
but it results into error -
ERROR: relation "System Tenant" does not exist
LINE 1: select * from "System Tenant"
^
Could you please suggest how I can resolve it?
lets say:
so=# create schema t;
CREATE SCHEMA
so=# create table t."Bad Name"();
CREATE TABLE
so=# create table "b#d Name"();
CREATE TABLE
now find all:
so=# select oid::regclass from pg_class where relname ilike '% name%';
oid
--------------
t."Bad Name"
"b#d Name"
(2 rows)
and use exactly as it is listed:
so=# select * from t."Bad Name";
--
(0 rows)
or
so=# select * from "b#d Name";
--
(0 rows)

extract data from two databases postgres

I have two database , test1 and test2
in test1 I have a table named : emplyee
in test2 I have a table named : user_
I want to extrat the users from table employee who are not exist in the table user_
I try with this query
select * from employee where mail_employe not in
( select emailaddress from test2.user_ )
I have this error :
ERROR: schema "test2" does not exist
LINE 3: ( select emailaddress from test2.user_ )
I try also with this syntax :
select * from employee where mail_employe not in
(SELECT emailaddress from dblink('dbname=test2','SELECT emailaddress FROM user_'))
I have also this error
ERROR: a column definition list is required for functions returning "record"
LINE 3: (SELECT emailaddress from dblink('dbname=test2','SELEC...
updated
I try to user the systax of SCHEMA
in the two database I have this code of SCHEMA
CREATE SCHEMA public
AUTHORIZATION postgres;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
COMMENT ON SCHEMA public
IS 'standard public schema';
I try to use this code without success
select * from test1.public.employee where mail_employe not in
(SELECT emailaddress from test2.public.user_')
but i have the same error
Example:
CREATE SCHEMA sch1;
CREATE SCHEMA sch2;
CREATE TABLE sch1.foo ( id serial, name varchar );
CREATE TABLE sch2.bar ( id serial, title varchar );
Query:
SELECT sch1.foo.name, sch2.bar.title
FROM sch1.foo
INNER JOIN sch2.bar on sch1.foo.name = sch2.bar.title;
SELECT sch1.foo.name
FROM sch1.foo
WHERE sch1.foo.id IN (SELECT sch2.bar.id FROM sch2.bar);
Unfortunately, as long as you don't change search path you need to write full path to resources.

How should I modify a SQL query for PostgreSQL?

I have SQL query, which is working nice on Oracle and MSSQL. Now I'm trying this on PostgreSQL and it gives a strange exception: org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table "main"
Here is the query:
SELECT *
FROM "main" main
INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
INNER JOIN "something" somehting ON something_link."somethingid" = something."id"
INNER JOIN "type" type ON something."typeid" = type."id"
This is quite simple query and I can't see why it is not working on Windows XP SP2, PostgreSQL 8.3?
somehting=>something
postgres=# create database test
postgres-# ;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test".
test=# select version();
version
-----------------------------------------------------------------------------------------------
PostgreSQL 8.3.3 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
test=# create table main(id int);
CREATE TABLE
test=# create table something_link(mainid int);
CREATE TABLE
test=# create table something(id int);
CREATE TABLE
test=# create table type(id int);
CREATE TABLE
test=# alter table something add column typeid int;
ALTER TABLE
test=# SELECT *
test-# FROM "main" main
test-# INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
test-# INNER JOIN "something" somehting ON something_link."somethingid" = something."id"
test-# INNER JOIN "type" type ON something."typeid" = type."id"
test-# ;
ERROR: column something_link.somethingid does not exist
LINE 4: INNER JOIN "something" somehting ON something_link."som...
^
test=# alter table something_link add column somethingid int;
ALTER TABLE
test=# SELECT *
FROM "main" main
INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
INNER JOIN "something" *somehting* ON something_link."somethingid" = something."id"
INNER JOIN "type" type ON something."typeid" = type."id"
;
ERROR: invalid reference to FROM-clause entry for table "something"
LINE 4: ...hing" somehting ON something_link."somethingid" = something....
^
HINT: Perhaps you meant to reference the table alias "somehting".
test=# SELECT *
FROM "main" main
INNER JOIN "something_link" something_link ON main."id" = something_link."mainid"
INNER JOIN "something" something ON something_link."somethingid" = something."id"
INNER JOIN "type" type ON something."typeid" = type."id"
;
id | mainid | somethingid | id | typeid | id
----+--------+-------------+----+--------+----
(0 rows)
The real problem is actually not the query, but the PostgreSQL 8.3 default configuration.
After correcting the spelling mistake (10x Kendrick Wilson), the problem persisted, until I edited the "postgresql.conf" file. There should be a line:
add_missing_from = on
This line ensures compatibility with the other SQL dialects.
According to this, seems like you either mistyped an alias or used a table name in place of it.