Error when using openrowset for particular record - sql

INSERT INTO OPENROWSET('MSDASQL', 'Driver=PostgreSQL Unicode;uid=postgres;Server=localhost;port=5432;database=data;pwd=xxx',
'select SanctionId,SchemeType,SchemeCode,CorrigendumStatus,AttendumStatus,yearofPlan,ReceivedDate from tesing WHERE SanctionId = ''-1'' ')
select SanctionId,SchemeType,SchemeCode,CorrigendumStatus,AttendumStatus,yearofPlan,ReceivedDate from testing where SanctionId=1103
While executing the above query, I am getting following error:
Msg 8152, Level 16, State 10, Line 1
String or binary data would be truncated.
The statement has been terminated.
Can anyone help me to resolve this?

You will have to check the source data against the target column definitions.
This happens when you try to insert, say, 100 characters into a varchar(50) column

Related

Adding columns and data to table

I am trying to run the following queries. When I run them separately the code executes correctly. When I run them together I get the error message:
Msg 207, Level 16, State 1, Line 11
Invalid column name 'TotOP'.
Msg 207, Level 16, State 1, Line 12
Invalid column name 'TotPK'.
It's as if the first query has not run at all? - Any ideas
As a work around I know that I can generate all of the columns at the beginning and only partially populate the table leaving these final two columns empty until I run the second query however I am curious to know why these statements can be run separately but not together.
Query 1:
ALTER TABLE [fcgen].[RPD1yeardata]
ADD [TotOP] DECIMAL (7,3) NULL,
[TotPK] DECIMAL (7,3) NULL;
Query 2:
UPDATE [fcgen].[RPD1yeardata]
SET [TotOP] = [1]+[2]+[3]+[4]+[5]+[6]+[7]+[8]+[9]+[10]+[11]+[12]+[13]+[14]+[39]+[40]+[41]+[42]+[43]+[44]+[45]+[46]+[47]+[48],
[TotPK] = [15]+[16]+[17]+[18]+[19]+[20]+[21]+[22]+[23]+[24]+[25]+[26]+[27]+[28]+[29]+[30]+[31]+[32]+[33]+[34]+[35]+[36]+[37]+[38]
FROM [fcgen].[RPD1yeardata]
just use GO after 1s query
ALTER TABLE [fcgen].[RPD1yeardata]
ADD [TotOP] DECIMAL (7,3) NULL,
[TotPK] DECIMAL (7,3) NULL;
Go
--Query 2:
UPDATE [fcgen].[RPD1yeardata]
SET [TotOP] = [1]+[2]+[3]+[4]+[5]+[6]+[7]+[8]+[9]+[10]+[11]+[12]+[13]+[14]+[39]+[40]+[41]+[42]+[43]+[44]+[45]+[46]+[47]+[48],
[TotPK] = [15]+[16]+[17]+[18]+[19]+[20]+[21]+[22]+[23]+[24]+[25]+[26]+[27]+[28]+[29]+[30]+[31]+[32]+[33]+[34]+[35]+[36]+[37]+[38]
FROM [fcgen].[RPD1yeardata];

IN operator with SELECT clause fails

The resultant error is:
Msg 207, Level 16, State 1, Line 9
Invalid column name 'Email'.
Code:
-- Bulk insert data from csv file into server temp table
BULK INSERT vwTemporaryIT_USE_ONLY_Import FROM 'C:\Bulk\b_email.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
Go
-- Set the flag in db for all records imported from csv
UPDATE [APTIFY].[dbo].[Person]
SET
[IT_Use_Only] = 1
WHERE
[Email] IN
(Select [Email] From vwTemporaryIT_USE_ONLY_Import)
Go
I can see that the vwTemporaryIT_USE_ONLY_Import table is being populated with the data from the CSV fine, but is seems the following statement is failing for some reason:
WHERE
[Email] IN
(Select [Email] From vwTemporaryIT_USE_ONLY_Import)
I am certainly not an expert at this and I may not have setup the table or view correctly, as I recently added the Email column to both. But they have matching datatype of nvchar(100) not null. I have also tried it as null. I'm not even sure if IN handles nvchar such is the level of my SQL expertise. Any clues what I'm doing wrong?
Actually no! In the Person table it was called Email1. I have changed the code to:
WHERE [Email1]
IN
(Select [Email] From wTemporaryIT_USE_ONLY_Import)
...and now it works fine. Thanks for your help!!

SQL import/create columns in SMILE chemical structures

We try to import a csv file where the first column includes chemical structures (SMILE) like this
c1cccc(c12)n(C)c(c2)CN(C)C(=O)c(c3)ccc(c34)NCC(=O)N(C4)C,14-BENZODIAZEPINEDERIV.4_145,1
c1cccc(c12)n(C)c(c2)CN(C)C(=O)c(c3)ccc(c34)N[C#H](C(=O)N(C4)C)CC(=O)OC,14-BENZODIAZEPINEDERIV.3_146,1
Here is the code in SQL
--Define Table
CREATE TABLE Amide_actives_test
(Structure VARCHAR(40),
Name VARCHAR(40),
Active INT)
GO
--Import Data from CSV
BULK
INSERT Amide_actives_test
FROM 'C:\Amide_actives.csv'
WITH
(
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n' --Use to shift the control to next row
)
GO
--Check the content of the table
SELECT * FROM Amide_actives_test
GO
The following error message will pop out:
Bulk load data conversion error (truncation) for row 1, column 1 (Name).
Msg 4863, Level 16, State 1, Line 10
...repeating the previous 2 lines 10 times....
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 10
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Apparently there is a problem of SQL to read the first column in "Structure VARCHAR(40)". I have tried all the string types (CHAR,VARCHAR.NCHAR,NVARCHAR,NTEXT,TEXT) and none of them works.
http://msdn.microsoft.com/en-us/library/ff848814.aspx
There is one way to solve this issue is to purchase another customized MySQL module from DayLight. However, 1. it costs 2. it doesn't support SQL
http://www.daylight.com/dayhtml/doc/pgsql/daycart_pg_search.html
May I know if any SQL guru has SQL solutions? Thanks!
First problem is Structure VARCHAR(40) varchar length is lesser than the input so you got trucation error. Try increasing the varchar length and check

Setting a varchar column length with a variable or a subquery

I have a table that is created by an import of an Excel file. As is the case, all text fields are imported as nvarchar(255). I'd rather do the bulk import then manipulate the table afterwards. I know SSIS allows me to set data types and sizes via data mapping, but that doesn't seem to work consistently for me. Maybe I'm not holding my mouth right...
Anyway, I want to change the varchar length definitions to the max length of the data in each column. Rather than run a statement to check the max length as a literal...
select max(len(rtrim(FIELD))) from TABLE$
...I want to do it in code. So I hit on this idea:
declare #Var int
set #Var = (select max(len(rtrim(FIELD))) from TABLE$)
alter table dbo.TABLE$ alter column FIELD varchar(#Var)
Lines one and two work fine, but it gives me an error when executing the third line:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '#Var'.
So I tried this, thinking it would be a more compact solution...
alter table dbo.TABLE$
alter column FIELD varchar(select max(len(rtrim(FIELD))) from TABLE$)
...but it wasn't. I got these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
So my question is kind of a two-parter. First, why won't these methods work, and second, what would work--short of finding the mas length of each column, then setting the varchar length with a literal?
Thanks in advance.
Dynamic SQL would work for what you're trying to do. Whether it's the right tool for the job or the right thing to do is another story. Give this a shot:
DECLARE #cmd NVARCHAR(4000)
SET #cmd = 'ALTER TABLE dbo.Table$ ALTER COLUMN FIELD VARCHAR(' + CAST((SELECT MAX(LEN(RTRIM(FIELD))) FROM dbo.TABLE$) AS NVARCHAR(10)) + ')'
EXEC(#cmd)
That query parses but I didn't try to run it. Be careful using Dynamic SQL though. It can get you into trouble if you start using it everywhere. Further reading:
http://sqlmag.com/database-performance-tuning/don-t-fear-dynamic-sql
http://www.sommarskog.se/dynamic_sql.html

Msg 102, Level 15, State 1, Line 2 Incorrect syntax near ',' trying to INSERT INTO

i've succesfully created db and tables, but when i try populate one of a table, like this
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0),
(0,'productName2',0),
(1,'productName3',9),
(1,'productName4',7),
(1,'productName5',3),
(1,'productName6',10),
(0,'productName7',0),
(1,'productName8',6),
(1,'productName9',12),
(1,'productName10',20);
GO
i got an error:
Msg 102, Level 15, State 1,
Line 2 Incorrect syntax near ','.
firstly which ',' is meant, secondly - what is wrong?
PS: i use MS Management Studio v 9.0 if it is needed...
Versions of SQL Server 2005 and below do not support the multiple VALUE clause syntax
SQL Server 2005 is version 9...
See How do I insert multiple rows WITHOUT repeating the "INSERT INTO dbo.Blah" part of the statement? for more
if you are using SQL SERVER 2005 and below, the query wouldn't work because it doesn't support multiple value clause insert statement. You should insert it one by one.
Like this one below,
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0)
GO
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName2',0)
GO