SQL Server 2016 with Azure Blob Container , nvarchar(max) is not supported for external tables? - sql-server-2016

I'm trying to create external table as it described in
https://msdn.microsoft.com/en-us/library/mt652315.aspx
using following structure:
CREATE EXTERNAL TABLE dbo.Blob2 (
ID int NOT NULL,
Description nvarchar(max) NULL,
Title nvarchar(max) NULL
)
WITH (LOCATION='/Blob/',
DATA_SOURCE = AzureStorage,
FILE_FORMAT = TextFileFormat
);
and getting error:
Msg 46518, Level 16, State 12, Line 49
The type 'nvarchar(max)' is not supported with external tables.
Am I'm missing something? I'm seeing that in documentation for external tables nvarchar/string/text in the list
https://msdn.microsoft.com/en-us/library/dn935021.aspx
Is there any chance that I can store text data in that container?

Related

Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 4 in Azure Synapse

I have a Spotify CSV file in my Azure Data Lake. I am trying to create external table you SQL serverless pool in Azure Synapse.
I am getting the below error message
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 4 (Track_popularity) in data file https://test.dfs.core.windows.net/data/folder/updated.csv.
I am using the below script
IF NOT EXISTS (SELECT * FROM sys.external_file_formats WHERE name = 'SynapseDelimitedTextFormat')
CREATE EXTERNAL FILE FORMAT [SynapseDelimitedTextFormat]
WITH ( FORMAT_TYPE = DELIMITEDTEXT ,
FORMAT_OPTIONS (
FIELD_TERMINATOR = ',',
USE_TYPE_DEFAULT = FALSE
))
GO
IF NOT EXISTS (SELECT * FROM sys.external_data_sources WHERE name = 'test.dfs.core.windows.net')
CREATE EXTERNAL DATA SOURCE [test.dfs.core.windows.net]
WITH (
LOCATION = 'abfss://data#test.dfs.core.windows.net'
)
GO
CREATE EXTERNAL TABLE updated (
[Artist] nvarchar(4000),
[Track] nvarchar(4000),
[Track_id] nvarchar(4000),
[Track_popularity] bigint,
[Artist_id] nvarchar(4000),
[Artist_Popularity] bigint,
[Genres] nvarchar(4000),
[Followers] bigint,
[danceability] float,
[energy] float,
[key] bigint,
[loudness] float,
[mode] bigint,
[speechiness] float,
[acousticness] float,
[instrumentalness] float,
[liveness] float,
[valence] float,
[tempo] float,
[duration_ms] bigint,
[time_signature] bigint
)
WITH (
LOCATION = 'data/updated.csv',
DATA_SOURCE = [data_test_dfs_core_windows_net],
FILE_FORMAT = [SynapseDelimitedTextFormat]
)
GO
SELECT TOP 100 * FROM dbo.updated
GO
Below is the data sample
My CSV is utf-8 encoding. Not sure what is the issue. The error shows column (Track_popularity). Please advise
I’m guessing you may have a header row that should be skipped. Drop your external table and then drop and recreate the external file format as follows:
CREATE EXTERNAL FILE FORMAT [SynapseDelimitedTextFormat]
WITH ( FORMAT_TYPE = DELIMITEDTEXT ,
FORMAT_OPTIONS (
FIELD_TERMINATOR = ',',
USE_TYPE_DEFAULT = FALSE,
FIRST_ROW = 2
))

Unable to create a table in SQL

I am trying to execute this simple query.
create Table test1
{
ID int identity(1,1),
value nvarchar
}
Its throwing an error as
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '{'
I am stuck here. Please help me out of this
Use () instead of {}
create Table test1
(
ID int identity(1,1),
value nvarchar
)
don't use follow brace. you should use parenthesis.
create Table test1
(
ID int identity(1,1),
value nvarchar
)
Syntax for create table:
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
Use parenthesis ( ) instead of curly braces { }
Set data size for the nvarchar. other wise when insert any string value more than 1 character you will receive String or binary data would be truncated. error
Not mandatory but adding NOT NULL to the IDENTITY column is good practice.
Adding schema name to the table is good practice. (By default dbo is the schema)
So the working query will be:
create Table dbo.test1
(
ID int identity (1, 1) NOT NULL,
value nvarchar (500)
)
Use () instead of {}
Check out this link for the example SQL Create Table
On the internet about the topic you will find many useful information.

Why a function (which is used as default constraint of a column in a table) cannot be changed?

I am working with SQL Server 2008 R2. I have a scalar-valued function which I need to modify. So I created a script which drops that function and then create it again.
When I run the script SQL Server gives me an error:
Msg 3729, Level 16, State 1, Line 2
Cannot DROP FUNCTION 'dbo.udf_GetCurrentUserId' because it is being referenced by object 'DF__generated__creat__02D4B8E6'.
I investigated and find out that this function is being used as a default constraint for a column in a table.
create table generated_email
(
generated_email_pk bigint not null identity,
name varchar(64) not null,
row_version RowVersion not null,
create_user varchar(50) default coalesce(dbo.udf_GetCurrentUserId(),user) not null,
create_datetime datetime default getDate() not null,
update_user varchar(50) default coalesce(dbo.udf_GetCurrentUserId(),user) not null,
update_datetime datetime default getDate() not null
)
I thought ok lets just modify the function. So I created a script which alter that function.
When I run the script SQL Server I get another error:
Msg 3729, Level 16, State 3, Procedure udf_GetCurrentUserId, Line 68
Cannot ALTER 'dbo.udf_GetCurrentUserId' because it is being referenced by object 'DF__generated__creat__02D4B8E6'.
Damn. So I am stuck. Why SQL Server implemented this restriction? Why it thinks that if a function is created it will never need modification?
That's silly that you can't change a function when it's being used in a constraint, but it seems to be a legitimate limitation in SQL Server. You could create a function such as
CREATE FUNCTION dbo.udf_GetCurrentUserIdCaller() AS varchar(8000)
BEGIN
RETURN dbo.udf_GetCurrentUserId()
END
Then your work of unloading and reloading constraints gets reduced to changing all constraints to use dbo.udf_GetCurrentUserIdCaller(), and then you can edit udf_GetCurrentUserId() all you like. SQL Server does not check nested dependencies.
You can programmatically access constraint text to get the information you need to script a solution from
SELECT obj.name as 'Table', col.name as 'Column',object_name(default_object_id) AS [ConstraintLabel],
object_definition(default_object_id) AS [DefaultValue]
FROM sys.objects obj INNER JOIN sys.columns col
ON obj.object_id = col.object_id
where obj.type = 'U' and object_definition(default_object_id) is not null

SQL Server Insert Statement From a CSV File

I'm trying to follow the example on this page:
http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/
The database is on another machine and I'm connecting to it using SQL Server Management Studio 2012.
Here's the insert statement
BULK
INSERT CSVTest
FROM '\\BPWKS\network share\csvtest.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
When I go to check the content of the table, I get the following error:
Error: Msg 208, Level 16, State 82, Line 1
Invalid object name 'CSVTest'.
Table Definition
Create Table CSVTest
(
ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME
)
GO
I've also tried importing the local file at 'C:\csvtest.csv'
Resolution appears to be
Missing Table initially
Missing file or permissions to access
file.

SQL BULK INSERT FROM errors

I'm attempting to insert a CSV file into an Microsoft SQL Server Management Studio database like this:
BULK INSERT [dbo].[STUDY]
FROM 'C:\Documents and Settings\Adam\My Documents\SQL Server Management Studio\Projects\StudyTable.csv'
WITH
(
MAXERRORS = 0,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
But I am getting errors:
Msg 4863, Level 16, State 1, Line 2
Bulk load data conversion error (truncation) for row 1, column 9 (STATUS).
Msg 7399, Level 16, State 1, Line 2
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 2
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Unfortunately, I cannot provide the contents of StudyTable.csv, to protect the privacy of the company we're working for.
EDIT I can vouch for the validity of the csv file though. It was exported from OpenOffice's version of Excel, and I went through and made sure it was valid.
EDIT2
Here's a dummy version of the CSV file:
1234,,,1234,1234,,"asdf","asdf","Z","asd",7/1/2010 12:23,8/5/2010 13:36,9/4/2010 13:36,"(asdf,1661,#1234,F,T)","F",,,"F",,"68866",1234,1234,1234,"F"
Here's a create script for the STUDY table:
CREATE TABLE [dbo].[STUDY]
(
[STUDY_ID] INT IDENTITY(1,1) NOT NULL,
[PARENT_ID] INT,
[GROUP_ID] INT,
[WORKFLOW_NODE_ID] INT,
[STUDY_TEMPLATE_ID] INT,
[INSPECTION_PLAN_ID] INT,
[NAME] VARCHAR(255),
[DESCRIPTION] VARCHAR(4000),
[STATUS] VARCHAR,
[OLD_STATUS] VARCHAR,
[CREATED_ON] DATE,
[COMPLETED_ON] DATE,
[AUTHORIZED_ON] DATE,
[EVENTS] VARCHAR,
[NEEDS_REVIEW] CHAR,
[HAS_NOTES] CHAR,
[HAS_AUDITS] CHAR,
[STUDY_PART] CHAR,
[STUDY_TYPE] VARCHAR,
[EXTERNAL_REFERENCE] VARCHAR,
[CREATED_BY] INT,
[COMPLETED_BY] INT,
[AUTHORISED_BY] INT,
[ARCHIVED_CHILD_COMPLETE] CHAR
)
It sounds like the data in your STATUS column in the .csv file is longer than the definition for the field in your SQL Table.
Check the definition on that field and make sure the data you have in the .csv will fit (both length and type).
The first error is likely because a column is too big to fit the database. First thought is does the CSV file contain column headers?
The second error is very generic.
Do any of your fields contain a comma as part of their data - for example 37, High Street which isn't encased in quotes? I've come unstuck lots of times in the past because of "issues" in the data (the data is valid, just not quite what's expected). If your CSV file is a few thousand rows or more - what happens if you try importing just the first hundred (or ten) rows?