How do I make CREATE DATABASE command re-runnable in SQL? - sql

I'd like to create a script that simply drops and creates a database over and over in PostgreSQL.
For a table this is not problem with the following:
DROP TABLE IF EXISTS test CASCADE;
CREATE TABLE IF NOT EXISTS test ( Sample varchar );
The above code works, no problem.
However, when I try to do the same for a database, ie:
DROP DATABASE IF EXISTS sample;
CREATE DATABASE sample;
I get the following error:
ERROR: DROP DATABASE cannot run inside a transaction block
SQL state: 25001
Any idea how I can get the database to be created and dropped repetitively without doing it manually?

Related

Rename a table in Amazon Redshift

I've been trying to rename a table from "fund performance" to fund_performance in SQLWorkbench for a Redshift database. Commands I have tried are:
alter table schemaname."fund performance"
rename to fund_performance;
I received a message that the command executed successfully, and yet the table name did not change.
I then tried copying the table to rename it that way. I used
#CREATE TABLE fund_performance LIKE "schema_name.fund performance";
CREATE TABLE fund_performance AS SELECT * FROM schema_name."fund performance";
In both these cases I also received a message that the statements executed successfully, but nothing changed. Does anyone have any ideas?
Use following it may work out for you
SELECT * into schema_name.fund_performance FROM schema_name.[fund performance]
It will copy the data by creating new table as fund_performance but it won't create any constraints and Identity's
To Rename specific table without disturbing existing constraints
EXEC sp_rename 'schema_name.[fund performance]', 'schema_name.fund_performance';

Insert If Table Exists in Databricks or Spark SQL

I am writing in a Databricks notebook where I need to say something like:
%sql
IF EXISTS myTable INSERT OVERWRITE TABLE myTable SELECT * FROM somethingElse
I know that the INSERT OVERWRITE clause works, but I'm not sure how to get the IF EXISTS to work without breaking out of pure SQL code and using python (which would make the script messier).
Unfortunately, there is no DDL named "IF EXISTS" supported in Databricks.
You have to use command called "Drop Table":
Drop a table and delete the directory associated with the table from the file system if this is not an EXTERNAL table. If the table to drop does not exist, an exception is thrown.
IF EXISTS
If the table does not exist, nothing happens.
DROP TABLE [IF EXISTS] [db_name.]table_name
Example:
DROP TABLE IF EXISTS diamonds;
CREATE TABLE diamonds USING CSV OPTIONS (path "/databricks-datasets/Rdatasets/data-001/csv/ggplot2/diamonds.csv", header "true")
Reference: SQL Language Manual This is a complete list of Data Definition Language (DDL) and Data Manipulation Language (DML) constructs supported in Databricks.
Hope this helps.

How to create table like avro?

I created a table (test_load) based on the schema of another one (test). Then i inserted test_load into another table.
drop table if exists test_load;
create external table test_load
like test
location /test_load_folder;
insert into warehouse
select * from test_load;
It works fine when everything is in parquet.
I then evolved my test schema to avro and recreated my test_load table but when i try to insert into warehouse i receive an error :
Error while processing statement: FAILED: Execution Error, return code 2
from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
I'm looking for the good syntax to re-create the load table and scpecify its avro. My hypothesis is that hive still considers its parquet files.
I tried
drop table if exists test_load;
create external table test_load
like test
location /test_load_folder
stored as avro;
but i have a syntax error.

Deleting objects from SQL Server

In SQL Server Database Engine I have a table named Table A.
I deleted the table using graphical interface, but when I wanted to create a table with same name, the error shows
The object already exists
What is the remedy of this situation?
The following steps should help you track down what is going on and help you create your table:
Right-click on your database and select refresh
Verify that your table does not exist under this database.
If you table is
not shown here, then very likely your table is displayed under the
master database.
To create a table in your selected database,
first select the database and then run your query.
A better
option for number 4, just to be sure you are specifying the correct
database is to run the command use dbname; (where dbname is
the name of your database). Do this on the line above your create table code.

Oracle name already in use

I have a script that I am running for homework that looks like so:
drop table burger_king_locations_clean;
drop table burger_king_locations_unresolv;
create table burger_king_locations_clean
(
...
);
create table burger_king_locations_unresolv
(
....
);
But when I run this I get the following message and I don't understand because I know the table is gone and there is no view or any other object with that name.
Table dropped.
drop table burger_king_locations_unresolv
*
ERROR at line 1:
ORA-00942: table or view does not exist
Table created.
Table created.
create table burger_king_locations_unresolv
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
I don't understand because this exact thing was working before but I put my computer in sleep mode and then came back to this and now it doesn't work at all. Any ideas?
"I have a script that I am running for homework that looks like so"
So you say. But the posted output shows the execution of two DROP statements, one of which fails, and two CREATE statements, one of which is run twice.
Why did the drop table burger_king_locations_unresolv; fail? Who can tell? Perhaps you had already dropped it, perhaps the previous create had failed.
Likewise it is not possible for us to tell you why the create table burger_king_locations_unresolv ran twice. Perhaps your script has a stray back slash following the semi-colon.
Wouldn't you need to purge the table?
drop table burger_king_locations_clean PURGE;
drop table burger_king_locations_unresolv PURGE;