External table 'logdata' is not accessible because location does not exist or it is used by another proces - tsql

I am try to create an external table on Azure Synapse by once I run select * from logdata I get the error "External table 'logdata' is not accessible because location does not exist or it is used by another proces"
below is my code
CREATE DATABASE appdb;
CREATE MASTER KEY ENCRYPTION BY PASSWORD =<>
CREATE DATABASE SCOPED CREDENTIAL SasToken
WITH IDENTITY='SHARED ACCESS SIGNATURE',
SECRET=<>
CREATE EXTERNAL DATA SOURCE log_data
WITH (
LOCATION='https://<>.dfs.core.windows.net/data',
CREDENTIAL=SasToken
)
CREATE EXTERNAL FILE FORMAT TextFileFormat WITH(
FORMAT_TYPE=DELIMITEDTEXT,
FORMAT_OPTIONS(
FIELD_TERMINATOR=',',
FIRST_ROW=2
)
)
CREATE EXTERNAL TABLE logdata(
[Id] INT,
[Correlationid] VARCHAR (200),
[Operationname] VARCHAR (200),
[Status] VARCHAR (200),
[Eventcategory] VARCHAR (200),
[Level] VARCHAR (200),
[Time] DATETIME,
[Subscription] VARCHAR (200),
[Eventinitiatedby] VARCHAR (1000),
[Resourcetype] VARCHAR (1000),
[Resourcegroup] VARCHAR (1000)
)
WITH(
LOCATION='/Log.csv',
DATA_SOURCE=log_data,
FILE_FORMAT=TextFileFormat
)
-- drop EXTERNAL table logdata ;
SELECT * from logdata;
```
I tried changing the access levels but couldn't work either.

You need the necessary access rights to the file in order to fix this problem. The simplest method is to give yourself the "Storage Blob Data Contributor" role on the storage account that you are trying to query.
And also check the details you entered while creating the external table in synapse. The location's URL is case-sensitive. and make sure your file is present at the destination.
Correct Syntax/code to create External Table:
CREATE MASTER KEY ENCRYPTION BY PASSWORD ='Welcome#Pratik123';
CREATE DATABASE SCOPED CREDENTIAL MyCred
WITH IDENTITY='SHARED ACCESS SIGNATURE',
SECRET='SAS Token';
CREATE EXTERNAL DATA SOURCE MyDs2
WITH (
LOCATION='abfss://containername#storageaccountname.dfs.core.windows.net/foldername if any',
CREDENTIAL=MyCred
)
CREATE EXTERNAL FILE FORMAT MyFile2 WITH(
FORMAT_TYPE=DELIMITEDTEXT,
FORMAT_OPTIONS(
FIELD_TERMINATOR=',',
FIRST_ROW=2
)
)
CREATE EXTERNAL TABLE MyData3(
[Id] varchar(20),
[NAME] VARCHAR (200),
[ADDRESS] VARCHAR (200)
)
WITH(
LOCATION='/dataaddress.csv',
DATA_SOURCE=MyDs2,
FILE_FORMAT=MyFile2
)
SELECT * from MyData3;
My output:
refer this similar error example by- Mike Stephenson

Related

From postgres to greenplum via pxf UUID and INET types aint' supported?

So I am creating external table in greenplum based on the table i have in postgres.
And When I create field
field_name uuid
I am getting an error "Field type UNSUPPORTED_TYPE" Same goes to inet type.
What exactly doesn't support uuid type? Because I've read documentation and as far as I understand both postgres and greenplum support uuid type. So I take it the problem is in pxf connection itself?
FORMAT 'CUSTOM' (FORMATTER='pxfwritable_import')
maybe i need another formatter or something?
The full set of types that are supported by Greenplum PXF will depend on the PXF profile that you are reading from/writing to. For example, Apache ORC does not have a native type for UUID (Apache ORC - Types), but PXF will write UUIDs as strings into ORC files:
CREATE WRITABLE EXTERNAL TABLE pxf_uuid_test_w (col1 int, col2 uuid)
LOCATION ('pxf://orc-data/uuid-test?&PROFILE=file:orc')
FORMAT 'CUSTOM' (FORMATTER='pxfwritable_export');
-- CREATE EXTERNAL TABLE
INSERT INTO pxf_uuid_inet_test_w VALUES (1, 'ceb6817b-0ef1-4167-971a-857f10d4afde');
-- INSERT 0 1
However, PXF does not perform the implicit cast of UUID to string when using any of the Parquet profiles (*:parquet)
CREATE WRITABLE EXTERNAL TABLE pxf_uuid_test_w (col1 int, col2 uuid)
LOCATION ('pxf://parquet-data/uuid-test?&PROFILE=file:parquet')
FORMAT 'CUSTOM' (FORMATTER='pxfwritable_export');
-- CREATE EXTERNAL TABLE
INSERT INTO pxf_uuid_test_w VALUES (1, 'ceb6817b-0ef1-4167-971a-857f10d4afde');
-- ERROR: PXF server error : Type 2950 is not supported (seg0 127.0.1.1:6000 pid=191734)
-- HINT: Check the PXF logs located in the '/pxf-base/logs' directory on host 'localhost' or 'set client_min_messages=LOG' for additional details.
As a workaround, you can create your external table using text, varchar, or char column types and include explicit casts where needed:
CREATE TABLE uuid_test(col1 int, col2 uuid) DISTRIBUTED BY (col1);
INSERT INTO uuid_test values (1, 'ceb6817b-0ef1-4167-971a-857f10d4afde');
-- INSERT 0 1
CREATE WRITABLE EXTERNAL TABLE pxf_uuid_test_w (col1 int, col2 text)
LOCATION ('pxf://parquet-data/uuid-test?&PROFILE=file:parquet')
FORMAT 'CUSTOM' (FORMATTER='pxfwritable_export');
-- CREATE EXTERNAL TABLE
INSERT INTO pxf_uuid_inet_test_w SELECT col1, col2::text FROM uuid_test ;
-- INSERT 0 1

PSQL throwing error: relation "serial" does not exist, when creating a table

It happens when i run this code:
CREATE TABLE distributors (
did integer PRIMARY KEY DEFAULT nextval('serial'),
name varchar(40) NOT NULL CHECK (name <> '')
);
I have tried remover the nextval('serial') but to no avail
You want to do this:
CREATE TABLE distributors (
did serial PRIMARY KEY DEFAULT,
name varchar(40) NOT NULL CHECK (name <> '')
);
The serial type is actually a macro. That per docs (Serial) does:
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
Assuming you are on a recent version(10+) of Postgres generated always as identity(see Create Table) is the preferred alternative these days.
nextval('someseries') relies on having an existing series. You can create that with:
CREATE SEQUENCE someseries;
When you removed the nextval, you probably still had the DEFAULT keyword there, which expects a value afterward to define what the default for the column is.

IBM Db2 on Cloud script creating tables in the wrong schema

On IBM Db2 on Cloud I have imported a script. I created a new schema under which I want to have the new tables created, but when I run the script, it keeps trying to create the tables in a previous schema. Not sure how to get the scripts to create the tables in the new schema.
I have tried the below script without the .SQL_GROUPING_SORTING and it tries to add the tables to a different schema. I have changed the default schema in the Run SQL window within db2 to SQL_GROUPING_SORTING and am now getting the error
""KZF72118" does not have the privilege to perform operation "IMPLICIT CREATE SCHEMA".. SQLCODE=-552, SQLSTATE=42502, DRIVER=4.26.14"
DDL statement for table 'HR' database:
CREATE TABLE EMPLOYEES.SQL_GROUPING_SORTING (
EMP_ID CHAR(9) NOT NULL,
F_NAME VARCHAR(15) NOT NULL,
L_NAME VARCHAR(15) NOT NULL,
SSN CHAR(9),
B_DATE DATE,
SEX CHAR,
ADDRESS VARCHAR(30),
JOB_ID CHAR(9),
SALARY DECIMAL(10,2),
MANAGER_ID CHAR(9),
DEP_ID CHAR(9) NOT NULL,
PRIMARY KEY (EMP_ID));
CREATE TABLE JOB_HISTORY.SQL_GROUPING_SORTING (
EMPL_ID CHAR(9) NOT NULL,
START_DATE DATE,
JOBS_ID CHAR(9) NOT NULL,
DEPT_ID CHAR(9),
PRIMARY KEY (EMPL_ID,JOBS_ID));
CREATE TABLE JOBS.SQL_GROUPING_SORTING (
JOB_IDENT CHAR(9) NOT NULL,
JOB_TITLE VARCHAR(15) ,
MIN_SALARY DECIMAL(10,2),
MAX_SALARY DECIMAL(10,2),
PRIMARY KEY (JOB_IDENT));
CREATE TABLE DEPARTMENTS.SQL_GROUPING_SORTING (
DEPT_ID_DEP CHAR(9) NOT NULL,
DEP_NAME VARCHAR(15) ,
MANAGER_ID CHAR(9),
LOC_ID CHAR(9),
PRIMARY KEY (DEPT_ID_DEP));
CREATE TABLE LOCATIONS.SQL_GROUPING_SORTING (
LOCT_ID CHAR(9) NOT NULL,
DEP_ID_LOC CHAR(9) NOT NULL,
PRIMARY KEY (LOCT_ID,DEP_ID_LOC));
With the Db2 on Cloud Lite Plan
The Lite plan uses one database schema.
So the only schema you can use is the one that matches your user name. In your case this would be KZF72118
Create your tables with out a schema name, and they will be created in schema KZF72118.
You would need to use one of the other plans to remove this restriction

Equivalent to UNIQUE IDENTIFIER in PostgreSQL

I was tryiong to switch from MSSQL to PostgreSQL and hence trying to convert queries to PostgreSQL equivalent. However running PostgreSQL query is giving an error:
ERROR: type "uniqueidentifier" does not exist LINE 3: ID
UNIQUEIDENTIFIER DEFAULT UUID_GENERATE_V4()::VARCHAR NO...
^ SQL state: 42704 Character: 38
MSSQL
CREATE TABLE [dbo].[ISS_AUDIT]
(
[ID] UNIQUEIDENTIFIER DEFAULT NEWID() NOT NULL,
[GRAPH_ID] [varchar](196)
PRIMARY KEY(ID)
);
PostgreSQL
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE public.ISS_AUDIT
(
ID UNIQUEIDENTIFIER DEFAULT UUID_GENERATE_V4()::VARCHAR NOT NULL,
GRAPH_ID VARCHAR(196),
PRIMARY KEY(ID)
);
Am I missing something on UNIQUEIDENTIFIER ?
This is the correct script:
CREATE TABLE public.ISS_AUDIT
(
ID uuid PRIMARY KEY DEFAULT UUID_GENERATE_V4(),
GRAPH_ID VARCHAR(196)
);
See this link. Extract:
SQL Server calls the type UniqueIdentifier and PostgreSQL calls the
type uuid. Both types occupy 16-bytes of storage. For compatibility
reasons with other software or databases, many use some stanardized
text representation of them particularly for transport rather than
using the native type.
We need UUID which can be used as below:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE public.ISS_AUDIT(
ID UUID DEFAULT UUID_GENERATE_V4()::UUID NOT NULL,
GRAPH_ID VARCHAR(196),
PRIMARY KEY(ID)
);

How to create tables in a specific database in a DB2 for z/OS subsystem using T-SQL?

Below T-SQL only creates table in the schema of current user, not the database specified. Why?
CREATE TABLE TEST ( URI VARCHAR(255) NOT NULL, PARENT_URI VARCHAR(255) ,
TITLE VARCHAR(1000) NOT NULL, MIMETYPE VARCHAR(100) , DESCRIPTION VARCHAR(1000) ,
MODIFIED_BY VARCHAR(1000)) IN DATABASE DB4TEST;
The table can be created, but I can only see it in current user's default schema, not in the database I specified. The database does exist.
Thanks.
You are confusing the two concepts of schema and database.
A DB2 database can contain multiple schemas.
If you want the table to be created inside SCHEMA DB4TEST in the DATABASE DB4TEST, try this:
CREATE TABLE DB4TEST.TEST ( URI VARCHAR(255) NOT NULL, PARENT_URI VARCHAR(255) ,
TITLE VARCHAR(1000) NOT NULL, MIMETYPE VARCHAR(100) , DESCRIPTION VARCHAR(1000) ,
MODIFIED_BY VARCHAR(1000)) IN DATABASE DB4TEST;