Error using Liquibase on SQL Server - sql

I'm experimenting with Liquibase, trying to get it to copy one database to another. Unfortunately, I keep getting this error:
Implicit conversion from data type varchar to varbinary is not allowed.
Use the CONVERT function to run this
query.
The SQL it's generating is here:
CREATE TABLE [dbo].[Attachment] (
[Applicantid] uniqueidentifier NOT NULL,
[Attachmentid] uniqueidentifier CONSTRAINT DF_Attachment_Attachmentid DEFAULT '(newid())' NOT NULL,
[AttachmentType] INT CONSTRAINT DF_Attachment_AttachmentType DEFAULT 0 NOT NULL,
[FileAttachment] image NOT NULL,
[FileName] ntext NOT NULL,
[FileType] nvarchar(125) NOT NULL,
[Filesize] INT NOT NULL,
[CCN] varbinary(8) CONSTRAINT DF_Attachment_CCN DEFAULT '0' NOT NULL,
[CreateDate] DATETIME CONSTRAINT DF_Attachment_CreateDate DEFAULT (getdate()) NOT NULL,
[LastUpdate] DATETIME CONSTRAINT DF_Attachment_LastUpdate DEFAULT (getdate()) NOT NULL,
CONSTRAINT [PK_Attachment] PRIMARY KEY (Attachmentid)
):

Those liquibase project guys need to learn a few things about more recent versions of SQL Server - 2005 and up:
NTEXT is long been deprecated - use NVARCHAR(MAX) instead
IMAGE is long been deprecated, too - use VARBINARY(MAX) instead
(those datatypes would be OK if you're dealing with SQL Server 2000 or earlier, however).
Plus: just from that table definition, there's no way to be able to figure out why you get this error. Is this your original (source) table, or is this what Liquibase generates as a target table?? If so: any chance you can see / trace / inspect the SQL statements used to migrate the data from the old to the new table??
Maybe there's a varchar column there that you try to convert to varbinary implicitly...

Related

Why does Diesel fail to migrate a PostgresSQL database when the columns specify a length? [duplicate]

I am experimenting with PostgreSQL coming from SQL using MySQL and I simply wish to create a table with this piece of code which is valid SQL:
CREATE TABLE flat_10
(
pk_flat_id INT(30) DEFAULT 1,
rooms INT(10) UNSIGNED NOT NULL,
room_label CHAR(1) NOT NULL,
PRIMARY KEY (flat_id)
);
I get the error
ERROR: syntax error at or near "("
LINE 3: pk_flat_id integer(30) DEFAULT 1,
I have conducted searches on the web and found no answer and I cant seem to find an answer in the PostgreSQL manual. What am I doing wrong?
I explicitly want to set a limit to the number of digits that can be inserted into the "pk_flat_id" field
I explicitly want to set a limit to the number of digits that can be inserted into the "pk_flat_id" field
Your current table definition does not impose a "size limit" in any way. In MySQL the parameter for the intdata type is only a hint for applications on the display width of the column when displaying it.
You can store the value 2147483647 in an int(1) without any problems.
If you want to limit the values to be stored in an integer column you can use a check constraint:
CREATE TABLE flat_10
(
pk_flat_id bigint DEFAULT 1,
rooms integer NOT NULL,
room_label CHAR(1) NOT NULL,
PRIMARY KEY (flat_id),
constraint valid_number
check (pk_flat_id <= 999999999)
);
The answer is that you use numeric or decimal types. These are documented here.
Note that these types can take an optional precision argument, but you don't want that. So:
CREATE TABLE flat_10
(
pk_flat_id DECIMAL(30) DEFAULT 1,
rooms DECIMAL(10) NOT NULL,
room_label CHAR(1) NOT NULL,
PRIMARY KEY (pk_flat_id)
);
Here is a SQL Fiddle.
I don't think that Postgres supports unsigned decimals. And, it seems like you really want serial types for your keys and the long number of digits is superfluous.
Changing integer to numeric works.
CREATE TABLE flat_10
(
pk_flat_id bigint DEFAULT 1,
rooms numeric NOT NULL,
room_label CHAR(1) NOT NULL,
);

MS SQL explicitly using "default defaults" on NOT NULL fields - why?

I stumbled upon this definition:
CREATE TABLE dbo.whatever (
[flBlahBlah] BIT DEFAULT ((0)) NOT NULL,
[txCity] NVARCHAR (50) DEFAULT ('') NOT NULL,
[cdFrom] VARCHAR (10) DEFAULT ('') NOT NULL
);
I can't think of a reason to add those default values. Not null string is defaulted to '' and bit is defaulted to 0. Is there a reason for defining these default values? Am I missing something? Is this in some best practice handbook I'm not aware of?
I'd just use:
CREATE TABLE dbo.whatever (
[flBlahBlah] BIT NOT NULL,
[txCity] NVARCHAR (50) NOT NULL,
[cdFrom] VARCHAR (10) NOT NULL
);
The database is in MS SQL Server 2012, now migrating to Azure Database.
For example you create table from a first batch of your question. Then insert value like this
INSERT INTO dbo.whatever (flBlahBlah) VALUES (1)
You will get 1 row dbo.whatever
flBlahBlah txCity cdFrom
1
So if you "forget" to insert in one of the column with default values determined - SQL Server will take care of them.
It is very useful when you got table, in which you need to insert new field. With default value determined you don't need to change SP/query's/other stuff that works with this table.

How can I prevent SqlPackage.exe from dropping and re-creating constraints when deploying a dacpac?

I have a Visual Studio sql project with a table defined like the following:
CREATE TABLE [dbo].[Hoerses]
(
[HoersId] INT NOT NULL PRIMARY KEY,
[DatePurchased] datetime NOT NULL CONSTRAINT [DF_Hoerses_DatePurchased] DEFAULT DATETIMEFROMPARTS(1985,01,01,0,0,0,0)
)
When I target a preexisting SQL database with a "Script" command
sqlpackage.exe /Action:Script /SourceFile:DatabaseProject1.dacpac /Profile:publish.xml /OutputPath:deployscript_test.sql /TargetPassword:redacted
Then I get the following generated SQL even though the constraint had the same name and definition before & after:
PRINT N'Dropping [dbo].[DF_Hoerses_DatePurchased]...';
GO
ALTER TABLE [dbo].[Hoerses] DROP CONSTRAINT [DF_Hoerses_DatePurchased];
GO
PRINT N'Creating [dbo].[DF_Hoerses_DatePurchased]...';
GO
ALTER TABLE [dbo].[Hoerses]
ADD CONSTRAINT [DF_Hoerses_DatePurchased] DEFAULT DATETIMEFROMPARTS(1985,01,01,0,0,0,0) FOR [DatePurchased];
GO
PRINT N'Update complete.';
GO
(My main concern with trying to prevent this superfluous re-creation is because I occasionally see a "Lock request time out period exceeded." error when it's trying to drop a constraint during actual deployments/publishing)
The problem was apparently in the use of DATETIMEFROMPARTS.
If I instead declare the table as
CREATE TABLE [dbo].[Hoerses]
(
[HoersId] INT NOT NULL PRIMARY KEY,
[DatePurchased] datetime NOT NULL CONSTRAINT [DF_Hoerses_DatePurchased] DEFAULT '1985-01-01'
)
Then SqlPackage.exe no longer tries to drop & re-add the constraint.
Dacpac deployment works by comparing an XML construct of the Database schema with one from your VS project.
Sometimes this process is confused by syntactic niceties resulting in repeating changes such as you describe.
My recommendation is that you go to a deployed database, script out the troublesome object and paste that back over the implementation your Visual Studio file for that object. This resolves the apparent discrepancy so the deployment is no longer fooled into believing it has a change to implement.
I have also found an article which helped me.
http://johnnydba.blogspot.com/2015/07/are-your-vs-database-projects-dropping.html
Basically these are the possible issues mentioned in the article:
Some system functions are represented as lowercase in SQL Server like getdate(), sysutcdatetime(), getutcdate(), and newid().
myDate datetime2 DF_myTable_myDate DEFAULT(getdate()) NOT NULL
Scalar numerical values in default constraints need to be enclosed in double parenthesis ((0)), but I noticed that string values do not.
myBit bit DF_myTable_myBit DEFAULT((0)) NOT NULL
Filtered index and check constraint conditions must be enclosed in parenthesis (), the column names must be in square brackets, and the comparison operator must not have spaces between the left and right side.
-- Column check
CONSTRAINT CH_myTable_someCheck CHECK ([myColumn]>(0))
-- Function check
CONSTRAINT CH_myTable_anotherCheck CHECK ([dbo].[someFunc]([myColumn],[myAnotherColumn])=(1))
Some columns must be enclosed in square brackets []
It's the keyword case used for the constraints.
Use lowercase for any keyword like getdate() or newid() in your defaults.
These constraints will be always dropped:
CREATE TABLE [dbo].[APPLICATION_ERROR]
(
[ERROR_ID] UNIQUEIDENTIFIER
CONSTRAINT [CT_ERROR_ID] DEFAULT (NEWID()) NOT NULL,
[ERROR_DATE] DATETIME
CONSTRAINT [CT_ERROR_DATE] DEFAULT (GETDATE()) NULL
);
these will not:
CREATE TABLE [dbo].[APPLICATION_ERROR]
(
[ERROR_ID] UNIQUEIDENTIFIER
CONSTRAINT [CT_ERROR_ID] DEFAULT (newid()) NOT NULL,
[ERROR_DATE] DATETIME
CONSTRAINT [CT_ERROR_DATE] DEFAULT (getdate()) NULL
);
If the constraint is numeric, you should use double parenthesis:
[LABOR_AMOUNT] MONEY
CONSTRAINT [DF_LABOR_AMOUNT]
DEFAULT ((0)) NULL
You should also add this to your publishing profile, but it's not the core of the issue:
<IgnoreKeywordCasing>True</IgnoreKeywordCasing>

SQL server Invalid Column name Invalid object name

I'm having a problem with a table I created. I am trying to run a query however a red line appears under my code ('excursionID', and 'excursions'), claiming 'Invalid Column name 'excursionID' and 'Invalid object name 'dbo.excursions' even though I have created the table already!
Here is the query
SELECT
excursionID
FROM [dbo].[excursions]
Here is the query I used to create the table
USE [zachtravelagency]
CREATE TABLE excursions (
[excursionID] INTEGER NOT NULL IDENTITY (1,1) PRIMARY KEY,
[companyName] NVARCHAR (30) NOT NULL,
[location] NVARCHAR (30) NOT NULL,
[description] NVARCHAR (30) NOT NULL,
[date] DATE NOT NULL,
[totalCost] DECIMAL NOT NULL,
I've tried dropping the table and inserting table again.
For some reason all my other tables work, it's just this table that doesn't identify itself. I'm very new to SQL so thank you for your patience!
You use DB [zachtravelagency] for create table.And You dont use this DB in your query. Default used db master in SSMS. Try
SELECT
excursionID
FROM [zachtravelagency].[excursions]

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).