Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
I am very new to PostgreSQL and I figured I could benefit from the communities help on this one. I'm struggling to get this concept to work.
All it is supposed to do is insert data into dimension tables if the data does not already exist. Then returns the dimension ID for later use. Errors fall within a CASE statement because I also want to assign a boolean value to success by if an error was present or not. (EDIT: Here, when I say "error", I am referring to a field in the data).
The error I get states that time_stamp does not exist. I thought I read that INSERT INTO RETURNING INTO would implicitly create a temp table though. Seems I must have misunderstood though. Is anyone able to help me find the correct logic here? Thanks!
CREATE OR REPLACE FUNCTION distribute_process_data() RETURNS TRIGGER AS
$BODY$
BEGIN
select CURRENT_TIMESTAMP() into time_stamp;
INSERT INTO
sources(source, category)
VALUES(NEW.source, NEW.category) ON CONFLICT (source) DO NOTHING
RETURNING id INTO source_id;
INSERT INTO
mediums(sources_id, medium)
VALUES(source_id, NEW.medium) ON CONFLICT (sources_id, medium) DO NOTHING
RETURNING id INTO medium_id;
INSERT INTO
countries(country)
VALUES(NEW.country) ON CONFLICT (country) DO NOTHING
RETURNING id INTO country_id;
INSERT INTO
requests(sources_id, request)
VALUES(source_id, NEW.request) ON CONFLICT (request) DO NOTHING
RETURNING id INTO request_id;
CASE
WHEN NEW.error IS NOT NULL THEN
INSERT INTO
errors(timestamp, processes_id, error)
VALUES(time_stamp, NEW.id, error)
RETURNING id INTO error_id;
select FALSE INTO success;
ELSE
select NULL INTO error_id;
select TRUE INTO success;
END CASE;
INSERT INTO
processes(
id, timestamp, requests_id, errors_id, sources_id, mediums_id, countries_id, successful,
date, web_visits, conversions, ad_impressions, ad_clicks, ad_cost, site1_ad_visits,
site2_ad_visits, site1_ad_visits_bounce_rate, downloads_desktop, downloads_mobile,
initiated_registrations, completed_registrations, paid_users, revenue
)
VALUES(
NEW.id, time_stamp, request_id, error_id, medium_id, country_id, success,
NEW.date, NEW.web_visits, NEW.conversions, NEW.ad_impressions, NEW.ad_clicks, NEW.ad_cost,
NEW.site1_ad_visits, NEW.site2_ad_visits, NEW.site1_ad_visits_bounce_rate,
NEW.downloads_desktop, NEW.downloads_mobile, NEW.initiated_registrations, NEW.completed_registrations,
NEW.paid_users, NEW.revenue
)
RETURN new;
END;
$BODY$ language plpgsql;
I tried using the code provided. It did not work, stating that my "variables" do not exist.
I was expecting it to complicity create them.
It may be worth mentioning, I'd favoring readability over speed here, since I'm still a new user and will be in charge of maintaining the code.
There is no way of storing variables out of the trigger other than a table or Session variables that are to expensive and dangerous.
A temporary table in other hand is very handy and common usage of temporary files also .
Almost every time the storage cost is less expensive than the cache cost for larger servers that usually have big amount of engineering on top of whether or not a query should use the cache and when.
A example of it is dynamic SQL used for queries that doesn't get executed multiple times instead of static queries that uses the cache all the time, once the postgres cache limit is reached previous cached queries get reindexed and the database gets very slow,dynamic sql in other hand executes without cache, are slower and affordable to the right situations.
You could try some validations about the table structure using the information schema before
IF SELECT column_name FROM information_schema.columns WHERE table_name ='countries' AND table_schema='public' IS NULL THEN
EXECUTE 'ALTER TABLE countries ADD column'|| **column_name** ||' TIMESTAMP; '
ELSE
EXECUTE 'INSERT INTO countries ('||column_name||') VALUES( now() )';
END IF;
There is no other way out for manipulating results without error due to non-existing objects other than Dynamic SQL.
There is another way you can use Returning syntax to achieve easier, performant and dynamic using WITH, the code below is possible to interpolate the results avoiding declare unique variables, also binds the process to each other results :
WITH query1 AS (
SELECT 'val' AS var ),
query2 AS (
INSERT INTO test (name) SELECT query1.var FROM query1 RETURNING id
),
query3 AS (
DELETE FROM test WHERE id = (SELECT query2.id FROM query2 )
)
SELECT * FROM query1 ;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I would never expect that I would need help with such a banal command:
INSERT INTO test (id, field1) VALUES (1, "dfds");
And result:
column dfds does not exist
LINE 1: INSERT INTO test (id, field1) VALUES (1, "dfds");;
^
SQL state: 42703
Character: 41
This is 200% correct SQL. 'dfds' is not any 'column' - this is value.
How to insert row in PGAdmin (GUI)? I click on 'view data' icon and no way to insert row.
String are quoted with ':
INSERT INTO test (id, field1) VALUES (1, 'dfds');
" is for identifiers.
I would like to understand question about conversion:
exam dump I'm working with has this question at least three times with 3 different solutions you approve or don't approve of note that RegistrationNumber is defined as varchar(5) :
You run the following query:
SELECT UserId FROM tblVehicleRegistration
WHERE RegistrationNumber = 20012
AND RegistrationDate > '2016-01-01'
The query output window displays the following error message: “Conversion failed when converting the varchar value ‘AB012’ to data type int.”
You need to resolve the error.
Solution: You modify the Transact-SQL statement as follows:
SELECT UserId FROM tblVehicleRegistration
WHERE RegistrationNumber = '20012'
AND RegistrationDate > '2016-01-01'
answer says this does not work
I would think the test is incorrect. Here is a simplified example:
declare #tblVehicleRegistration table (RegistrationNumber varchar(5))
insert into #tblVehicleRegistration(RegistrationNumber) VALUES('AB012')
SELECT * FROM #tblVehicleRegistration WHERE RegistrationNumber = 20012 --Fails as expected
SELECT * FROM #tblVehicleRegistration WHERE RegistrationNumber = '20012' --works as expected
SQL Server will do a convertion in order to compare 'AB012' and 20012. If you check this link Data type precedence you will see that type varchar, wich is low precedence, needs to be converted to int, wich is high precedence, in order to make a comparison.
I created a table and tried hands-on. It worked properly after casting or changing the integer value to a string with quotation marks.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have one table called empl, and have another table new_empl with the same columns and definition with empl. Is it possible to insert records from v_record in new_empl using the following code ?
DECLARE
v_record empl%rowtype;
BEGIN
Insert into new_empl values v_record;
END;
There are too many columns in empl table and I want to avoid listing them.
The above snippet would work for Oracle, but for Postgres/pgplsql, the following snippet works like a charm:
DECLARE
v_record empl%rowtype;
BEGIN
Insert into new_empl values (v_record.*);
END;
Is it possible to insert records from v_record in new_empl using the
following code
Yes its possible however the way you are doing insert will not insert anything as nothing to assigned to the variable v_record . Rather you can do something like below:
DECLARE
v_record empl%rowtype;
BEGIN
Insert into new_empl Select * from empl;
END;
But why you want to do it in a PLSQL block when you can do it SQL itself.
Insert into new_empl select * from empl;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a temp table with 10 values like this:
CREATE TABLE #RequireAuth (val1 int, val2 int /*, etc...*/ )
How can I call another stored proc that will take in these 10 values and return me back 6 values?
SELECT * FROM #RequireAuth -- Not sure how to call a SP from here?
I then need to take those 6 values and update a different table.
I've made some assumptions about your temp table because what you describe and what you show are different, but heres the base concept for a while loop.
CREATE TABLE #RequireAuth (val1 int, done bit default 0)
declare #varible int
,#count int
select #count =count(2) from #RequireAuth where done=0
while (#count>0)
BEGIn
select top 1 #varible=val1 from #RequireAuth where done=0
exec sp_YourProc #variable
update R set done=1 from #RequireAuth R where val1=#varible
select #count =count(2) from #RequireAuth where done=0
END