sql server - How to properly insert/update data with central european characters - sql-server

I have a proprietary software that communicates with sql server database for storing data and trying to insert/update records behind the scenes.
Running Select ##version on server returns
Microsoft SQL Server 2014 (SP2-GDR) (KB4019093) - 12.0.5207.0 (X64)
Jul 3 2017 02:25:44
Copyright (c) Microsoft Corporation
Express Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (Hypervisor)
server collation is Slovenian_CI_AS, but all string columns in tables are declared with:
DATA_TYPE: nvarchar
CHARACTER_SET_NAME: UNICODE
COLLATION_NAME: SQL_Latin1_General_CP1_CI_AS
Now if I try updating a field, for example:
UPDATE CategoryText SET Title = 'č' WHERE Category_Id = 1 AND LanguageId = 1060"
The software displays Ä as a title.
Of course, I get the right result (letter č) if i run the following query from the console
select Title from CategoryText where Category_Id = 1 AND LanguageId = 1060
But I would like to insert the text the way that will be displayed properly in the software.
Doing this from ubuntu with en_US.UTF-8 local settings, tried also from windows without luck.
I can change my local settings, I can change the collation of the string I'm importing.
I cannot change the way software is communicating with the server and I do not think I can alter the tables in the database.

Related

SQL Server datetime2 parse issue

When I run the following SQL at my local SQL Server instance:
DECLARE #t DATETIME2 = '2019-12-12 00:00:00'
I get the following error:
Unable to convert #t to a System.Data.SqlClient.SqlParameter object. The specified literal cannot be converted to DateTime2(System.Data.SqlDbType), as it used an unsupported date/time format. Use one of the supported date/time formats. Literal value: 2019-12-12 00:00:00
The only way to fix it is to add 'T' between date and time
DECLARE #t DATETIME2 = '2019-12-12T00:00:00'
At the same time, this script runs perfectly fine at my production server (without adding 'T').
I tried to find the difference and don't see anything that could affect the behavior:
Collation is the same at both servers - SQL_Latin1_General_CP1_CI_AS
Language is the same - English (United States)
Server version: LOCAL is Microsoft SQL Server 2017 Developer Edition (64-bit) v14.0.2027 RTM; REMOTE is Microsoft SQL Server 2017 Enterprise Edition: Core-based Licensing (64-bit) v14.0.1000 RTM
Host OS: LOCAL is Windows 10 Pro; REMOTE is Windows Server 2016 Datacenter
Can somebody explain, why I'm getting the error locally?
This is a client-side error so it seems you are running the query from SSMS with the Enable Always Encrypted (column encryption) option for the connection (connection Options-->Always Encrypted) on the remote client but not the prod server. With the option enabled, the SSMS client parses literals in order to build parameters for AE.
Unlike '2019-12-12 00:00:00', the ISO 8601 datetime format literal '2019-12-12T00:00:00' is unambiguous and can be reliably parsed regardless of the client regional settings.
So your options are to either use an ISO 8601 datetime format or turn of the SSMS Enable AE option for the connection.

Altered table structures not available in database diagram (localdb)

Using Management Studio 17.8.1 I added a column to my table saved and refreshed.
However when I create a database diagram making use of the table, the column is missing.
Version
Microsoft SQL Server 2016 (SP1) (KB3182545) - 13.0.4001.0 (X64) Oct 28 2016 18:17:30
Copyright (c) Microsoft Corporation Express Edition (64-bit) on Windows 10 Pro 6.3 <X64> (Build 17763: ) (Hypervisor)
[Update]
The following table will display in the designer
CREATE TABLE [dbo].[Table_1](
[Id] [int] NOT NULL,
[smallId] [smallint] NULL
) ON [PRIMARY]
[Update]
Strangely the designer is even showing deleted tables.
Closing MMSM and re-opening will allow the diagrams to use the new structures.
Could it be a bug in localdb ?
When I tried altering a table I got a message
Microsoft SQL Server Management Studio has detected that an operation is blocking user input.
This can be caused by an active modal dialog or a task that needs to block user interaction.

How to insert greek characters into sqlserver table

I have a simple insert, but with greek characters
INSERT INTO tmp (fname) VALUES ('ΚΩΝΣΤΑΝΤΙΝΟΣ')
I have tried creating the table in two ways:
create table tmp (fname varchar(40))
and
create table tmp (fname nvarchar(40))
When I then select the data:
select * from tmp
I get:
?O?S???????S
I'm using:
Microsoft SQL Server 2005 - 9.00.4060.00 (Intel X86)
Mar 17 2011 13:20:38
Copyright (c) 1988-2005 Microsoft Corporation
Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
and
Microsoft SQL Server 2012 (SP1) - 11.0.3000.0 (X64)
Oct 19 2012 13:38:57
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (Hypervisor)
it does the same in both.
I'm using:
Microsoft SQL Server Management Studio 11.0.2100.60
to create the table and insert the data etc.
How do I get Greek characters to store correctly or if they are stored correctly to display correctly when I select them?
Try to use prefix Unicode character string:
create table tmp (fname nvarchar(40))
INSERT INTO tmp (fname) VALUES (N'ΚΩΝΣΤΑΝΤΙΝΟΣ')
Also there may be a problem with collation of the columns, please set
create table tmp (fname nvarchar(40) COLLATE SQL_Latin1_General_CP1253_CI_AI)
From MSDN:
Prefix Unicode character string constants with the letter N. Without
the N prefix, the string is converted to the default code page of the
database. This default code page may not recognize certain characters.

Stored procedure doesn't do anything in remote server when called with "Exec procedure"

I'm puzzling with a Stored procedure that has different behavior when run with an withoout the Exec keyword.
I have 2 servers, Server 1:
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) Apr 2 2010 15:48:46 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.0 (Build 6002: Service Pack 2) (Hypervisor)
Server 2:
Microsoft SQL Server 2008 (SP2) - 10.0.4000.0 (X64) Sep 16 2010 19:43:16 Copyright (c) 1988-2008 Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) (VM)
A stored procedure (ACTUALIZA_MANTE_FULL) in Server 1 that deletes and inserts data to a table in Server 2 from tables in server 1.
When I run "EXEC ACTUALIZA_MANTE_FULL" the process returns no errors but the destinatio table has no rows.
When I run "ACTUALIZA_MANTE_FULL" (without EXEC) the process runs with no errors and the data is inserted correctly.
Why the difference?, what i'm missing?
EDIT: procedure pseudo code (sorry can't put actual code)
CREATE PROCEDURE MIPROC<br>
AS <br>
declare #from datetime<br>
set #from = getdate()-30<br>
<br>
delete from [ADMSRV].remotedb.dbo.remoteTable where t_date >= #from<br>
<br>
Insert into [ADMSRV].remotedb.dbo.remoteTable <br>
Select fields, sum(datum) as datum<br>
form localdb.dbo.localtable1<br>
where ld_date >= #from<br>
group by ields <br>
union <br>
Select fields, sum(datum) as data<br>
form localdb2.dbo.localtable2<br>
where ld_date >= #from<br>
group by fields<br>
GO<br>
the fields in (fields) are same cuantity, and type of PK in remoteTable
Remote table has PK only, no FK, no unique constraint, no unique index or triggers.

RODBC management of dates from SQL server

I have a SQL database (2012) that I am trying to access through R (R Studio). From Enterprise Manager this is what it looks like:
select top 5[date],value from dbo.history
1991-02-11 11.1591
1991-02-12 11.2
1991-02-13 11.3501
1991-02-14 11.37
1991-02-15 11.3002
However from R this is what I get:
sqlQuery(DF_CN,'select top 5 [date],value from dbo.history')
date value
1 0011-02-19 11.16
2 0012-02-19 11.20
3 0013-02-19 11.35
4 0014-02-19 11.37
5 0015-02-19 11.30
When I try and select all the data from the table, this is what I get
sqlQuery(DF_CN,'select * from dbo.history')
Error in charToDate(x) :
character string is not in a standard unambiguous format
It may to be something about the yyyy-mm-dd default format from SQL server which I can change if I use CONVERT, but this looks like a hack and the SELECT * would not work.
Is there something in R I can do to recognise the SQL Server dates?
Ben
This is driving me nuts - surely someone has seen this before - there is a clear disconnect between the SQL Server output and what R is reading.
I am using RStudio 0.98.1091 and R x64 3.1.2.
Sql Server 2014
Microsoft SQL Server Management Studio 12.0.2000.8
Microsoft Data Access Components (MDAC) 6.1.7601.17514
Microsoft MSXML 3.0 4.0 6.0
Microsoft Internet Explorer 9.10.9200.17148
Microsoft .NET Framework 4.0.30319.18444
Operating System 6.1.7601
ODBC Driver 11 for SQL Server.
Everything looks up to date on my system.
the [date] column was of type DATE, is now DATETIME. I now get this;
sqlQuery(DF_CN,('select * from dbo.history')
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
It looks like some disconnect between SQL server setup and R setup.
I worked it out by using the as.is parameter:
sqlQuery(DF_CN,'select * from dbo.history',as.is=T)
Then casting the values I needed directly in R.
I have worked it out.
Driver set to SQL server Native Client 11.0
ODBC settings - Use regional settings when outputting currency, numbers, dates and times switched off.
I am thinking that SQL Server has its own ideas on regional settings rather than what has been provided by windows causing great confusion.

Resources