Comments (11)
See you can clearly see that it is not accepting ';' at the end of the statement so remove it in the program.
But when it comes to .go program you are using fmt.Sprintf(``) and you added \ at the start and end of days('1970-01-01-00.00.00.000000') so you have to remove \ and ; in the go program.
make the changes and run the program.
If you face any issues let me know.
Thanks,
Akhil
@akhilravuri1 Thanks I removed both the \ and ; and it worked for the
CREATE OR REPLACE FUNCTION %[1]s (in db2ts timestamp)
returns bigint
language sql
deterministic
no external action
return (
bigint((days(db2ts - current timezone) - days(\'1970-01-01-00.00.00.000000\')) * 86400 + midnight_seconds(db2ts - current timezone))*1000 + microsecond(db2ts)/1000
);
Will try the other one now and see what happens, the other SUBSTRING_INDEX one is a little more complicated.
from go_ibm_db.
Other one worked as well after removing the ;; at the end. Thanks
from go_ibm_db.
Hi @devanshah2
we don't have test cases for creating a function but it should be working as it is an SQL query.
Thanks,
Akhil
from go_ibm_db.
Hi @devanshah2
we don't have test cases for creating a function but it should be working as it is an SQL query.Thanks,
Akhil
I had tried a quick example as follows:
const query := '--#SET TERMINATOR ;;
CREATE OR REPLACE FUNCTION SUBSTRING_INDEX(in varchar(2000), delimit varchar(200), n Int)
returns varchar(2000)
deterministic
no external action
language sql
contains sql
begin atomic
declare out varchar(2000);
declare dem varchar(2000);
declare num int;
declare pos int;
declare temp varchar(2000);
set dem=delimit;
set temp=In;
set num=n;
set pos=1;
if(num<0) then
while(locate(delimit,temp)!=0) do
set temp=substr(temp,locate(delimit,temp)+1);
set num=num+1;
end while;
set num=num+1;
set temp=In;
end if;
while (num>0) do
set pos=pos+locate(delimit,temp)-1;
set temp=substr(temp,locate(delimit,temp)+1);
set num=num-1;
end while;
if(n>0) then
return substr(In,1,pos);
else
return substr(In,pos+1);
end if;
end;;
--#SET TERMINATOR ;'
db, err := sql.Open("go_ibm_db", con)
if err != nil {
log.Error(err)
return nil, err
}
db.Exec(query)
and it throws error when I run it:
SQLExecute: {42601} [IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "begin atomic declare out" was found following "contains sql ". Expected tokens may include: "". LINE NUMBER=60. SQLSTATE=42601
from go_ibm_db.
Hi @devanshah2
This error is thrown by the server. So please try the same sql with db2cmd and check whether the sql is accepted or not.
Thanks
Akhil
from go_ibm_db.
Hi @devanshah2
This error is thrown by the server. So please try the same sql with db2cmd and check whether the sql is accepted or not.
Thanks
Akhil
The same works just fine in the db2cmd and using db2 -tfw sample.sql
Anything else that I can try? to verify this?
from go_ibm_db.
@devanshah2 can you please send the sample program to reproduce the issue.
Thanks,
Akhil
from go_ibm_db.
@devanshah2 can you please send the sample program to reproduce the issue.
Thanks,
Akhil
Following is a runable program:
package main
import (
"database/sql"
"fmt"
_ "github.com/ibmdb/go_ibm_db"
)
func createCon(con string) *sql.DB {
db, err := sql.Open("go_ibm_db", con)
if err != nil {
fmt.Println(err)
return nil
}
return db
}
func execquery(db *sql.DB) error {
query := fmt.Sprintf(`
CREATE OR REPLACE FUNCTION %[1]s (in db2ts timestamp)
returns bigint
language sql
deterministic
no external action
return (
bigint((days(db2ts - current timezone) - days(\'1970-01-01-00.00.00.000000\')) * 86400 + midnight_seconds(db2ts - current timezone))*1000 + microsecond(db2ts)/1000
);
`, "utcmillis")
// Execute DDL Command/Query
_, err := db.Exec(query)
// Handle error from query execution
if err != nil {
fmt.Errorf("Unable to execute query due to error: %v", err)
return err
}
return nil
}
func main() {
con := "HOSTNAME=host;DATABASE=name;PORT=number;UID=username;PWD=password"
db := createCon(con)
err := execquery(db)
if err != nil {
fmt.Println(err)
}
}
Which is able to re-produce error:
SQLExecute: {42601} [IBM][CLI Driver][DB2/LINUXX8664] SQL0007N The statement was not processed because a character that is not supported in SQL statements was included in the SQL statement. Invalid character: "". Text preceding the invalid character: "nt timezone) - days(". LINE NUMBER=7. SQLSTATE=42601
Will need to fill in your own HOSTNAME=host;DATABASE=name;PORT=number;UID=username;PWD=password
thanks
from go_ibm_db.
can you please create create_func.t file with the below content
opt echo on
opt callerror on
SqlAllocEnv 1
SqlAllocConnect 1 1
SqlDriverConnect 1 1 "DATABASE=go;HOSTNAME=localhost;PORT=50000;UID=uname;Pwd=***;PROTOCOL=TCPIP" -3 255 sql_driver_noprompt
sqlallocstmt 1 1
sqlprepare 1 "CREATE OR REPLACE FUNCTION utcmillis (in db2ts timestamp) returns bigint language sql deterministic no external action return ( bigint((days(db2ts - current timezone) - days(\'1970-01-01-00.00.00.000000\')) * 86400 + midnight_seconds(db2ts - current timezone))*1000 + microsecond(db2ts)/1000 );" -3
sqlexecute 1
SqlFreeStmt 1 Sql_close
SqlTransact 1 1 Sql_commit
Killenv 1
please add your connection details
How to run create_func.t file:-
- open a command prompt
- cd to the create_func.t file directory
- run this commad in the commad prompt
db2cli < create_func.t
from go_ibm_db.
db2cli < create_func.t
@akhilravuri1 Ran and following was the output:
IBM DATABASE 2 Interactive CLI Sample Program
(C) COPYRIGHT International Business Machines Corp. 1993,1996
All Rights Reserved
Licensed Materials - Property of IBM
US Government Users Restricted Rights - Use, duplication or
disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
> > opt callerror on
> SqlAllocEnv 1
SQLAllocEnv: rc = 0 (SQL_SUCCESS)
CLI henv = 1, Test Driver henv = 1
> SqlAllocConnect 1 1
SQLAllocConnect: rc = 0 (SQL_SUCCESS)
CLI hdbc = 1, Test Driver hdbc = 1
> SqlDriverConnect 1 1 "DATABASE=go;HOSTNAME=localhost;PORT=50000;UID=uname;Pwd=****!;PROTOCOL=TCPIP" -3 255 sql_driver_noprompt
SQLDriverConnect: rc = 0 (SQL_SUCCESS)
SQLDriverConnect: ConnStrOut:
UID=uname;PWD=******;DATABASE=go;HOSTNAME=localhost;PORT=50000;PROTOCOL=TCPIP;
cbConnStrOut: 91
> sqlallocstmt 1 1
SQLAllocStmt: rc = 0 (SQL_SUCCESS)
CLI hstmt = 1, Test Driver hstmt = 1
> sqlprepare 1 "CREATE OR REPLACE FUNCTION utcmillis (in db2ts timestamp) returns bigint language sql deterministic no external action return ( bigint((days(db2ts - current timezone) - days('1970-01-01-00.00.00.000000')) * 86400 + midnight_seconds(db2ts - current timezone))*1000 + microsecond(db2ts)/1000 );" -3
SQLPrepare: rc = 0 (SQL_SUCCESS)
> sqlexecute 1
SQLExecute: rc = -1 (SQL_ERROR)
SQLError: rc = 0 (SQL_SUCCESS)
SQLError: SQLState : 42601
fNativeError : -104
szErrorMsg : [IBM][CLI Driver][DB2/6000] SQL0104N An unexpected token ";" was found following "second(db2ts)/1000 )". Expected tokens may include: "END-OF-STATEMENT". LINE NUMBER=1. SQLSTATE=42601
cbErrorMsg : 189
SQLError: rc = 100 (SQL_NO_DATA_FOUND)
> SqlFreeStmt 1 Sql_close
SQLFreeStmt: rc = 0 (SQL_SUCCESS)
> SqlTransact 1 1 Sql_commit
SQLTransact: rc = 0 (SQL_SUCCESS)
> Killenv 1
SQLDisconnect: rc = 0 (SQL_SUCCESS)
SQLFreeConnect: rc = 0 (SQL_SUCCESS)
SQLFreeEnv: rc = 0 (SQL_SUCCESS)
>
from go_ibm_db.
See you can clearly see that it is not accepting ';' at the end of the statement so remove it in the program.
But when it comes to .go program you are using fmt.Sprintf(``) and you added \ at the start and end of days('1970-01-01-00.00.00.000000') so you have to remove \ and ; in the go program.
make the changes and run the program.
If you face any issues let me know.
Thanks,
Akhil
from go_ibm_db.
Related Issues (20)
- Getting issues during go vet
- SQLDriverConnect: {HY000} [IBM][CLI Driver] SQL10007N Message "0" could not be retrieved. Reason code: "3". HOT 7
- Not able to connect to a new DB2 instance as soon as previous connection is closed HOT 6
- Context with timeout will not prevent long running queries HOT 3
- `ExecContext` and `QueryContext` do not implement any `database/sql` interfaces HOT 1
- Installer setup program fails. HOT 2
- Link Error `libdb2.so: undefined reference to sqloRemStgDelete` HOT 6
- Unable to run [email protected] on M1 architecture HOT 14
- error while loading shared libraries: libcrypt.so.1 HOT 3
- ibmdb/go_ibm_db pooling vs database/sql pooling HOT 6
- Panic when initDriver HOT 6
- index out of range [2021] with length 2021 HOT 3
- ../../go/pkg/mod/github.com/ibmdb/[email protected]/api/api_unix.go:13:11: fatal error: sqlcli1.h: No such file or directory HOT 2
- How to fix CLI0129E An attempt to allocate a handle failed because there are no more handles to allocate HOT 2
- After connecting to db2 successfully, Chinese character display exception in the query result HOT 14
- Stored procedures support array-based input parameters HOT 2
- Inconsistent GSKIT Error 2 HOT 12
- fatal error: invalid pointer found on stack HOT 6
- [help]how to debug the cost breakdown HOT 2
- The gbk encoded Chinese field may be truncated when read in the utf8 operating environment HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go_ibm_db.