CREATE OR REPLACE VIEW sql error - sql

Trying to update a table view using:
CREATE OR REPLACE VIEW [vtable] AS SELECT * FROM Files_Table ORDER BY File
The table is returning the old view, not the updated.
Statement tested in the Sqlite database browser:
Error message from database engine: near "OR": syntax error
but didn't get this in the program?
Any idea why it's not updating?

SQLite does not support the CREATE OR REPLACE syntax. The only database that I know which supports that syntax is Oracle, but I am guessing there are others.
Drop the view and create it with the new definition:
DROP VIEW IF EXISTS [vtable]; -- "OR REPLACE"
CREATE VIEW [vtable] AS SELECT * FROM Files_Table ORDER BY File;

I ran into the same error when using CREATE OR REPLACE in MS SQL Server. The following worked for me:
ALTER VIEW [vtable] AS
SELECT *
FROM Files_Table
ORDER BY File;

Related

How do I make CREATE DATABASE command re-runnable in 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?

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.

MS SQL, Cant use just the Table name anymore

Im doing the following in a SQL Query from SQL Server Management:
SELECT * FROM tablename
Which looks like it works for some other tables when executed in a Procedure. But for this specific table I have to do:
SELECT * FROM databasename.dbo.tablename
In both a single query and in a procedure or i get the following Error: Message 208 Invalid object name.
I've tried doing this as well:
SELECT * FROM dbo.tablename
Table schema is set to dbo and my users default schema is also dbo. Also this is a newly installed server and the database is a restored database from another server.
Im new to SQL so its probably something stupid but I cant find the answer anywhere.
EDIT
I fixed it. I was stupid and didn't know about reserved keywords. The table name was User and hence I needed to use Brackets []. [tablename]
I think the solution that may help you is:
Use databasename;
GO
select * from tablename

Create view and use it in a single SQL script

Simple thing ... i thought. Create a view and use it later in the same SQL script.
Let's say we have a script as follows:
CREATE VIEW someView AS (...)
DROP VIEW someView
If I try to parse it SQL Management complaints there's an error around DROP.
If I execute them separately (create first, then drop) they work both fine.
Is there any way to create a view and use it in a single SQL script?
I could wrap further statements in string an then EXEC it but it's a bit inconvenient.
Code example was fixed (missing VIEW)
More meaningful example:
create view TEST as (select name from spt_values where number=1);
drop view TEST
Is it possible to execute it at once?
I got the error:
Msg 156, Level 15, State 1, Procedure TEST, Line 2
Incorrect syntax near the keyword 'drop'.
Running create statement separately and then dropping view works perfectly.
Separate your query with GO keyword like query bellow:
CREATE VIEW someView AS ()
GO
DROP VIEW someView
GO
Regardless of which particular DBMS you are using, you should create a script separating your SQL statements with ';'.
For example
CREATE VIEW someView as (...);
<<some other sql statements>>
DROP VIEW someView;

PhpPgAdmin Syntax error when creating View

I am attempting to create a View in PhpPgAdmin (PostGreSQL db) which has the following SQL statement:
DELETE FROM myTable WHERE myTable.error IS NULL;
PhpPgAdmin gives me the following error:
ERROR: syntax error at or near "DELETE" at character 59
In statement:
CREATE OR REPLACE VIEW "Schema1"."Delete empty errors" AS DELETE FROM myTable WHERE myTable.error IS NULL;
As far as I can tell this SQL statement is valid, and I have delete privileges for the table. Is the DELETE statement not allowed in Views? Any ideas what I am doing wrong?
Views are used to display the data from SELECT statements only (usually when the SELECT is complex). Views cannot contain DELETES, UPDATES, or INSERTS.
Perhaps you want a function?
EDIT: As OMG Ponies points out, you can have updateable views, but thats where you would issue a DELETE to an existing view and then use a RULE to rewrite the query as a DELETE.
And please, please don't wrap a function call to do a DELETE as a side effect in a view. Its unexpected and Jesus shoots a puppy every time this happens.