Sunday, February 19, 2012

Inserting records into Postgres from SQL server 2k Using Openquery

Hello,
We have a set of tables in a MS SQL Server 2000, These tables are
updated via
PDA from external location using a sycronisation from a Win CE MS SQL
DB.
This is all fine, I have to create a trigger that will insert the
information from
SQL Server to Postgres. I done a bit of reading and found the OPENQUERY
command and have been able to read from the Postgres DB but as yet have
been unable to write insert records into postgres using this. I am
using only
varchar and int4 fields which from what i have read should be fine.
Does anyone have a example of a INSERT INTO over a linked server that i
can
use as a base plus any information of pitfalls that might be usefull to
know about.
I have included my query below along with the error message i have
received.
I have been tinkering for most of a day with this and this is that last
error i received.
--Query
SET QUOTED_IDENTIFIER OFF
SET XACT_ABORT ON
declare @.var varchar(20)
declare @.var1 varchar(20)
declare @.var2 varchar(20)
declare @.var3 varchar(20)
declare @.var4 int
declare @.var5 int
declare @.cmd varchar(4000)
set @.var = '066'
set @.var1 = '5'
set @.var2 = '081'
set @.var3 = 'PKJ8889999H'
set @.var4 = 100
set @.var5 = 10
SELECT @.cmd = "INSERT INTO openquery(MARGE,'select customer_id,
location_id, product_code, max_level, min_level from
MARGE.consign.dbo.cs_static WHERE 1=0')
VALUES ('" + @.var1 + "','" + @.var2 + "','" + @.var3 + "','100','10')"
exec (@.cmd)
commit tran
--Error
Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider 'MSDASQL' reported an error. The provider did not give
any information about the error.
OLE DB error trace [Non-interface error: OLE DB provider MSDASQL
returned an incorrect value for DBPROP_CATALOGLOCATION which should be
either DBPROPVAL_CL_END or DBPROPVAL_CL_START].
OLE DB error trace [OLE/DB Provider 'MSDASQL'
IColumnsInfo::GetColumnsInfo returned 0x80004005: The provider did not
give any information about the error.].I know its bad form to reply to your own post but after a day and a
half i've
sorted it out. Below are my working queries in case anyone has similar
issues *shrug* works for me and standard disclaimer applys :)
thanks
David Phillips
SET QUOTED_IDENTIFIER OFF
SET XACT_ABORT ON
declare @.var1 varchar(20)
declare @.var2 varchar(20)
declare @.var3 varchar(20)
declare @.var4 varchar(20)
declare @.var5 varchar(20)
declare @.cmd varchar(4000)
set @.var1 = '68922'
set @.var2 = '122'
set @.var3 = 'KHG8889900H'
set @.var4 = '5000'
set @.var5 = '1500'
SELECT @.cmd = "INSERT INTO openquery(MARGE,'select customer_id,
location_id, product_code, max_level, min_level from consign.cs_static
WHERE 1=0')
VALUES ('" + @.var1 + "','" + @.var2 + "','" + @.var3 + "','" + @.var4 +
"','" + @.var5 + "')"
exec (@.cmd)
SELECT * FROM OPENQUERY (MARGE, 'select * from consign.cs_static')
delete from openquery(MARGE, 'select customer_id, location_id,
product_code, max_level, min_level from consign.cs_static')where
location_id = 122

No comments:

Post a Comment