Linked server errror: Cannot get the current row value of column - sql

I have this query in SQL SERVER:
select *
from ADRESSEN
where Transferred is NOT null
and NR_ID not in (Select NR_ID from openquery(PROD, 'Select * from TABELLEN.NUMMERN'))
When I execute this query I get this error:
OLE DB provider "OraOLEDB.Oracle" for linked server "PROD" returned message "".
Msg 7341, Level 16, State 2, Line 12
Cannot get the current row value of column "[OraOLEDB.Oracle].NR_ID" from OLE DB provider "OraOLEDB.Oracle" for linked server "PROD".
But when I comment one of the condition in WHERE and I execute it, the query works:
select *
from ADRESSEN
where Transferred is NOT null
It works! And this too:
select *
from ADRESSEN
where
NR_ID not in (Select NR_ID from openquery(PROD, 'Select * from TABELLEN.NUMMERN'))

Related

Insert return records from stored procedure - SQL

The script below return a select records:
EXEC [LINK_SERV].[DB_SAMPLE].[dbo].[SP_SAMPLE] '1235123'
The I want to insert the return records to a temp table so I wrote the script below (assumed temp table is already created):
INSERT INTO #TempTable
EXEC [LINK_SERV].[DB_SAMPLE].[dbo].[SP_SAMPLE] '1235123'
But I get this error:
OLE DB provider "SQLNCLI11" for linked server "LINK_SERV" returned message "The partner transaction manager has disabled its support for remote/network transactions.
Msg 7391, Level 16, State 2, Line 25
The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "LINK_SERV" was unable to begin a distributed transaction.
Please advise the specific config to enable this. I tried the same code in some other server and it work. Thank you in advance.
This is where the beauty of OPENQUERY() comes into great use.
i.e. something like this...
INSERT INTO dbo.MyTable2
SELECT Col1, Col2, etc
FROM dbo.MyTable1
LEFT JOIN OPENQUERY(<<LINKEDSERVER>>,
'SELECT BLAHBLAH
FROM dbo.BLAHBLAH WHERE <something> = <something>);

Postgres CASE statement not working in SQL Server

I've written a query in postgres (on PGAdmin) and I'd like to put it in SSMS2012 for reporting purposes however it doesn't seem to allow the CASE statement, this is my query:
SELECT *
FROM OPENQUERY (POSTGRESQL,
'SELECT
Table1 AS Table_1,
CASE Table2
WHEN 75887 THEN ''1''
WHEN 75888 THEN ''2''
WHEN 75889 THEN ''3''
WHEN 75890 THEN ''4''
WHEN 75891 THEN ''5''
WHEN 75892 THEN ''6''
END AS Table_2,
DateTable1 AS DateTime
FROM SuperTable1 ')
And I'm getting the error:
OLE DB provider "MSDASQL" for linked server "POSTGRESQL" returned
message "Requested conversion is not supported.".
Msg 7341, Level 16, State 2, Line 1
Cannot get the current row value of column "[MSDASQL].table_2" from OLE DB provider "MSDASQL" for linked server "POSTGRESQL".
I've noticed if I remove the whole case statement it has no issues retreiving the data in the columns in the SELECT statement and the whole thing (including the CASE statement) works fine in PGAdmin.
Try simplify the query
Run 4 separated querys to determinate which one is giving you the problem.
Also try using 1 instead of ''1''
SELECT Table1 AS Table_1
SELECT DateTable1 AS DateTime
SELECT
CASE Table2
WHEN 75887 THEN 1
END AS Table_2
SELECT
CASE Table2
WHEN 75887 THEN ''1''
END AS Table_2

Using Local Variables for comparing data in the Excel being read in SQL Server

I am trying to read data from an Excel using the Following statement which is working absolutely fine
Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\input.xls','select MyColum from [Sheet1$]')
the XLS inpout.xls has two columns ID and NAME. Now I want the above statement to return only ONE value/row where ID = x and assign the returned value to a local variable as shown below -
DECLARE #dbValue varchar(20)
DECLARE #I int
SET #I = 1
Select #dbValue = (Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\input.xls','select MyColum from [Sheet1$] where [ID]=#I'))
this returns an error
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)" returned message "No value given for one or more required parameters.".
Msg 7320, Level 16, State 2, Line 3
Cannot execute the query "select MyColum from [Sheet1$] where [ID]=#I" against OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)".
Please suggest.
Maybe something like this will work. But your question is confusing, since your query on the Excel file selects MyColumn, but you say that file has columns ID and NAME.
DECLARE #dbValue varchar(20)
DECLARE #I int
SET #I = 1
Select #dbValue = NAME FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=d:\input.xls','select ID from [Sheet1$]') as T
WHERE ID=#I;

How to use OpenRowSet to insert data into a blank file?

How to use OpenRowSet to insert data into a blank file?
I need to insert into a txt file (say to D:\TDB) some select output (say select * from sys.tables) from the database
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;HDR=Yes;', 'SELECT * FROM sys.tables.txt')
select * from sys.tables;
I get
OLE DB provider "MICROSOFT.JET.OLEDB.4.0" for linked server "(null)"
returned message "The Microsoft Jet database engine could not find the
object 'sys.tables.txt'. Make sure the object exists and that you spell its
name and the path name correctly.".
Msg 7350, Level 16, State 2, Line
1 Cannot get the column information from OLE DB provider
"MICROSOFT.JET.OLEDB.4.0" for linked server "(null)".
What is wrong?
PS. please do not propose the bcp solution, cause already tested and does not work everytime, so I would test openrowset now..
#serhio , I tested your sql below:
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;HDR=Yes;', 'SELECT * FROM sys.tables.txt')
select * from sys.tables;
I got a few test results
The filename should not include "." in it.
(sys.tables.txt→systables.txt)
HDR(Header Row) can not be used here.(Remove it)
The txt file must exist.(Create it)
The first line in the txt file should be the all column name of your
source data.
sql
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=D:\TDB;', 'SELECT * FROM systables.txt')
select * from sys.tables;
systables.txt
name,object_id,principal_id,schema_id,parent_object_id,type,type_desc,create_date,modify_date,is_ms_shipped,is_published,is_schema_published,lob_data_space_id,filestream_data_space_id,max_column_id_used,lock_on_bulk_load,uses_ansi_nulls,is_replicated,has_replication_filter,is_merge_published,is_sync_tran_subscribed,has_unchecked_assembly_data,text_in_row_limit,large_value_types_out_of_row,is_tracked_by_cdc,lock_escalation,lock_escalation_desc

Update table using Openquery linked server

I've tried this code and still got the following error, perhaps anyone could help?
UPDATE a
SET a.MMDWNO = '21'
FROM OPENQUERY(NMIIFLIB,
'select * from MVXCDTANSN.MITMAS WHERE MMITTY = ''25''') a
Error :
OLE DB provider "MSDASQL" for linked server "NMIIFLIB" returned
message "[IBM][iSeries Access ODBC Driver][DB2 UDB]SQL7008 - MITMAS in
MVXCDTANSN not valid for operation.".
Msg 7343, Level 16, State 4, Line 1
The OLE DB provider "MSDASQL" for linked server "NMIIFLIB"
could not UPDATE table "[MSDASQL]".
The select statement works fine but when I try to update I always stuck with this.
If you're trying to update a table on linked server, try this syntax:
UPDATE OPENQUERY(NMIIFLIB, 'select * from MVXCDTANSN.MITMAS where MMITTY = ''25''')
SET MMDWNO = 21
you must try this. Hope this will help you.
UPDATE OPENQUERY(firstlink, 'select * from job.dbo.student where id = ''3''')
SET name = 'sanjeev acharya'