I am utilising Spring Data JPA and am getting some strange behaviour that I cannot work out how to fix.
#Query(value="EXEC dbo.spChartPracticeInternalDoctor ?, ?, ?, ?", nativeQuery = true)
List<ReportDataSet> spChartPracticeInternalDoctor(Long id, String date,Long months, String tests);
When I pass in a CSV string in tests that contains no spaces the stored procedure executes correctly ( i.e. SC,ECG )
But when I pass in a tests string with a space it errors.
e.g. SC,ECG,IP Standard Consult
2018-04-12 12:45:13.309 DEBUG 684 --- [ XNIO-2 task-2] com.pci.dprm.aop.logging.LoggingAspect : Enter: com.pci.dprm.web.rest.ReportDataSetResource.spChartPracticeInternalDoctor() with argument[s] = [36, 2018-04-12T12:05:01 08:00, 24, SC,ECG,IP Standard Consult]
Hibernate: EXEC dbo.spChartPracticeInternalDoctor ?, ?, ?, ?
2018-04-12 12:45:13.316 WARN 684 --- [ XNIO-2 task-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 8114, SQLState: S0001
2018-04-12 12:45:13.317 ERROR 684 --- [ XNIO-2 task-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Error converting data type nvarchar to date.
It seems that hibernate is not escaping the string correctly when passing it to SQL, but I cannot work out how to overwrite the escaping.
I would appreciate any ideas to deal with this issue
I think error is not because of 4th parameter it is because of 2nd parameter. Logs shows that --> Error converting data type nvarchar to date.
I think you need to convert second argument to Date.
Please see below code snippet for your reference.
#Query(value="EXEC dbo.spChartPracticeInternalDoctor ?, ?, ?, ?", nativeQuery = true)
List<ReportDataSet> spChartPracticeInternalDoctor(Long id, Date date,Long months, String tests);
Related
I have a table like the one below in SQL Server:
I have an API that receives 15,000 records every 10 seconds to save in the table above. As if userid exist update that row if not exist userid insert record. I use the following code to write the record of each user with pyodbc (in python) That means I run the following code 15,000 times :
update Mytable
set buy = ?, model = ?, price = ?, color = ?, number = ?,
balance = ?, time = ?, type = ?,
where userid = ?
if ##ROWCOUNT = 0
insert into Mytable (userid, buy, model, price, color,
number, balance, time, type)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
The above code works fine but takes 700 seconds for 15,000 records. I have just this API and I have no other information about the number of users and ...
How can I save 15,000 records in less than seven seconds?
How can I save 15,000 records in less than seven seconds?
The most important things (in order of importance) are:
to not to send 15,000 separate batches or RPCs to SQL Server
to not run 15,000 separate transactions
and
to not run 15,000 separate DML statements.
The easiest way to do this from Python is to send the data as a JSON document and insert/update the data using batch update/insert, or merge.
You can send all 15,000 or break it into a few batches. See eg, Trying to insert pandas dataframe to temporary table
Once you have the data on the server you can use TSQL's MERGE, or a single UPDATE and single INSERT statement for the whole batch.
Thanks from #David Browne and This tutorial for use OPENJSON in SQL server.
I use OPENJSON and Write 15000 rows just in 1 second with below code (in pyodbc) and then delete duplicate old records with 1 query.
Write 1500 rows:
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=server_name;'
'Database=database_name;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
SqlSave= DECLARE #json NVARCHAR(max) = ' [
{ "userid" : 14F2G34, "buyV":"pen" ,"modelM" : "Bic", "color" : "red","numberB" : 4000,"balanceVal" : 750,"timeBuy" : 1631303488,"type":"simple" },
{ "userid" : 14F2G35, "buyV":"pen" ,"modelM" : "blueBic", "color" : "blue","numberB" : 1000,"balanceVal" : 150,"timeBuy" : 1631303488,"type":"coly" },
{ "userid" : 14F2G36, "buyV":"pen" ,"modelM" : "oli", "color" : "yellow","numberB" : 6000,"balanceVal" : 200,"timeBuy" : 1631303488,"type":"ni" },
...
]';
INSERT INTO Mytable
SELECT *
FROM OPENJSON(#json, '$')
WITH (
useruserid int '$.userid',
buy varchar(60) '$.buyV',
model varchar(60) '$.modelM',
color varchar(60) '$.color',
number varchar(60) '$.numberB',
balance varchar(60) '$.balanceVal',
time varchar(60) '$.timeBuy',
type varchar(60) '$.type',
);
cursor.execute(SqlSave)
cursor.commit()
Note: if length your string above is more than 4000 use NVARCHAR(max).
For update exist rows I delete old rows, I have ID column that auto incremental and I delete all duplicate rows by userid column except duplicate rows which have max ID by below code:
delete FROM mytable
WHERE ID NOT IN
( SELECT max(ID)
FROM mytable
GROUP BY userid
)
I am creating a WPF application using Advantage database server. I want to insert some data using stored procedure
Any sample code ?
I tried two input parameter TestID and TestName (both NCHAR)
INSERT INTO TestTable(
Test_Id,
Test_Name)
VALUES (
#TestID,
#TestName);
But show error like
Error 7200: AQE Error: State = HY000; NativeError = 5154;
[SAP][Advantage SQL Engine][ASA] Error 5154: Execution of the stored
procedure failed. Procedure Name: TestInsert. Error 7200: AQE
Error: State = S0000; NativeError = 2121; [SAP][Advantage SQL
Engine]Column not found: #TestID -- Location of error in the SQL
statement is: 42 (line: 3 column: 5) Error in stored procedure:
TestInsert AdsCommand query execution failed.
I am new in SAP ADS. Please help me.
use _XXXX notation for input parameters.
ie,
INSERT INTO TestTable( Test_Id, Test_Name)
VALUES ( _#TestID, _#TestName);
I'm having a problem while i'm trying to collect explain plan with the db2exfmt tool.
Can some body explain me the process of how to use that tool?
My requirement is to collect cost of a stored procedure. I have the EXPLAIN tables created in instance 'XYZ' schema and I have a procedure named "UNNAMED", which has the package name "P123456" and the schema "ABCD".
I used the following commands:
! db2exfmt -d SAMPLE -e DB2INST1 -s ABCD -n P123456 -g TIC -w -1 -#***5*** -t
Wherein 5 is the Section Number of the part of the procedure I'm trying to collect cost for.
Furthermore, I have also tried to do the following:
1) Identify the package corresponding to the stored proc :
select r.routineschema, r.routinename, rd.bname as packagename
from syscat.routines r, syscat.routinedep rd
where
r.specificname=rd.specificname and
r.routineschema=rd.routineschema and
rd.btype='K' and
r.routineschema = 'XYZ' and
r.routinename = 'ABCD'
2) Identify the section number for the SQL statement :
select sectno, text
from syscat.statements
where pkgschema='XYZ' and pkgname='P123456'
3) Populate Explain tables :
call EXPLAIN_FROM_CATALOG( 'XYZ', 'P123456', ' ', 5, 'SYSTOOLS', ?, ?, ?, ?, ? )
The latter throws an error:
Message: The parameter mode OUT or INOUT is not valid for a parameter in the routine named "EXPLAIN_FROM_CATALOG" with specific name "EXPLAIN_FROM_CATALOG" (parameter number "5", name "EXPLAIN_SCHEMA").. SQLCODE=-469, SQLSTATE=42886, DRIVER=3.50.152
I am logged in as USER : "MNO" and want the explain tables under SYSTOOLS schema to be populated.
Can someone please help me resolve the problem?
As the error message indicates, and the manual says, explain_schema is an INOUT parameter, so you cannot specify a literal value.
You may want to try wrapping the procedure call in a compound statement, providing declared variables for each OUT and INOUT parameter, something like:
begin
declare v_schema varchar(50) default 'SYSTOOLS';
declare v_req, v_srcname, v_srcschema, v_srcver varchar(128);
declare v_ts timestamp;
call EXPLAIN_FROM_CATALOG( 'XYZ', 'P123456', ' ', 5, v_schema,
v_req, v_ts, v_srcname, v_srcschema, v_srcver );
end
PS. Code is not tested
You have to specify every IN/OUT parameter with a "?" - running it will then prompt you for the input value ('SYSTOOLS').
So try
call EXPLAIN_FROM_CATALOG( 'XYZ', 'P123456', ' ', 5, **?**, ?, ?, ?, ?, ? )
How to find which field is giving error in query:
com.abc.fast.common.db.exception.UncategorizedSQLException: CallableStatementCallback; SQL [{call p_proc_u(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}]; [ErrorCode: 257; SQLState: 42000; Message: Implicit conversion from datatype 'VARCHAR' to 'NUMERIC' is not allowed. Use the CONVERT function to run this query. (DataSource: 1, Type: SYBASE)]; nested exception is com.sybase.jdbc3.jdbc.SybSQLException: Implicit conversion from datatype 'VARCHAR' to 'NUMERIC' is not allowed. Use the CONVERT function to run this query.
This indicates that a numeric value is getting enclosed in quotes, or a character value is missing quotes.
Double check your code to make sure values are being properly passed into the database.
If you're experiencing this using Spring's Stored Procedure template, make sure the order of your 'declareParameter' is in the same order as the parameter list as specified in the destination stored procedure.
Given a stored proc that looks like:
CREATE PROCEDURE dbo.sp_ac_createStoredProc
(
#pFirst_name VARCHAR(100),
#pLast_name VARCHAR(100) = NULL,
#pAge NUMERIC(2,0) = NULL
declare your parameters like this
declareParameter(new SqlParameter(FIRST_NAME, Types.VARCHAR));
declareParameter(new SqlParameter(LAST_NAME, Types.VARCHAR));
declareParameter(new SqlParameter(AGE, Types.NUMERIC));
and it will be fine. Declare your parameters like this:
declareParameter(new SqlParameter(AGE, Types.NUMERIC));
declareParameter(new SqlParameter(FIRST_NAME, Types.VARCHAR));
declareParameter(new SqlParameter(LAST_NAME, Types.VARCHAR));
Well, basically, just don't.
You can see the variable values it finally used to call stored proc. This can be acheived by seting jdbctemplate logger to DEBUG mode. For example if you are using Spring jdbctempate then you can set property log4j.logger.org.springframework.jdbc.core to DEBUG. This can be done in log4j.properties or logback.xml.
I am creating a stored procedure inside my DB2 database, here is my stored procedure codes:
CREATE OR REPLACE PROCEDURE PWDCHANGE (UNAME IN VARCHAR(32),
OLDPWD IN VARCHAR(32),
NEWPWD IN VARCHAR(32))
AS
BEGIN
IF LOGINTABLE.USERNAME = UNAME AND LOGINTABLE.PASSWORD = OLDPWD THEN
UPDATE LOGINTABLE SET PASSWORD = NEWPWD;
DBMS_OUTPUT.PUT_LINE('Password Changed Successfully!');
ELSE DBMS_OUTPUT.PUT_LINE('Incorrect Old Password Input');
END IF;
END;
But as I deploy it in my IBM Data Studio it displays an error which says:
Deploy SENJOBLADE.PWDCHANGE(VARCHAR(32), VARCHAR(32), VARCHAR(32))
Running
SENJOBLADE.PWDCHANGE - Deploy started.
Create stored procedure returns SQLCODE: -104, SQLSTATE: 42601.
SENJOBLADE.PWDCHANGE: 1: An unexpected token "VARCHAR" was found following "PWDCHANGE (UNAME IN". Expected tokens may include: ",".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.13.111
An unexpected token "VARCHAR" was found following "PWDCHANGE (UNAME IN". Expected tokens may include: ",".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.13.111
SENJOBLADE.PWDCHANGE - Deploy failed.
SENJOBLADE.PWDCHANGE - Roll back completed successfully.
I don't know why VARCHAR is an unexpected token, I don't know what to change to be able to deploy my stored procedure. The function of this stored procedure is to accept 3 inputs which is used for password changing, a username, a current password, and the replacement password that's why I have 3 IN values.
SENJOBLADE is my schema name, LOGINTABLE is the table name which has two columns, the USERNAME & PASSWORD column
I tried removing the VARCHARs and this error displays:
Deploy SENJOBLADE.PWDCHANGE(IN, IN, IN)
Running
SENJOBLADE.PWDCHANGE - Deploy started.
Create stored procedure returns SQLCODE: -104, SQLSTATE: 42601.
SENJOBLADE.PWDCHANGE: 2: An unexpected token "BEGIN" was found following "PWD IN)
AS
". Expected tokens may include: ":".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.13.111
An unexpected token "BEGIN" was found following "PWD IN)
AS
". Expected tokens may include: ":".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.13.111
SENJOBLADE.PWDCHANGE - Deploy failed.
SENJOBLADE.PWDCHANGE - Roll back completed successfully.
Please help, I'm still learning about stored procedures so I'm not yet used to it. Any help would be appreciated, thank you!
try Deploy SENJOBLADE.PWDCHANGE(UNAME, OLDPWD, NEWPWD)