Import csv file to SQL with double quote by bcp - sql-server

I have a csv file need to import into SQL.The csv is splitted by a comma.
part of csv like below:
2008, Albania, 60
2208, "Algeria, Angola", 70
1245, "Ethiopia, France, Polynesia(France)", 23
Table template like this:
CREATE TABLE [Global].[Country](
[ID] [char](10) NOT NULL,
[COuntry] [varchar](max) NOT NULL,
[Count] [tinyint] NOT NULL,
CONSTRAINT [PK_Country] PRIMARY KEY CLUSTERED
(
[ID] ASC,
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
I am using the following command to import the file.
bcp Country in Country.csv -S -U -P -d Global -c -t ',' -e error.log
but got error message like :
SQLState = 22005, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Invalid character value for cast specification
And special symbol like picture below:
I can't figure out how to include the double quotes while inserting the data into the Azure SQL server.
How should I format the file? or what should I do?

Related

AspNetUserLogins table and maximum size of index keys in SQL Server

The schema of the identity model in VS2017/aspnetcore defines a table called AspNetUserLogins table to store external logins (CREATE statement below). It defines the primary key as a composite of [LoginProvider] [nvarchar] (450) and [ProviderKey] [nvarchar] (450). The SQL server limits for the maximum size of index keys is specified at 900 bytes here. A note on that page specifically says
"If a table column is a Unicode data type such as nchar or nvarchar,
the column length displayed is the storage length of the column. This
is two times the number of characters specified in the CREATE TABLE
statement. In the previous example, City is defined as an nvarchar(30)
data type; therefore, the storage length of the column is 60."
So is this key not twice the allowed size?
Sql Server Management Studio seems to think so....
Warning! The maximum key length for a clustered index is 900 bytes.
The index 'PK_AspNetUserLogins' has maximum length of 1800 bytes. For
some combination of large values, the insert/update operation will
fail.
CREATE TABLE [dbo].[AspNetUserLogins](
[LoginProvider] [nvarchar](450) NOT NULL,
[ProviderKey] [nvarchar](450) NOT NULL,
[ProviderDisplayName] [nvarchar](max) NULL,
[UserId] [nvarchar](450) NOT NULL,
CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY CLUSTERED
(
[LoginProvider] ASC,
[ProviderKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Looks like they know...issue1451
It looks as though this will cause subsequent issues. I originally created my database on my desktop prior to deploying it to Azure and there is a significant difference between the 2 databases. In SSMS, using the "Script Table as > CREATE table", the tables designs are:
Azure database:
CREATE TABLE [dbo].[AspNetUserLogins](
[LoginProvider] [nvarchar](225) NOT NULL,
[ProviderKey] [nvarchar](225) NOT NULL,
[ProviderDisplayName] [nvarchar](max) NULL,
[UserId] [nvarchar](450) NOT NULL,
CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY CLUSTERED
(
[LoginProvider] ASC,
[ProviderKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
Desktop database:
CREATE TABLE [dbo].[AspNetUserLogins](
[LoginProvider] [nvarchar](450) NOT NULL,
[ProviderKey] [nvarchar](450) NOT NULL,
[ProviderDisplayName] [nvarchar](max) NULL,
[UserId] [nvarchar](450) NOT NULL,
CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY CLUSTERED
(
[LoginProvider] ASC,
[ProviderKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Note the [PRIMARY] references, I cannot get these into Azure. This results in the following error from a: MVC Net core 2 website using Microsoft.AspNetCore.Identity;
MVC Net Core 2.0 error resulting from the inability to add primary clustered keys

ContainsTable: noise/stop word giving no results

Suppose you have the following queries (using PT language):
declare #textoPesquisa as nvarchar(200) = 'haiti and florida and à'
if(#textoPesquisa is null or #textoPesquisa='') set #textoPesquisa = '*'
select * from Noticias n
left join containstable(NoticiasParaEstatistica, (Titulo), #textoPesquisa,
LANGUAGE N'Portuguese') s on n.IdNoticia=s.[key]
where s.[key] is not null
select * from NoticiasParaEstatistica where contains(titulo,
#textoPesquisa,LANGUAGE N'Portuguese')
I'm under the impression that à is considered a stop word, so the previous queries return no results (due to the fact that I'm using AND). Now, if I turn off the stopword list, everything works fine, but that doesn't look like a good option.
After looking at the docs, I've found the transform noise words option. I've activated it in the server and I've rebuilt the catalog, but I'm still getting 0 results.
Btw, here's table + insert that might be able to reproduce this scenario:
CREATE TABLE [dbo].[NoticiasParaEstatistica](
[IdNoticia] [bigint] NOT NULL,
[Titulo] [varchar](400) NOT NULL
CONSTRAINT [PK_NoticiasParaEstatistica] PRIMARY KEY CLUSTERED
(
[IdNoticia] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
INSERT INTO NoticiasParaEstatistica (Titulo)
values ('haiti florida à')
What am I missing?
thanks!

Add data from one Azure DB table to another Azure DB table

I Want add data from one Azure DB table to another Azure DB table But same server and same table format. this task i want to run every 5 min. how can i do it any idea about it? i'm founded use elastic database but i want to do different way
CREATE TABLE [dbo].[AuditLog](
[AuditLogId] [int] IDENTITY(1,1) NOT NULL,
[TableName] [nvarchar](max) NULL,
[UserId] [int] NULL,
[EmployeeName] [nvarchar](max) NULL,
CONSTRAINT [PK_Application.AuditLog] PRIMARY KEY CLUSTERED ([AuditLogId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO
INSERT INTO [DBTwo].[dbo].
[AuditLog(TableName,UserId,EmployeeName,Actions)
SELECT TableName,UserId,EmployeeName,Actions
FROM [CemexTenant].[Application].[AuditLog]
DELETE FROM [DBOne].[dbo].[AuditLog]

Generating a Database from SQL script in Visual Studio

I was recently given a project using ASP.NET, C#, and an SQL database. I am somewhat familiar with ASP.NET and C#, however I have never used the Microsoft database software in visual studio. I simply need to generate a database from an SQL script given to me. I was able to get the database mostly generated, however some of the code (The part adding items to one of the tables) did not run on execution. I have tried adding other scripts and rerunning the first one, I cannot get anything to work. I know the script is fine, I just cant get it to run correctly. Thanks for the help. Here's the script
/*These first few lines don't run */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Product](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](128) NOT NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[ProductNote](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NOT NULL,
[NoteText] [nvarchar](1000) NOT NULL,
[CreateDate] [datetime] NOT NULL DEFAULT (GETDATE()),
[Archived] [BIT] NOT NULL DEFAULT (0)
CONSTRAINT [PK_ProductNote] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ProductNote] WITH CHECK ADD CONSTRAINT [FK_ProductNote_Product] FOREIGN KEY([ProductID])
REFERENCES [Product] ([ID])
GO
ALTER TABLE [dbo].[ProductNote] CHECK CONSTRAINT [FK_ProductNote_Product]
GO
/*This part does not run either */
DECLARE #n INT = 1
WHILE (#n <=20)
BEGIN
INSERT INTO dbo.Product (Name) SELECT 'Product ' + CONVERT(VARCHAR(10),#n)
SET #n = #n + 1
END
SET ANSI_PADDING OFF
GO
In Visual Studio
Go to View->Solution Explorer
Then Connect your sql Data Base
following links might be helpfull ro connect your db in visual studio
1.http://support.webecs.com/kb/a204/how-do-i-connect-to-sql-server-using-visual-studio-_net.aspx
2.https://support.zen.co.uk/kb/Knowledgebase/SQL-Server-Connecting-to-your-database-through-code-or-Visual-StudioNET
Expand your DB then Right Click on Stored Procedure -> Add new stored procedure
Replace all text with your script->Select All->Right Click ->Run Selection
That's it.

Execute SQL script for a particular Database

I am using Installshield 2015 version.We have created a install script .msi project.
Under SQL Scripts Tab, we have created new SQL Connection.
I am providing CatalogName "NMC" and selected checkbox(Create catalog if absent).
I am running the below SQL Scripts to run against this Database(NMC).
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Account]') AND type in (N'U'))
BEGIN
PRINT 'Tables have already been created'
END
ELSE
BEGIN
/****** Object: Table [dbo].[Rights] ******/
CREATE TABLE [dbo].[Rights](
[RightId] [int] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Rights] PRIMARY KEY CLUSTERED
(
[RightId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I want the Table created on the Database which is mentioned in Catalog(NMC).
Right now the script is executing against Master Database.
How can I execute the script against Catalog mentioned above?I don't want to use command "USE NMC" in SQL Script.
You may change default database MNC for login you use in top part & again change to master at the end...
Exec sp_defaultdb #loginame='login', #defdb='MNC'
Exec sp_defaultdb #loginame='login', #defdb='master'

Resources