I am trying to import data from SQL Server to mysql using OPENQUERY with a linked server.
I have imported couple of tables but I am having an issue with a table that has a long varchar field.
everytime i run this query I get the following
Msg 7344, Level 16, State 1, Line 2
The OLE DB provider "MSDASQL" for linked server "Serv1" could not INSERT INTO table "[MSDASQL]" because of column "Notes". Could not convert the data value due to reasons other than sign mismatch or overflow.
the column Notes is of the type varchar(8000) in SQL servr and also varchar(8000) in MySQL.
What is the issue? why it is giving me this error? not I have tried to case Notes to varchar(8000) first but that did not work.
INSERT OPENQUERY (Serv1, 'SELECT
id,
i3_identity,
mid,
Notes,
Comments,
result_code,
Disposition,
completed_on,
calltype
FROM finaltesting.activities')
SELECT
CAST(ID AS int) AS id,
CAST(identity AS int) AS identity,
CAST(merchantAccount AS varchar(255)) AS mid,
Notes,
CAST(Comments AS varchar(8000)) AS Comments,
CAST(FinishCode AS varchar(255)) AS result_code,
CAST(Disposition AS varchar(255)) AS Disposition,
CAST(callDate AS datetime) AS completed_on,
CAST(CallType AS varchar(255)) AS calltype
FROM activities
Could not convert the data value due to reasons other than sign mismatch or overflow.
I was able to solve this problem by changes the column type in MySQL from varchar(8000) to Text. I am using MySQL 5.6.12. It is probably column type issue when converting form SQL Server to MySQL Server.
Thanks
Related
I'm trying to import tables from a MYSQL database via a linked server. The MYSQL database has all the date fields set to a default of '0000-00-00'. I can't even list the contents via a stored procedure as I get the following error.
An unexpected NULL value was returned for column "[MYSQL_progmgt]...[dbo.project].date_completed" from OLE DB provider "MSDASQL" for linked server "MYSQL_progmgt". This column cannot be NULL.
If I use any other table that doesn't have dates everything works fine.
I also need to import tables (that contain dates) from another MYSQL database via a linked server as well and have no problems as the default date fields are left as NULL values.
My stored procedure is
USE [TEST_COPY_PETER]
GO
/****** Object: StoredProcedure [dbo].[RAP_weekly] Script Date: 2/25/2022 10:57:03 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[RAP_weekly]
AS
Select * from [MYSQL_progmgr]...[dbo.project]
I've also tried to retrieve a single field for clarity with
Select NULLIF(date_completed,'1901-01-01') as date from [MYSQL_progmgr]...[dbo.project]
Select COALESCE(date_completed,'1901-01-01') as date from [MYSQL_progmgr]...[dbo.project]
Can't figure it out.
My sql server is version 2019 we are using MYSQL 5.3 ODBC driver.
The table on the MySQL side has the date fields set as
deployment_date` date NOT NULL DEFAULT '0000-00-00'
table on the SQL server side is not created yet as I can't even read the MySQL table.
Pete
here is the modified working code.note the quadruple single quotes.
DECLARE #OPENQUERY nvarchar(4000), #TSQL nvarchar(4000), #TSQL_SELECT nvarchar(4000), #LinkedServer nvarchar(4000)
SET #LinkedServer = 'MYSQL_ECHEANCIER'
SET #OPENQUERY = 'Select nullif( project.deployment_date, ''''0000-00-00'''') as deployment_date FROM OPENQUERY('+ #LinkedServer + ','''
SET #TSQL = 'SELECT * from dbo.project'')'
EXEC (#OPENQUERY+#TSQL)
If a column does not accept NULL value; the appreciate solution is two handle the NULL value all the time. In your case use the minimum value of datatime which is 1753-01-01.
ISNULL(date_completed, '1753-01-01')
OR
ISNULL(date_completed, Cast('1753-01-01' as DateTime))
Very new to SQL and having some trouble understanding what part of this statement is causing an error. There is a db named ap, with a table named invoices with a column named invoice_table. The textbook says I should be able to write a single select statement to return the 4 columns, invoice_total, a FORMAT on invoice_total with 1 decimal point, a CONVERT of invoice_total as an INT and a CAST of invoice_total as an INT. But I get an error 1064 every time I try to run it. Running mysql workbench with Innodb as dbms.
USE ap;
SELECT invoice_total,
FORMAT(invoice_total, 1),
CONVERT(invoice_total, INT),
CAST(invoice_total AS INT)
FROM invoices
;
If you are using MySQL, that cannot CONVERT or CAST to INT. Only to SIGNED or UNSIGNED.
I'm using MySQL 5.6.17 on Amazon Web Services RDS and when calling SELECT UUID_SHORT() I'm getting a number bigger than 9223372036854775807. For example the number I get is
12057145185130250250
The problem is in my table I have a column as BIGINT(20) unsigned, but when storing a number 12057145185130250250 I get the error MySQL 22003
'MySQL 22003 Out of range value for column '' at row 1'
If I run SELECT UUID_SHORT() on our test server which is MySQL 5.6.11 (running on Windows 2008 64x) the result is as follows;
23526798209843216
I changed the column I'm trying to save my number to, as BIGINT(20) unsigned, but still get this error.
Any ideas why ?
UPDATE
Further to my initial post, I found that if I manually insert the value '12057145185130250250' using Workbench editor into the BIGINT(20) column, it saves fine.
However, if I save the value using a stored procedure like below this is when I get the problem;
DELIMITER $$
CREATE DEFINER=`testaccount`#`%` PROCEDURE `CreateCustomer`(iUserId
INTEGER, sPassword VARCHAR(75))
BEGIN
DECLARE iSalt BIGINT;
SELECT UUID_SHORT() INTO iSalt;
INSERT INTO UserCustomer
(
UserId,
Password,
Salt
)
VALUES
(
iUserId,
SHA2(CONCAT(iSalt,sPassword), 256),
iSalt
);
END
BIGINT and BIGINT UNSIGNED are not the same. All the integer data types are signed unless explicitly unsigned.
But also, UUID_SHORT() is designed to produce unique but not random, not unpredictable, and always-incrementing values, which seems like a particularly bad choice for a salt, no?
...Especially since the RANDOM_BYTES() function was introduced in 5.6.17.
http://dev.mysql.com/doc/refman/5.6/en/encryption-functions.html#function_random-bytes
I am getting the following error message.
Cannot resolve the collation conflict between "Latin1_General_CI_AI" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
I only get it when I place this code below in my WHERE clause.
WHERE Region IN (SELECT Token FROM dbo.getParmsFromString(#Region))
Now #Region contains all the values from my multi-select fields from SSRS.
Below is the code for the function that is used.
CREATE FUNCTION [dbo].[getParmsFromString]
(#String VARCHAR(MAX))
RETURNS #Parms TABLE
(
Token VARCHAR(MAX)
)
AS
BEGIN
IF CHARINDEX(',', #String) != 0
BEGIN
;WITH cte0(Token, List) AS
(
SELECT SUBSTRING(#String, 1, CHARINDEX(',',#String,1) - 1)
,SUBSTRING(#String,CHARINDEX(',',#String,1) + 1, LEN(#String)) + ','
UNION ALL
SELECT SUBSTRING(List,1,ISNULL(CHARINDEX(',',List,1) - 1,1))
,SUBSTRING(List,CHARINDEX(',',List,1) + 1, LEN(List))
FROM cte0
WHERE LEN(cte0.List) > 0
)
INSERT INTO #Parms (Token)
SELECT Token
FROM cte0
OPTION (MAXRECURSION 0)
RETURN;
END
ELSE
INSERT INTO #Parms
SELECT #String
RETURN;
END
Try changing
RETURNS #Parms TABLE
(
Token VARCHAR(MAX)
)
with
try changing RETURNS #Parms TABLE
(
Token VARCHAR(MAX) COLLATE DATABASE_DEFAULT
)
and
WHERE Region IN (SELECT Token FROM dbo.getParmsFromString(#Region))
with
WHERE Region COLLATE DATABASE_DEFAULT IN (SELECT Token FROM dbo.getParmsFromString(#Region))
Generally this type of error occurs when you try to compare the the data of different regions or when you compare data using a specific encryption with other data using a different encryption.
The most probable reason is that their tempdb is using the collation "SQL_Latin1_General_CP1_CI_AS" while the database is using "Latin1_General_CI_AS". As a result, temp objects are created under the collation "SQL_Latin1_General_CP1_CI_AS" and then fail to compare with database objects of the database which are using the collation "Latin1_General_CI_AS".
The easiest fix and also the one which would recommended would be to run the database on a server which was installed using collation "Latin1_General_CI_AS".
FYI. SQL collations("SQL_Latin1_General_CP1_CI_AS") are present in sql server for backward compatibility. When dealing with international data or databases using unicode and non unicode data, it is recommended to use windows collations ("Latin1_General_CI_AS").
You can change your database collation by:
use master
ALTER DATABASE "Your database"
COLLATE Latin1_General_CI_AS;
SELECT name, collation_name
FROM sys.databases;
and if needed you can also change the collation of "master" database i.e. rebuilding the database, for this go through to these links:
http://msdn.microsoft.com/en-us/library/dd207003(v=sql.100).aspx
http://sqlbuzz.wordpress.com/2011/08/20/how-to-rebuild-master-database-aka-rebuilding-sql-server-2008r2/
but make sure you backup all your database before doing this.
An insert statement with a large number of values returns 'Error converting data type varchar to numeric.'
How can I find which value actually triggers the error?
MS SQL Server 2008 is used.
you can use the function ISNUMERIC()
select * from table
where isnumeric(column) = 0
or
declare #v varchar(100)
select #v = 'abd'
select ISNUMERIC(#v)
Also take a look at this IsNumeric, IsInt, IsNumber post to help you further with these kind of problems