Error : there is no parameter $1postgres sql - sql

I am trying to insert data into table using below query. But it's working fine without parametrization but when I add parameter to query. It throws error there is no parameter $1.
Using nodejs to execute query.
INSERT INTO or(
json,id,ortatus,status,
qty,initial_qty,name,position,pled,
price,value,created_by,created_on,modified_on)
SELECT
$1,$2,$3,$4,$5,$6,$7,$8,$8,$10,$11,'0000000000',to_timestamp('${timestamp}'),
CURRENT_TIMESTAMP WHERE NOT EXISTS (SELECT ID FROM OR WHERE ID=$12)
ON CONFLICT DO NOTHING`,[`${Json}`,Id,orStatus,Status,
qty,initialQty,`${name}`,Position,pled,Price,Value,Id])

Related

Error "Token unknown - line 4, column 2; SET" creating trigger with variables

I want to create a trigger, but I'm getting an error.
I have 2 tables STOK and STOK_HAREKET:
STOK = (ID,URUN,FIYAT,MINIMUM,MAXIMUM)\
STOK_HAREKET = (STOK_ID,FİYAT,GIRIS,CIKIS)\
I want to build a trigger to STOK_HAREKET. On insert and update, the GIRIS and CIKIS ın STOK_HAREKET table is collected and submitted
from the output and save this value to the MAXIMUM value ın the STOK table.
CREATE TRIGGER T_STOK_BIU FOR STOK_HAREKET BEFORE INSERT OR UPDATE
AS
begin
Set #gsum = ( SELECT SUM(GIRIS) FROM STOK_HAREKET WHERE STOK_ID = new.STOK_ID );
Set #csum = ( SELECT SUM(CIKIS) FROM STOK_HAREKET WHERE STOK_ID = new.STOK_ID );
Set #toplm = (#gsum-#csum)
update STOK set MAKSIMUM = #toplm where ID = new.STOK_ID
end
This code gives an error:
org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [335544634] [42000]: Dynamic SQL Error; SQL error code = -104; Token unknown - line 4, column 2; SET [SQLState:42000, ISC error code:335544634]
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:133)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeStatement(SQLQueryJob.java:578)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.lambda$1(SQLQueryJob.java:487)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:173)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeSingleQuery(SQLQueryJob.java:494)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.extractData(SQLQueryJob.java:913)
at org.jkiss.dbeaver.ui.editors.sql.SQLEditor$QueryResultsContainer.readData(SQLEditor.java:3760)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.lambda$0(ResultSetJobDataRead.java:123)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:173)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.run(ResultSetJobDataRead.java:121)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer$ResultSetDataPumpJob.run(ResultSetViewer.java:5033)
at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:105)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: java.sql.SQLSyntaxErrorException: Dynamic SQL Error; SQL error code = -104; Token unknown - line 4, column 2; SET [SQLState:42000, ISC error code:335544634]
at org.firebirdsql.gds.ng.FbExceptionBuilder$Type$1.createSQLException(FbExceptionBuilder.java:534)
at org.firebirdsql.gds.ng.FbExceptionBuilder.toFlatSQLException(FbExceptionBuilder.java:304)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.readStatusVector(AbstractWireOperations.java:140)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.processOperation(AbstractWireOperations.java:204)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.readSingleResponse(AbstractWireOperations.java:171)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.readResponse(AbstractWireOperations.java:155)
at org.firebirdsql.gds.ng.wire.AbstractWireOperations.readGenericResponse(AbstractWireOperations.java:257)
at org.firebirdsql.gds.ng.wire.AbstractFbWireDatabase.readGenericResponse(AbstractFbWireDatabase.java:201)
at org.firebirdsql.gds.ng.wire.version11.V11Statement.prepare(V11Statement.java:89)
at org.firebirdsql.jdbc.FBStatement.prepareFixedStatement(FBStatement.java:881)
at org.firebirdsql.jdbc.FBStatement.internalExecute(FBStatement.java:868)
at org.firebirdsql.jdbc.FBStatement.executeImpl(FBStatement.java:496)
at org.firebirdsql.jdbc.FBStatement.execute(FBStatement.java:482)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.execute(JDBCStatementImpl.java:329)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.lambda$0(JDBCStatementImpl.java:131)
at org.jkiss.dbeaver.utils.SecurityManagerUtils.wrapDriverActions(SecurityManagerUtils.java:96)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:131)
... 12 more
Caused by: org.firebirdsql.jdbc.FBSQLExceptionInfo: Dynamic SQL Error
The syntax you use is not valid syntax for Firebird. I recommend you review the Firebird 4.0 Language Reference, chapter Procedural SQL (PSQL) Statements for the correct syntax for Firebird.
Specifically, you need to explicitly declare variables before use, and use select ... into ... to assign values from a select.
However, there are more problems with your trigger:
You execute two separate selects, which could have been one select
You retrieve values using two selects, and then use those for an update. This can be done in one merge statement
As user13964273 remarks in the comments, doing this is a bad idea because of transaction visibility and performance (or having to retry concurrent updates which conflict)
Taken at face value, your trigger could be rewritten to:
create trigger T_STOK_BIU for STOK_HAREKET before insert or update
as
begin
merge into STOK
using (
select STOK_ID, sum(GIRIS) - sum(CIKIS) as toplm
from STOK_HAREKET
where STOK_ID = new.STOK_ID
group by STOK_ID
) as src
on STOK.STOK_ID = src.STOK_ID
when matched then update set STOK.MAKSIMUM = src.toplm;
end
However, you should consider instead using a view to calculate this on the fly when needed. Or use a table which has a summarized aggregate row, and insert changes (delta rows) into the table through a trigger, and on a schedule recompute the summarized aggregate row and delete those delta rows. You can then - use a view to calculate the actual value when needed, while reducing performance concurrency issues.

How do we insert data into a table?

I'm attempting to insert data into a table:
#one_files =
EXTRACT //all columns
FROM "/1_Main{suffixOne}.csv"
USING Extractors.Text(delimiter : '|');
CREATE TABLE A1_Main (//all cols);
INSERT INTO A1_Main SELECT * FROM #one_files;
Within the same script I'm attempting to SELECT data:
#finalData =
SELECT //mycols
FROM A1_Main AS one;
OUTPUT #finalData
TO "/output/output.csv"
USING Outputters.Csv();
Here's the exception I get:
What am I doing wrong? How do I select from my table? Can we not insert and query in the same script?
Some statements have restrictions on how they can be combined inside a script. For example, you cannot create a table and read from the same table in the same script, since the compiler requires that any input already physically exists at compile time of the query.
Check this:
https://learn.microsoft.com/en-us/u-sql/concepts/scripts

SQL query regarding Sequence

I made a sequence in Net Beans. But when I tried to insert data in the table I get an error.
My code is:
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (seq_person.nextval,'Lars','Monsen');
The error is:
[Exception, Error code 30,000, SQLState 42X04] Column
'SEQ_PERSON.NEXTVAL' is either not in any table in the FROM list or
appears within a join specification and is outside the scope of the
join specification or appears in a HAVING clause and is not in the
GROUP BY list. If this is a CREATE or ALTER TABLE statement then
'SEQ_PERSON.NEXTVAL' is not a column in the target table.
The error message Error code 30,000, SQLState 42X04 indicates you are using Derby DB not Oracle. That being the case you need to use Derby syntax for getting the next value. So your insert should look like this:
INSERT INTO Persons (ID,FirstName,LastName)
VALUES (NEXT VALUE FOR seq_person,'Lars','Monsen')

SAP HANA: Loading tables from with statements in stored procedures

Is it possible to load a table from a with statement via a stored procedure in HANA? Nothing i try seems to work, and the only thing that does work when creating a procedure is just displaying the data from the with statement via a select. Below I show three examples I have tried for accessing with statement data. Currently on HANA revision 84. Please note the table create is just for purposes of the test example.
CREATE PROCEDURE test_proc
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
AS
BEGIN
create table t1 (cal_day date);
with w1 as (
select current_date cal_day from dummy
)
--works just fine but isn't loading the data into anything
select * from w1;
--get indentifier must be declared error
select cal_day into t1 from w1;
--get incorrect syntax error
insert into t1
select cay_day from w1;
END
When you want to execute DDL statements in a SQLScript procedure you'll need to use dynamic SQL for that (EXEC).

Error Insert Into a table variable Hana

I'm working passing ms sql server statements into hana sql statements. I have a variable (Tab) type Table and a variable string (query) defined as:
Hana Statement
CREATE TYPE "tab_table_TYPE" AS TABLE ("DocEntry" integer, "LineId" integer, "VisOrder" integer, "Object" nvarchar(20));
v_Tab tab_table_TYPE
query := 'SELECT [DocEntry],[LineId],[VisOrder] ,[Object] FROM [#INV_AFC]';
so I'm trying to convert this Ms Sql Statement into a Hana Statement :
Ms Sql Server Statement
INSERT INTO #v_Tab([DocEntry],[LineId],[VisOrder],[Object]) exec (#query)
I wish to use an internal table type variable which can hold the resultset from the query!
When I use the Sql Converter with this sentence displays this error:
--[Note:ErrorModifier] INSERT INTO statement does not support EXEC; use EXEC('insert into table '+ originalstatement)
--[Note:Stringifier] SAP HANA does not support InsertTarget
Finally the question is : How would be the correct Hana Sql Statement for this case?
The syntax of your table-type creation is correct. I guess you are trying to execute the query inside a procedure.
Inside a procedure, you have different options. If your query is only a partial result and you want to run further statements on the result set of the query, you don't need to initialize a table variable at all. Just assign a variable to a resultset:
table_variable = SELECT DocEntry, LineId, VisOrder, Object FROM INV_AFC;
// Use table_variable for further statements, for example:
SELECT count(*) INTO resultSetCount FROM :table_variable;
If your query is already the final result, you can easily define an output variable and directly assign your result set as output variable. For example:
CREATE PROCEDURE "YOURSCHEMA"."SomeProcedureName" (
in someInputVariable1 NVARCHAR(255),
in someInputVariable2 BIGINT,
out tableVariable "YOURSCHEMA".tab_table_TYPE)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
tableVariable = SELECT DocEntry, LineId, VisOrder, Object FROM INV_AFC;
END;
When you then call this procedure the 3rd parameter will automatically contain your result set:
call "YOURSCHEMA"."SomeProcedureName"('someString', 123, ?);