First of all, thank you beforehand for helping me with this. Please do not state that this is a repeat question as I have searched a lot but still none of the threads I found here relate to my query. Actually I have a simple query which I'm failing to pass through and needed some help. My issue is as follows,
I want to run a simple SQL query to insert some data into a table i.e.,
INSERT INTO "public"."plan" (id,name,description) VALUES (6,"Plan Name","Plan Description");
But instead of passing Plan Name and Plan Description as text, I'm looking to define variables and pass those instead, in short something like this,
INSERT INTO "public"."plan" (id,name,description) VALUES (6,customPlanName,customPlanDescription);
I've tried using the following but this doesn't work,
DECLARE
planname TEXT;
plandesc TEXT;
SET planname = 'MidasName';
SET plandesc = 'PlanDescription';
INSERT INTO "public"."plan" (id,name,description) VALUES (6,planname,plandesc);
Can you please help me out with this? I want something to be run using PostgreSQL on PgAdmin III
Thank you in advance for any help provided.
example with prepared statements:
prepare plan_insert (text,text)
as INSERT INTO "public"."plan" (id,name,description) VALUES (6,$1,$2);
execute plan_insert ('MidasName','PlanDescription');
execute plan_insert ('Some Other','Some more');
Related
I have a PostgreSQL function which has a ref cursor as output parameter;
I know i can get output using below set of commands
begin;
select uspfillactions();
fetch All in "<unnamed portal 1>";
rollback;
But just want to know is there any way to get function output using single query?
Any dynamic query solution or anything else?
I don't want to mention Begin and rollback/commit also.
I'm studying the PREPARE and EXECUTE commands to optimize my functions in PostgreSQL, but i have some problems.
I have the following PREPARE command:
PREPARE my_query(int) AS SELECT id FROM table1 WHERE id = $1;
So, I need store this result (ID) into a variable, like this:
EXECUTE my_query(10) INTO var_1;
But I got a syntax error. What is the correct way to do this?
Your question doesn't really make any sense to me.
If you are trying to execute the prepared statement from within PL/PgSQL than you're out of luck. The EXECUTE command is reserved for a different purpose within PL/PgSQL. Not that it would have much use, PL/PgSQL plans are prepared and cached by default anyhow :)
If you are trying to do this outside of PL/PgSQL than the INTO would mean you are trying to write the result to a new table. Something you are unlikely to do/need that often.
I want to save a set of queries (multiple SQL updates) as a single element that I can execute using pgAdmin3 (PostgreSQL 9.1).
I know that I can save single SELECTS as views but how about multiple UPDATE queries?
Example:
BEGIN;
UPDATE ...;
UPDATE ...;
COMMIT;
Update: What I looking for is a step-by-step guide of adding a stored procedure using the GUI, not running a SQL query that creates it.
So far, I encountered two problems with "New function...": the return type is required and found that NULL is not acceptable, so tried integer. Also, set the type to SQL but I don't know what exactly to write inside the SQL tab, whatever I try the OK button is still disabled and the statusbar says: Please enter function source code.
Do you know or did you try stored procedure (well, stored-procedure-like functions) ?
http://www.postgresql.org/docs/9.1/interactive/plpgsql-structure.html
To call it
select <name of function>(<param1>, <param2>) as result;
Here is the missing guide for a basic SQL stored procedure, one that does return 1.
right click on Functions and choose New Function...
complete name as my_procedure, return type as integer, language as sql
select Definition tab and write SELECT 1;
done
It would be nice to know if you can create queries that are returning nothing.
Currently my development environment is using SQL server express 2008 r2 and VS2010 for my project development.
My question is like this by providing a scenario:
Development goal:
I develop window services something like data mining or data warehousing using .net C#.
That meant I have a two or more database involved.
my senario is like this:
I have a database with a table call SQL_Stored inside provided with a coloum name QueryToExec.
I first idea that get on my mind is written a stored procedure and i tried to came out a stored procedure name Extract_Sources with two parameter passed in thats ID and TableName.
My first step is to select out the sql need to be execute from table SQL_Stored. I tried to get the SQL by using a simple select statement such as:
Select Download_Sql As Query From SQL_Stored
Where ID=#ID AND TableName=#TableName
Is that possible to get the result or is there another way to do so?
My Second step is to excecute the Sql that i get from SQL_Stored Table.Is possible to
to execute the query that select on the following process of this particular stored proc?
Need to create a variable to store the sql ?
Thank you,Appreciate for you all help.Please don't hesitate to voice out my error or mistake because I can learn from it. Thank you.
PS_1:I am sorry for my poor English.
PS_2:I am new to stored procedure.
LiangCk
Try this:
DECLARE #download_sql VARCHAR(MAX)
Select
#download_sql = Download_Sql
From
SQL_Stored
Where
AreaID = #AreaID
AND TableName = #TableName
EXEC (#download_sql)
is it possible to refer to a column/table name by using a string? Something like SELECT * FROM 'my_table'::table_name_t?
The reason I'm asking: I have a table geometry_columns with some geometry tables. And I would like to know which objects are within a certain radius.
Thanks, Philip
You will need a (stored) function to achieve this. The function takes the table name as an argument, creates the SQL dynamically and then returns the result of the SELECT based on that query.
here are some examples (not exactly what you need, but they should get you headed in the right direction):
http://forums.devshed.com/postgresql-help-21/plpgsql-variable-representing-table-name-137201.html
Dynamic column in SELECT statement postgres
I don't think you can do that directly. I think you would have to build the select statement from another statement or piece of code, then execute the resulting statement.