Error in create database postgresql - database

Is any way to check a database is already exists or not in PostgreSQL?
Iam a new one in PostgreSQL.
And also need to check it from my java application via jdbc driver.

There is no IF NOT EXISTS option for CREATE DATABASE in PostgreSQL. See CREATE DATABASE.
You can check pg_database to see if the DB exists, and only attempt to create it if it does not.
You would need to do this via dblink, because of the limitation Frank points out in the comments:
regress=> SELECT create_database_if_not_exists('test');
ERROR: CREATE DATABASE cannot be executed from a function or multi-command string
CONTEXT: SQL statement "CREATE DATABASE test"
PL/pgSQL function create_database_if_not_exists(text) line 6 at EXECUTE statement

I found an alternate solution for this problem. By using the following query :
ResultSet res = st.executeQuery("select count(*) from pg_catalog.pg_database where datname = 'sample'") ;
res.next();
int count = res.getInt("count");
System.out.println("Count : " + count);
if(count == 0) {
st.executeUpdate("CREATE DATABASE sample");
}
Its work fine

Provided you do not have many databases made in postgreSQL
Here is a simple way to see what databases you have and do not have
Can use pgadmin ,and expand database column and see what databases you have and dont have.
As shown by the yellow box, the databases created in pgadmin, by me, can try this

Related

Invalid object name / column after re-opening

I'm trying to get into SQL using Microsoft SQL Server Management Studio. I've added a database and I want to make a query:
This works and I can execute the code, but after I saved my query and closed the program it doesn't work anymore when I open the program and try to execute the query again. It can't find the terms I'm relating to:
I don't know why this occurs or how I can solve it, it seems that the connection between the query and the database is gone... Can someone help me on this?
You're attempting to execute the query against the master database but that's not where your tables are. Three ways to handle this:
Use the drop-down in the toolbar to switch to the dbArtemis database
Fully-qualify your table names. dbArtemis.dbo.Klantnummer for example
Execute use dbArtemis; in your query window before the query itself.
Just add before your query the name of your database:
USE dbArtemi
GO
SELECT Naam
FROM tblklaten
WHERE klatenummer =
(SELECT DISTINCT klatnummer FROM tblorders where (orderID = 11013));
GO

How can I query over all db of my server without looping over DB in pymssql connection

I'd like first to know how to make a query over all the databases in my server instance with pymssql (in MSSQL management studio = right click --> new query on the server thumbnail then don't need to specify the name of the db in the query - it just gives you one more column in the output which is the segment from which the record is from). Then how do you do the same as registered servers on two or multiple hosts (I have 2 hosts and I want to pass the same query do I really need to make the two connections ?)
thanks
You could use sp_foreachdb, like this:
EXECUTE master.sys.sp_MSforeachdb 'USE [?]; EXEC update table set foo = bar'
Maybe this can help you (but - to be honest - I did not really understand what you want :-) )
SELECT * FROM sys.databases

Why can't SQL Server database name begin with a number if I run a query?

Recently I found an anomaly with SQL Server database creation. If I create with the sql query
create database 6033SomeDatabase;
It throws an error.
But with the Management Studio UI, I can manually create a database with a name of 6033SomeDatabase.
Is this expected behaviour or is it a bug? Please throw some light on this issue.
Try like this,
IF DB_ID('6033SomeDatabase') IS NULL
CREATE DATABASE [6033SomeDatabase]
I'll try to give you detailed answer.
SQL syntax imposes some restrictions to names of database, tables, and fields. F.e.:
SELECT * FROM SELECT, FROM WHERE SELECT.Id = FROM.SelectId
SQL parser wouldn't parse this query. You should rewrite it:
SELECT * FROM [SELECT], [FROM] WHERE [SELECT].Id = [FROM].SelectId
Another example:
SELECT * FROM T1 WHERE Code = 123e10
Is 123e10 the name of column in T1, or is it a numeric constant for 123×1010? Parser doesn't know.
Therefore, there are rules for naming. If you need some strange database or table name, you can use brackets to enclose it.

Heterogeneous Oracle to SQL Server: Using "IS NULL" in Where clause for Update statement

I am having trouble with using the IS NULL condition in an Update statement executed on an SQL Server 2000 database via an Oracle Database (11.2.0.4) using an ODBC heterogeneous connection.
Example of update statement that will not work.
UPDATE TABLENAME#RemoteSQLServer2000
SET "ColumnName" = 'SomeValue'
WHERE "AnotherColumnName" IS NULL;
Result:
ORA-02070: database RemoteSQLServer2000 does not support IS NULL in this context
However, the following statement works fine:
SELECT *
FROM TABLENAME#RemoteSQLServer2000
WHERE "AnotherColumnName" IS NULL;
Does anyone have an idea about what I can do to make this work? Thanks in advance. Let me know if you need more info.
Thanks to #MickMnemonic and another consultant, we were able to find a solution.
Oracle has a package that allows you to execute SQL from an application to an SQL Server database. This function will allow you to successfully execute a statement like the one mentioned in the question where you are filtering on NULL values. Oracle Documentation for DBMS_HS_PASSTHROUGH
Following is an example of the new code that works.
DECLARE
num_rows INTEGER;
BEGIN
num_rows :=
DBMS_HS_PASSTHROUGH.EXECUTE_IMMEDIATE#RemoteSQLServer2000 (
'UPDATE TABLENAME SET "ColumnName" = ''SomeValue'' WHERE AnotherColumnName IS NULL');
END;
/
try this in your where clause instead of where AnotherColumnName is null
Where AnotherColumnName || 'x' = 'x'

In SQL Server (2012), when can I just use a table name "Foo" w/o the full qualification dbname.dbo.Foo?

Is there a setting in the management studio that would allow me, when I create a new query, to use just:
SELECT * FROM Foo
instead of
SELECT * FROM dbname.dbo.Foo
assuming of course there is no ambiguity?
Currently I get an error message. Thanks.
You can set a default schema for a User in SQL Server 2012.
Here is the MSDN page
Note: You can't change the schema after setting it once.
SELECT YOUR DATABASE NAME HERE FROM THE DROP DOWN LIST and then if there is any ambiguity in column names just use two part name i.e TABLENAME.ColumnName
WHere you open a new query window it opens it in Master database context, And people who has been working with sql server for years and years makes this mistake quite often of openning a query window and start executing a script in master db. so your not the only one :)
You can also use the USE statement i.e
USE DataBase_Name
GO
//your query.........

Resources