Update table using Openquery linked server - sql

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'

Related

Incorrect syntax near the keyword 'EXEC'- OPENRPWSET

I am trying to load data into SQL server table. here is my code;
from OPENROWSET('SQLOLEDB', 'Server=(local);Trusted_Connection=yes;',
'EXEC [dbo].[sp_model1] #model = ''predict1''',
'EXEC [dbo].[sp_model2] #model1 = ''predict2''',
#q =''SELECT * FROM [tcs].[dbo].[fact_FY2019_FY2021]
where
[date] >= ''''2021-02-03 00:00:00''''
and id = 11 order by key;'''
)as a
GO
Every time i run my insert statement along with above code, i get the following error
OLE DB provider "SQLNCLI11" for linked server "(null)" returned message "Deferred prepare could not be completed.".
Incorrect syntax near the keyword 'EXEC'.
Not sure why am i getting this error.
Any help would be appreciated.

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

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'))

Invalid SQL statement; error when using IF EXISTS() in SQL statement, OLEDB MS ACCESS

I'm trying to build an SQL statement that will insert or update a row in the database depending on whether it exists in the table already. This format seems to work when I interface to an MSSQL server, but does not work when I use an OLEDB connection to MS Access.
IF EXISTS(select * from Resistors where ID = 2816)
update Resistors set
[Part Number]='1234'
where ID = 2816
else
insert into Resistors (
[Part Number]
)
values(
'1234'
)
;
I have validated these two sql commands using the OLEDB connection to MS Access. They work properly but now with the IF EXISTS() portion of the command.
update Resistors set
[Part Number]='1234'
where ID = 2816
insert into Resistors (
[Part Number]
)
values(
'1234'
)
The IF EXISTS() statement appears to match other documentation I have seen on the internet but I must be doing something wrong.
This is the error I receive when I run the command.
Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
This is my connection string
connection.ConnectionString = "Provider = Microsoft Office 12.0 Access Database Engine OLE DB Provider; Data source = " + _database_path;
Is the exists command not supported by this connection interface? Is there a version that supports the exists command?
Unfortunately, ifexists() is not supported in MS-Access sql.
you can use select count() as a work around, if returns 0 then it is not exist.
...
string cmdstr = "SELECT COUNT(*) FROM Resistors where ID = 2816";
....
int count = (int)command.ExecuteScalar();
....
if (count=0)
....
I hope this may help

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>);

Handling Dates from Oracle to SQL Server 2005 using OPENQUERY

I have linked SQL Server 2005 to an Oracle Server, and am using OpenQuery() to pass data from Oracle to SQL Server.
So, for instance, the following query works fine:
SELECT *
FROM OPENQUERY(ORACLE_LINK, 'SELECT FIELD1
FROM ORACLE.TABLE1');
Everything works, unless I ask for a field which is a date/time in the Oracle database. For instance, say that TABLE1 has ten fields, one of which is a timestamp. If I ask for all nine fields except for the timestamp, it works. If I ask:
SELECT *
FROM OPENQUERY(ORACLE_LINK, 'SELECT *
FROM ORACLE.TABLE1');
I get the following error:
OLE DB provider "MSDAORA" for linked server "ORACLE_LINK" returned message "Oracle error occurred, but error message could not be retrieved from Oracle.".
OLE DB provider "MSDAORA" for linked server "ORACLE_LINK" returned message "Data type is not supported.".
I also get the same error if I ask for only the timestamp field.
What is the best way of handling this error? Thanks.
I do it with this function (partial code):
select #StringOut = '{ts ''''' + convert(varchar(20), #DateIn, 20) + ''''' }';
RETURN #StringOut
and this goes into the query:
and procedure_date_dt = ' + dbo.TimestampString(#date) + '
and event_type = ''''Time Notation''''
and it works when I run it.