Using 4-part naming to do insert to DB2 from SQL Server 2005? - sql-server-2005

We're using DB2 9.1 on Linux and SQL Server 2005, and the IBM db2 odbc driver. We have a linked server set up to DB2. From SQL Server, I can do the following:
-- I can create the new table fine
exec ('create table dev.TestSylviaB (field1 int) in TS_DEV_USER_XXXX')
at LinkDB2
-- I can use 4 part naming to select from it, as long as I skip the second part and put everything in upper case
select * from LINKDB2..DEV.TESTSYLVIAB
-- I can insert using Exec (which does pass through)
exec ('insert into DEV.TestSylviaB (field1 ) values (1)') at LinkDB2
-- HOWEVER I cannot insert via the standard 4 part naming, like this:
insert into LINKDB2..DEV.TESTSYLVIAB values (1)
I get a "driver not capable" error message:
OLE DB provider "MSDASQL" for linked server "LINKDB2" returned message "[IBM][CLI Driver] CLI0150E Driver not capable. SQLSTATE=S1C00".
Msg 7343, Level 16, State 2, Line 1
The OLE DB provider "MSDASQL" for linked server "LINKDB2" could not INSERT INTO table "[LINKDB2]..[DEV].[TESTSYLVIAB]".
Any thoughts?
thanks!
Sylvia

The 4 part name insert is a distributed transaction and the DB2 driver needs to enroll into it. See Registering the IBM DB2 Driver for ODBC and CLI with the Microsoft DTC.

Related

Passing multiple parameters to variables of sql command in linked server

Here is a working example of how to pass parameters to a SQL statement on a linked server
EXEC ('SELECT ?, ?', 1, 2) AT [CDB]
But this method no works
EXEC ('
SET #A = ?;
SET #B = ?;
SELECT #A, #B', 1, 2) AT [LINKEDSERVER]
and generate error (the linked server is MySQL, the command is runnig in MS SQL as local server):
Msg 7215, Level 17, State 1, Line 7
Could not execute statement on remote server 'LINKEDSERVER'.
Sorry, I cannot attach the text of the message here, as it is in Czech (something like OLE DB error check status values).
However, I necessarily need to pass more parameters to variables without side effect Select.
So how do you do it?
But I don't want to use sp_executesql. In addition, I need to save the result of the execution on the linked server in a temporary table on the local server.
INSERT #Temptable (columns, ...)
EXEC (' more complex query with UNION ', params, ...) AT [LINKEDSERVER]
Jaroslav

How to Select Table SQL To Oracle

I want to select data from SQL Server to Oracle (Toad Apps).
If from Oracle to SQL Server its done like this
Insert Into M_CLASSIFICATION_ORACLE
SELECT * From OPENQUERY ([B1APPS], 'select * from V_Classification_Asset' ) AS derivedtbl_1
How about in Oracle (Toad apps)?
You can able to do it by using CREATE DATABASE LINK:
Create a Database Link to connect to SQL Server:
CREATE DATABASE LINK link-name ...
Query SQL Server using the Database Link:
SELECT * FROM sqlserver-table-name#link-name;
Documentation:
https://www.oracletutorial.com/oracle-administration/oracle-create-database-link/
Calling Stored Procedure using Database Link:
https://dba.stackexchange.com/questions/1856/running-a-stored-procedure-across-db-link

Python pyODBC : Issue inserting into a sql server table with an identity column

An INSERT statement that was created with Python gives me an error when I execute and commit it. I have taken a copy of the statement from Python and run it myself in SQL SERVER and it works fine there. The table I am trying to insert into has an identity column. When Python trys to execute it will give me an error saying what is below when I exclude the identity column in the statement
Table looks like this MY_TABLE ( ID INT IDENTITY(1,1) NOT NULL, A INT, B INT)
INSERT INTO MY_TABLE (A, B) VALUES(VALUE_A, VALUE_B);
"('23000', "[23000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot insert the value NULL into column 'MY_IDENTITY_COLUMN', table 'MY_TABLE'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW); [23000] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been terminated. (3621)")"
But when I try to include the value for the Identity column (I don't want to do this) I get the following error which makes sense as it's an identity column that we let the table auto-increment
"Cannot insert explicit value for identity column in table 'MY_TABLE' when IDENTITY_INSERT is set to OFF."
When I run the query in SQL SERVER the table fills the value for the Identity Column itself and auto-increments but for some reason when I run the statement in Python it does not do this and tries to pass a NULL
SQL SERVER version: 10.50.6560.0
Recognising it's a little late but... I had the same problem recently using some old program and ODBC. The solution was to create a View in SQL Server with only the columns required (i.e. A and B in your case) and then insert into that View.
A little hard to tell without your code, but here is an example.
In database create a test table
CREATE TABLE dbo.test(
ID INT IDENTITY NOT NULL PRIMARY KEY,
Name VARCHAR(40) NOT NULL
);
Then in Python specify the columns you are inserting into
import pyodbc
warecn = pyodbc.connect("Your Connection Stuff")
Inscursor = warecn.cursor()
Inscursor.execute("Insert into dbo.test(name) values ('This'), ('is'), ('a'), ('test')")
Inscursor.commit()
Inscursor.close()
del Inscursor
warecn.close()

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

How to INSERT INTO SQLite with SELECT FROM SQL Server

I want to SELECT a table in a SQL Server database and then INSERT it into a local SQLite database.
IS it possible to do this entirely with a query in the vein of:
INSERT INTO table1 ( column1 )
SELECT col1
FROM table2
but passing connection information?
Setup a local odbc data source, DSN, for SQLite file. Call it SQLite_DataSource
Then setup a linked server in SSMS:
USE [master]
GO
EXEC sp_addlinkedserver
#server = 'SQLite', -- the name you give the server in SSMS
#srvproduct = '', -- Can be blank but not NULL
#provider = 'MSDASQL',
#datasrc = 'SQLite_DataSource'
GO
Then you can use it like any other db:
INSERT into SQLite_DataSource (column1)
SELECT col1
FROM table2
Lookup SQL Server's documentation for sp_addlinkedserver for more details.
You need to create a SQL Server Linked Server to SQLite, take a look at this post for more information .