473,666 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use dblink within pl/pgsql function:

What I'd like to do is use dblink to extract a few rows from a remote
database and manipulate these within a function in pl/pgsql. Something
like this:

CREATE OR REPLACE FUNCTION find() RETURNS INTEGER AS '
DECLARE
count INTEGER:
myrec RECORD;
BEGIN
FOR myrec IN SELECT * FROM DBLINK(''select x,y from mytab'') as
temp(x integer, y real) LOOP
count := count + 1;
END LOOP;
RETURN count;
END; ' LANGUAGE 'plpgsql';
But this syntax does not work, and I cannot find a form which does work.
Does anyone know how to do this?

Thanks in advance.

--
Clive Page
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 12 '05 #1
1 8841
Clive Page wrote:
What I'd like to do is use dblink to extract a few rows from a remote
database and manipulate these within a function in pl/pgsql. Something
like this:

CREATE OR REPLACE FUNCTION find() RETURNS INTEGER AS '
DECLARE
count INTEGER:
myrec RECORD;
BEGIN
FOR myrec IN SELECT * FROM DBLINK(''select x,y from mytab'') as
temp(x integer, y real) LOOP
count := count + 1;
END LOOP;
RETURN count;
END; ' LANGUAGE 'plpgsql';
But this syntax does not work, and I cannot find a form which does work.
Does anyone know how to do this?


You didn't show us the specific error you get, making it difficult to help.

Offhand I see two errors in your script above unrelated to the use of
dblink, and possibly one related to dblink. First, the line "count
INTEGER:" ends in a colon instead of the required semicolon. Second, if
you don't initialize "count" to something other than NULL, adding 1 to
it will still add null. Try this:

CREATE OR REPLACE FUNCTION find() RETURNS INTEGER AS '
DECLARE
count INTEGER := 0;
myrec RECORD;
BEGIN
FOR myrec IN SELECT * FROM DBLINK(''select x,y from mytab'') as
temp(x integer, y real) LOOP
count := count + 1;
END LOOP;
RETURN count;
END; ' LANGUAGE 'plpgsql';

The dblink issue is that you've used a form of dblink that requires you
to have previously set up a connection. If you call your find() function
without first establishing that connection, you'll get a "ERROR:
connection not available" or something similar. See the dblink docs.

Joe
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10309
by: Brian Maguire | last post by:
What's the best way to create a connection with dblink with persisted connections for web applications? For example: I want to have a view that links to a table in another pg database so I can mask the connection and make it easier to query. so I would do this ...
1
4169
by: Oleg Lebedev | last post by:
Is there a way to rollback a dblink transaction? Say, I delete some data from the remote database, but I don't want this to be visible untill the data is inserted in the current database. And if the insertion of data in the current database throws an error, I want to rollback the dblink transaction, which should restore data in its original remote location. Thanks. Oleg
2
2328
by: Mark Gibson | last post by:
Hello, I've been experimenting with dblink recently, and have encountered some limitations I'd like to discuss. I've been trying to create views of remote tables, like so: CREATE VIEW stuff AS SELECT * FROM dblink(' ... remote connection info ... ', 'SELECT id, title, title_idx FROM stuff')
2
7671
by: Sam Masiello | last post by:
Hello all! I am rather stuck and am hoping someone can help. I am working on a project where users need to be able to send one query which will hit databases on multiple hosts (the database structures are the same) and tie the data back together again. After doing lots of reading it looks like dblink might be a great solution for this. After trying to insert dblink.sql into the database, I just get a series of errors:
1
2698
by: Kreißl, Karsten | last post by:
Hello, we want use dblink to connect several databases in a client/server environment. Connection from local users to the remote databases should be possible only for privileged users. We tried a solution with dblink, embedding this command in a view, like this: create view inst as select * from dblink('host=pollux port=5432 dbname=cob_int user=his_int password=#integration#', ..... This solution is insecure, because login and password...
2
1619
by: Gellert, Andre | last post by:
Hello, I have following problem: A user "xy" shouldn't have any rights to a table, but needs data from the content of the table. My idea was to setup a PL/PGSQL procedure to fetch the data from the table, so that the user only is allowed to access the procedure. I also tried using a SQL function, but this doesn't work, too. Working with views may be a solution - or are e.g. cursors
2
2789
by: Jim Worke | last post by:
Hi list, My boss would like to convert from MySQL to PostgreSQL, but there are a few things that hold us back. We'd like to have the databases in several physical servers, with referential integrity between the tables. Reading from Oracle articles, their referential integrity can be done using their dblink and triggers. What I'd like to ask is whether contrib/dblink is stable? Seeing dblink in a contrib directory does bring a doubt...
0
1110
by: Google Mike | last post by:
I had RH9 Linux. It came with pgSQL, but I couldn't seem to figure out how to get PL/pgSQL going. I read the HTML documentation that came with it and was confused until I tried a few different variations and guessed about some things. I've finally got it working and here's what the SQL looks like to enable and test it: -- ON YOUR SYSTEM, LOOK FOR WHERE plpgsql.so FILE EXISTS AND SUBSTITUTE THAT -- PATH WITH THE ONE I HAVE BELOW.
1
6003
by: priyapatil | last post by:
hello everyone i want to use dblink functions to access a remote database bt i am not able to install those functions on my machine i am using postgresql 8.2 on windows XP machine can anyone tell me how to install those functions?? on internet i have found that you have to write a command like psql your_databse < dblink.sql
0
8445
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.