What benefit does liquibase "splitStatements" provide? - liquibase

liquibase version being used - org.liquibase:liquibase-core:3.8.2. (not pro version)
Liquibase documents (1 & 2) says below about splitStatements (defaults to true)
Set to false to not have Liquibase split statements on ;'s and GO's.
Defaults to true if not set
and
Removes Liquibase split statements on ;'s and GO's when it is set to
false. Default value is: true.
Another useful sof post i found - In Liquibase is it OK to have an empty line on splitstatements?
I understand - when splitStatements is true, liquibase splits the statements on ; and GO
It not entirely clear what benefit splitStatements adds - i.e if SQL statement are split on ; (end delimiter) or not, what difference will it make - i.e if the statements are executed in a single query or multiple queries - won't the db handle the ";" based stuff anyway. This seems to be essential to understand. --could some one quote an example.
My current project has splitStatements:false. what advantages are we getting by disabling splitStatements. -any example would be greatly appreciated.
------------------------question expanded after answer from #user13579
below is an extract from a liquibase changelog file. This is what brought me to this question. It has splitStatements:false and a ;s in script and it works. With splitStatements:false i would expect a error in this case and the answer I suppose also suggests an error in this case. The below is from production code so I am NOT sure how it works and the backend is POSTGREs. Can someone explain.
--liquibase formatted sql
--changeset adam:001-users-001 failOnError:true splitStatements:false logicalFilePath:001-users.sql
CREATE TABLE sys_users
(
user_id SERIAL,
first_name character varying(64) NOT NULL,
last_name character varying(64) NOT NULL,
email character varying(255) NOT NULL
)
WITH (
OIDS=FALSE
);
CREATE TABLE user_role
(
role_id SERIAL,
role_name character varying(255) NOT NULL,
description character varying(255) NOT NULL,
created_on timestamp(6) with time zone NOT NULL,
created_by character varying(64) NOT NULL,
)
WITH (
OIDS=FALSE
);

Related

Can't create table with script because of ENUM

I'm currently learning some basic stuff about sql in school. We received an exercise in which consists in creating a script that creates tables. We have a schema and we need to recreate it.
I'm having some issues with this one:
When I run the script, it shows this error:
CREATE TABLE "EMPLOYEE" ( "BIRTH_DATE" DATE , "FIRST_NAME" VARCHAR(14) , "LAST_NAME" VARCHAR(16) , "GENDER" ENUM('M','F') , "HIRE_DATE" DATE ) IN "TS_EMPLOYEE"
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "(" was found following "16) , "GENDER" ENUM".
Expected tokens may include: "DEFAULT". SQLSTATE=42601
I looked for the error on the internet and thought I should specify the DEFAULT. For this reason I modified the script adding this part:
"GENDER" ENUM('M','F')DEFAULT 'M' ,
Unfortunately it didn't help me much, since it indicates me the same mistake as before.
Does anyone know where I am wrong? Or what I could change?
Any kind of help is appreciated! ^^
I don't think DB2 supports enums. If you are using DB2, then use a check constraint:
CREATE TABLE EMPLOYEE (
BIRTH_DATE DATE,
FIRST_NAME VARCHAR(14),
LAST_NAME VARCHAR(16),
GENDER CHAR(1),
HIRE_DATE DATE,
CHECK (GENDER IN ('M', 'F'))
);
Notes:
I removed the double quotes. Just don't use them for identifiers. They only clutter queries and introduce the possibilities for strange errors.
I think such a table should have a primary key, although I have not added one.
The lengths of the strings for the names seems unnecessarily short.

Postgres copy error extra data after last expected column from SQL Server BCP file

I am migrating a database from SQL Server 2016 hosted on Windows to Postgres 11 hosted on Debian.
I am exporting data with the BCP utility from SQL Server 2016 and am importing it in Postgres 11 with the COPY command.
For a lot of tables it works, but for some, I keep getting the "extra data after last expected column" error, even if my file contains the same amount of columns. It seems that COPY command has trouble with lines that contains empty strings, showned as "NUL" in Notepad++.
Here is the definition of my table in SQL Server. (table and column names changed)
Create table test (
TypeId int not null,
Name nvarchar(50) not null,
License nvarchar(50) not null,
LastChanged timestamp not null,
Id1 uniqueidentifier not null,
Id2 uniqueidentifier not null,
DescriptionCol nvarchar(256) not null default '',
ConditionCol bit not null default 0,
ConditionCol2 bit not null default 0,
ConditionCol3 bit not null default 1,
DescriptionCol2 nvarchar (2) not null default ''
)
And here is the table definition in Postgres.
CREATE TABLE test (
typeid integer NOT NULL,
name citext COLLATE pg_catalog."default" NOT NULL,
license citext COLLATE pg_catalog."default" NOT NULL,
lastchanged bytea NOT NULL,
id1 uuid NOT NULL,
id2 uuid NOT NULL DEFAULT uuid_generate_v4(),
descriptioncol text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::text,
conditioncol boolean NOT NULL DEFAULT false,
conditioncol2 boolean NOT NULL DEFAULT false,
conditioncol3 boolean NOT NULL DEFAULT true,
descriptioncol2 text COLLATE pg_catalog."default" NOT NULL
)
I extract the data that way:
bcp Database.Schema.test out E:\MyFile.dat -S ServerName -U User -P Password -a65535 -c -C 65001
And then I connect to the remote Postgres server and import data that way:
\copy Schema.test FROM 'E:\MyFile.dat' (DELIMITER E'\t', FORMAT CSV, NULL '', ENCODING 'UTF8');`
Now if I open the file that was generated in Notepad++, I will see "NUL" characters and that seems to be the problem that the COPY command cannot take.
If I try to put some data in the "NUL" caracter on the first row, then the copy command gives me the "extra data after last expected column" on the third row instead of the first row. I cannot edit the file and replace the "NUL" character with something else as I have hundreds of tables to migrate with some very big tables.
I need to either specify an option to SQL Server BCP utility or to Postgres COPY command in order to make this work.
As it is stated by #Tometzky,
bcp utility represents an empty string as a null and a null string as an empty string.
this explains the cause of unwanted behavior.
As an alternative to bcp, you may consider to use ssis (Microsoft SQL Server Integration Services) for this manner. It is easy to use and has wide range of compatibility between DBMS Systems.

When creating a table in PostgreSQL, the table is created, but the columns aren't [duplicate]

This question already has answers here:
PostgreSQL - query syntax without quotes
(3 answers)
Closed 8 years ago.
When I run the following statement, the table is created, but there are no
columns:
CREATE TABLE "mod_1237" ("Collecteddepth" float8 NOT NULL, "Collectedtime"
float8 NOT NULL, "CollectedData" Varchar(45) NOT NULL, "Collectedpass"
float8 NOT NULL, "Collectedmodtime" float8 NOT NULL) WITH (OIDS = FALSE);
I know I'm not creating a primary key, but that shouldn't prevent the columns from being generated. When I run this code it also doesn't generate any errors so everything looks fine until I try to write to the table. Any ideas as to why this wouldn't work or how to make a table with the given columns?
I'm using PostgreSQL version 9.1.4 on Windows Server Standard SP2
This was answered by Hubert ("Depesz") on the PostgreSQL mailing list, where the question was cross-posted. For the reference of anyone reading it here, here is the answer. Since it hasn't been added to the archives yet that link will 404 for a little while, so I'll reproduce Herbert's answer below:
most likely you did insert like:
insert into mod_1237 (Collecteddepth) values (...)
i.e. you didn't quote the column names. Hence the problem.
In psql, you can do:
\d mod_1237
and you will see the columns are there.
Best regards,
depesz
In other words: If you double-quote names in table definitions, double-quote them wherever you refer to them too.
This is a FAQ; see:
PostgreSQL - query syntax without quotes
Omitting the double quote to do query on PostgreSQL
and more.
Just get rid of the quotes as they are not needed.
CREATE TABLE mod_1237 (Collecteddepth float8 NOT NULL, Collectedtime float8 NOT NULL, CollectedDate Varchar(45) NOT NULL, Collectedpass float8 NOT NULL, Collectedmodtime float8 NOT NULL) WITH (OIDS = FALSE);

sqlite3 Error Executing SQL From File

I am trying to create tables in an SQLite database with sqlite3.
The command $ sqlite3 mydb < mytables.sql produce the following error: Incomplete SQL: ??C.
mytables.sql is:
CREATE TABLE SizeCulture (
SizeCultureID INTEGER PRIMARY KEY ASC,
SizeID INTEGER NULL,
CultureID TEXT NULL,
Name TEXT NULL,
Description TEXT NULL,
Abbreviation TEXT NULL,
);
CREATE TABLE Size(
SizeID INTEGER PRIMARY KEY ASC ,
Creation TEXT NOT NULL,
Modification TEXT NOT NULL,
Deleted INTEGER NOT NULL,
);
/****** Object: Table [Ordering].[BarCode] Script Date: 11/09/2011 14:58:19 ******/
CREATE TABLE BarCode(
BarCodeID INTEGER PRIMARY KEY ASC NOT NULL,
BarCodeValue TEXT NOT NULL,
);
This was modified from a script generated by SQL Server, where some tables need to be replicated on an Android device.
The above is just a set of repeating create table statements. From what I understand, SQLite follows standard SQL (like MySQL or postgres).
Though I can't test it at the moment, I think it's the trailing commas that are confusing it (for example, the comma at the end of Abbreviation TEXT NULL,). Try removing all those trailing commas.
Edit: To be clear, I'm talking about all of these commas:
Abbreviation TEXT NULL,
...
Deleted INTEGER NOT NULL,
...
BarCodeValue TEXT NOT NULL,
I had the same problem, but for a different reason (so I'm commenting because Google led me here). Turns out you can also encounter this error if your file has a weird encoding (like UCS-2 instead of UTF8).

sql: making a table structure for injections

I want to take the values from this site for the country table in my database.
The problem is that they don't provide the table structure, so I have to create one, but I cannot get it right - my phpMyAdmin keeps displaying an error when I want to inject the data into the table I created below:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMERIC, alpha3, name, officialName) VALUES ('004','AFG','Afghanistan','Afghan' at line 1
--
-- Table structure for table `countrytable`
--
CREATE TABLE IF NOT EXISTS `countrytable` (
`NUMERIC` int(11) NOT NULL,
`alpha3` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`officialName` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I think my table structure is incorrect. How can I fix it? Thanks!
Try all varchar fields to get the data in since all fields are in quotes in the string you have.
NUMERIC is reserved word in mysql
add in back-tick or quote it -> http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
`alpha3` should be a varchar(3) (or larger), not an int(11).