PostgreSQL error: function returning record called in context that cannot accept type record, on remote dblink call - postgresql-9.6

When I run this code:
do
$func$
declare cmd text;
begin
cmd = FORMAT($$ select public.func_doit('%s'::date, '%s'::date); $$,'2019-01-01', '2019-03-01');
PERFORM DBLINK('my_conn', cmd);
end;
$func$
I receive the following error:
SQL Error [0A000]: ERROR: function returning record called in context that cannot accept type record
Where: SQL statement "SELECT DBLINK('my_conn', cmd)"
PL/pgSQL function inline_code_block line 12 at PERFORM
ERROR: function returning record called in context that cannot accept type record
Where: SQL statement "SELECT DBLINK('my_conn', cmd)"
PL/pgSQL function inline_code_block line 12 at PERFORM
ERROR: function returning record called in context that cannot accept type record
Where: SQL statement "SELECT DBLINK('my_conn', cmd)"
PL/pgSQL function inline_code_block line 12 at PERFORM
The function being called: public.func_doit, RETURNS void.
There is no record in the mix as referenced by the error message. The function just does something and there's nothing to return.
After placing notices inside the called function, non are posted, which means that the function doesn't even begin to execute.
I know the function works because when I call it directly from its local DB, it works as expected, with the same parameters.
PERFORM is the right statement to use here because the function doesn't return anything.
And I also tried: ...perform * from public.func_doit..., which throws a syntax error.
What could I be missing here?

Related

Delphi FDQuery Fieldbyname conversion error

I have defined a MySQL database with a table that includes a field of type varchar(128), I call it fieldinquestion. I use a query to access this field, which usually works nicely. However, when I assign a string to the field, I get an error that I don't understand.
try
Self.FDQuery1.FieldByName('fieldinquestion').AsString:='Hello There'; //or something similar.
except
---> error: 'Could not convert variant of type (String) into type (Double)'
aDataType:=Self.FDQuery1.FieldByName(GCssValidationDisplay).DataType; --datatype is ftString
end;
In the try..except clause, this causes an error, but a verification for the datatype shows it is a string!
Any ideas?

how to execute a c function which is written in informix?

I have written a C function which print hello world
and registered as below:
CREATE FUNCTION my_func()
RETURNING INT
EXTERNAL NAME "/tmp/mytest.c"
LANGUAGE C
END FUNCTION;
placed the c code in /tmp/mytest.c in Informix server and the above function created. But when I am executing the function in dbeaver I am getting below error.
SQL Error[IX000]:User Defined Routine (my_func) module load failed
I have a Informix function which should call C function

How to use a temporary table with dynamic sql?

Im want to use a temporary table with dynamic sql but i have an error saying that the column Column, parameter, or variable #3: Cannot find data type MyTable. Must declare the table variable "#CountriesFound".
Here's my code
ALTER PROCEDURE sqlDynamic #countryId int
AS
CREATE TYPE MyTable AS TABLE ([countries] varchar(50));
DECLARE #statement NVARCHAR(MAX)
DECLARE #CountriesFound AS MyTable
INSERT INTO #CountriesFound(countries)
select Name
FROM CountriesFound
SET #statement =
'SELECT name, CASE WHEN (c.name IN (SELECT countries FROM #CountriesFound)) THEN 1
ELSE 0
END as countryFound FROM Country c WHERE c.countryId = #countryId'
EXECUTE sp_executesql #statement,
N'#countryId int, #CountriesFound MyTable READONLY',
#countryId = #countryId,
#CountriesFound=#CountriesFound;
Any idea how to fix it ?
The error you are getting has nothing to do with dynamic sql. You cannot create a stored procedure that uses a User-Defined DataType (MyTable in your example) in the same procedure that is going to create the TYPE. The TYPE has to already be created before the procedure will even let the code compile.
Consider this simplified code:
CREATE PROCEDURE TypeTest
AS
CREATE TYPE MyTable AS TABLE ([Id] int);
DECLARE #mt AS MyTable;
Just executing this will give the error:
Msg 2715, Level 16, State 3, Procedure TypeTest, Line 1 Column,
parameter, or variable #1: Cannot find data type MyTable. Parameter or
variable '#mt' has an invalid data type.
You need to CREATE the TYPE outside of the procedure code before you try to create the procedure. It doesn't make sense to CREATE a TYPE inside a procedure code, since TYPE objects are permanent on the server until they are dropped anyway, and procedures are usually created to be run multiple times.
It would be nice if the SQL Server gave an error message more to this effect, but SQL Server isn't famous for having the most helpful error messages.

Script always throws an error if CREATE/ALTER is not first statement

I am getting an error when I try to execute the script that is given at end of this post. My requirement is to check for procedure's existence, then drop it if it exists and finally create the procedure.
How would I do this using a single script file?
Procedure Proc_GenerateTestData. 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch. SQL2.sql 12 1
The SQL script that always throws above error is as below.
IF OBJECT_ID('dbo.Proc_GenerateTestData', 'p') IS NOT NULL
BEGIN
DROP PROCEDURE dbo.Proc_GenerateTestData
END
CREATE PROCEDURE dbo.Proc_GenerateTestData #numberOfRecords INT = 100
AS
--drop table #temp1
DECLARE #currentTime AS TIME = CAST(GETDATE() AS TIME);
DECLARE #currentWorkShiftDate AS DATETIME = CAST(CAST(GETDATE() AS DATE) AS DATETIME);
DECLARE #startTime TIME,
#lastShiftStartTime TIME;
DECLARE #tenantId AS UNIQUEIDENTIFIER = (SELECT TOP (1)
Tenants.Id
FROM Tenants
ORDER BY Tenants.Id ASC);
DECLARE #workshiftId INT,
#lastShiftStartDate DATETIME;
--more statements for this procedure follow
As the error clearly says - the CREATE PROCEDURE must be the first statement in a query batch.
This is not the case here, since you have the check for the DROP before hand.
You need to change your script so that the CREATE PROCEDURE statement is really the first in the query batch. If you're running this in SQL Server Management Studio, just add a GO delimiter before the CREATE.

dynamic parameter or variable required as INOUT or OUT argument

right now iam trying to create some Stored Procedures for a HSQL-DB.
I want to create a new User and return the ID of the new User.
For example i tried it like this to create the Procedure.
create procedure test(out param int)
modifies sql data
begin atomic
set param = 1;
end;
=> Call it
declare param int ;
call test(param);
call param;
=> Error
dynamic parameter or variable required as INOUT or OUT argument
I canĀ“t find the bug -.-
so long.
This is how you call the procedure:
declare param int;
call test(param)
call param
It looks the same, but each statement is executed separately!

Resources