Error while creating date column in tables- teradata - sql

proc sql;
connect to teradata as tera(mode=teradata server=oneview user="&teraid." password="&terapwd.");
execute(CREATE MULTISET TABLE UD497.PAN_AM_EMAIL
(
ATHNUM DECIMAL(10,0),
BLK_1_CDE CHAR(1),
BLK_2_CDE CHAR(1),
OPEN_DT DATE,
LANGUAGE CHAR(7),
MKTCELL CHAR(2),
PROJECT_ID CHAR(15),
CAMPAIGN CHAR(35);
) PRIMARY INDEX(ATHNUM);
) by tera;
Error Message :
ERROR: Teradata execute: Syntax error, expected something like a 'CHECK' keyword between ',' and the 'LANGUAGE' keyword.

The error message suggests that LANGUAGE is a keyword in Teradata, simply double quote it:
"LANGUAGE" CHAR(7),
But now, whenever you use it in SQL, you must double quote it, e.g.
select "LANGUAGE"...
Maybe simply change the name.

Related

How to call a DB2 AS400 table valued function which take Date parameters

I have a table valued function as below
CREATE FUNCTION ABELIBLE.TVFBOEGETSHIPMENTS (STARTDATE DATE, ENDDATE DATE, ADDRESSCODE CHAR(9))
RETURNS TABLE (ID INT, JOBNUMBER CHAR(9), CUSTOMERREFERENCE CHAR(18),
CONSIGNEENAME CHAR(30), CREATEDDATE DATE, AIRPORTOFORIGIN CHAR(3), AIRPORTOFARRIVAL CHAR(3),
AIRPORTOFDESTINATION CHAR(3), COUNTRYOFDESTINATION CHAR(3), ADDRESSCODE CHAR(9), CONSIGNMENTNUMBER CHAR(25))
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN
SELECT
ROW_NUMBER() OVER(ORDER BY EMJOBN DESC),
A.EMJOBN,
A.EMCREF,
A.EMOSNM,
DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')),
A.EMAOFO,
A.EMAOFA,
A.EMAOFD,
A.EMCOFD,
A.EMUKCD,
A.EMRPRT
FROM DTALIBLE.EMASTER A WHERE A.EMPSFT = 'Y' AND A.EMUKCD = ADDRESSCODE AND
DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')) >= DATE(STARTDATE) AND DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')) <= DATE(ENDDATE)
However I am not getting any results back when I run the following query
SELECT * FROM TABLE(ABELIBLE.TVFBOEGETSHIPMENTS(DATE('06/06/2019'), DATE('06/07/2020'),'MUL0044')) AS XXX
I am using DBeaver to run the query on AS400.
For my own sanity I tired the select statement with literals and i got the following result
Then again when I tried with below dates it does not fetch any results and i get this error.
any help is much appreciated. Thank you in advance.
The only thing that jumps out at me is that in your invocation,
SELECT * FROM TABLE(ABELIBLE.TVFBOEGETSHIPMENTS(DATE('06/06/2019'), DATE('06/07/2020'),'MUL0044')) AS XXX
'MUL0044' as a literal is treated as varchar, and your UDTF has the parm defined as char.
That mismatch usually gives a Function not found error however. You can cast the literal to char or change the parm type to varchar. Which to chose depends on how it will normally be invoked. If you're just testing with a literal and the function would normally be passed a char then just cast the literal to char.
If you use the Run SQL Scripts component of IBM ACS, you'll have the ability to debug the function.

How to create a user defined function that returns a table in a DB2 module?

I am trying to create a user-defined function that returns a table in DB2. Here is what I have so far.
This is a table that I use:
CREATE TABLE "CORPDATA"."EMPLOYEE" (
"EMPNO" CHAR(6) NOT NULL,
"FIRSTNME" VARCHAR(12) NOT NULL,
"MIDINIT" CHAR(1) NOT NULL,
"LASTNAME" VARCHAR(15) NOT NULL,
"WORKDEPT" CHAR(3),
"PHONENO" CHAR(4),
"HIREDATE" DATE,
"JOB" CHAR(8),
"EDLEVEL" SMALLINT NOT NULL,
"SEX" CHAR(1),
"BIRTHDATE" DATE,
"SALARY" DECIMAL(9 , 2),
"BONUS" DECIMAL(9 , 2),
"COMM" DECIMAL(9 , 2)
);
ALTER TABLE "CORPDATA"."EMPLOYEE" ADD CONSTRAINT "PK_EMPLOYEE" PRIMARY KEY
("EMPNO");
This is a user-defined function that returns a table (which is working fine):
CREATE OR REPLACE FUNCTION "CORPDATA"."DEPTEMPLOYEES" (DEPTNO CHAR(3))
RETURNS TABLE (EMPNO CHAR(6),
LASTNAME VARCHAR(15),
FIRSTNAME VARCHAR(12))
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN ATOMIC
RETURN
SELECT EMPNO, LASTNAME, FIRSTNME
FROM CORPDATA.EMPLOYEE
WHERE WORKDEPT = "DEPTEMPLOYEES".DEPTNO;
END
This is a how far I was able to get with the module:
CREATE MODULE CORPDATA.MODULE1
ALTER MODULE CORPDATA.MODULE1
PUBLISH FUNCTION DEPTEMPLOYEES2 (DEPTNO CHAR(3))
RETURNS TABLE (EMPNO CHAR(6),
LASTNAME VARCHAR(15),
FIRSTNAME VARCHAR(12))
Any attempts to actually add a function to a module failed with various errors. Here is my DB2 version information:
Database server = DB2/LINUXX8664 11.1.2.2
This is an Express-C installation under Redhat.
When I try this, I get
SQL0628N Multiple or conflicting keywords involving the "RETURNS" clause are
present. LINE NUMBER=16. SQLSTATE=42613
ALTER MODULE corpdata.module1
ADD FUNCTION DEPTEMPLOYEES (DEPTNO CHAR(3))
RETURNS TABLE (EMPNO CHAR(6),
LASTNAME VARCHAR(15),
FIRSTNAME VARCHAR(12))
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN ATOMIC
RETURN
SELECT EMPNO, LASTNAME, FIRSTNME
FROM CORPDATA.EMPLOYEE
WHERE WORKDEPT = "DEPTEMPLOYEES".DEPTNO;
END
When I try this (removed RETURNS clause), I get
SQL0491N The CREATE FUNCTION or ALTER MODULE statement used to define
"CORPDATA.MODULE1.DEPTEMPLOYEES" must have a RETURNS clause, and one of: the
EXTERNAL clause (with other required keywords); an SQL function body; or the
SOURCE clause. LINE NUMBER=8. SQLSTATE=42601
ALTER MODULE corpdata.module1
ADD FUNCTION DEPTEMPLOYEES (DEPTNO CHAR(3))
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN ATOMIC
RETURN
SELECT EMPNO, LASTNAME, FIRSTNME
FROM CORPDATA.EMPLOYEE
WHERE WORKDEPT = "DEPTEMPLOYEES".DEPTNO;
END
When I try this (removed BEGIN ATOMIC), I get
SQL0104N An unexpected token "SELECT" was found following "INISTIC
RETURN ". Expected tokens may include: "(". LINE NUMBER=9. SQLSTATE=42601
:) Yes, it does say "INISTIC".
ALTER MODULE corpdata.module1
ADD FUNCTION DEPTEMPLOYEES (DEPTNO CHAR(3))
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
RETURN
SELECT EMPNO, LASTNAME, FIRSTNME
FROM CORPDATA.EMPLOYEE
WHERE WORKDEPT = "DEPTEMPLOYEES".DEPTNO
It appears that DB2 LUW as at version 11.1 does not yet fully support table-functions inside modules unless that table-function includes the PIPE statement. This is despite the published documentation suggesting that it's possible with some restrictions. This is the reason you get the "conflicting keywords" error, since a pipelined function can only return one row at a time which is the opposite of RETURNS TABLE.
Check also if implementing a pipelined function might satisfy your requirements in this area.
When modules arrived at Db2 V9.7 they did not support table functions in modules at all, but since V10.1 there appeared to be some support for module table-functions, although the documentation was vague, lacking worked examples and the samples were not updated specifically for this.
There's a reference to this limitation on developerworks dating from 2014.
If this is important to your company, consider opening a request for enhancement (RFE), google for details.
You might also want to submit a documentation remark on the Db2 Knowledge Centre page for alter-module, and also on the restrictions-on-modules page, which do not mention the additional restriction on table-function inside modules regarding the use of the PIPE statement.

Unable to create a table in SQL

I am trying to execute this simple query.
create Table test1
{
ID int identity(1,1),
value nvarchar
}
Its throwing an error as
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '{'
I am stuck here. Please help me out of this
Use () instead of {}
create Table test1
(
ID int identity(1,1),
value nvarchar
)
don't use follow brace. you should use parenthesis.
create Table test1
(
ID int identity(1,1),
value nvarchar
)
Syntax for create table:
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
Use parenthesis ( ) instead of curly braces { }
Set data size for the nvarchar. other wise when insert any string value more than 1 character you will receive String or binary data would be truncated. error
Not mandatory but adding NOT NULL to the IDENTITY column is good practice.
Adding schema name to the table is good practice. (By default dbo is the schema)
So the working query will be:
create Table dbo.test1
(
ID int identity (1, 1) NOT NULL,
value nvarchar (500)
)
Use () instead of {}
Check out this link for the example SQL Create Table
On the internet about the topic you will find many useful information.

invalid identifier in WHERE clause

I'm trying to write the SQL to copy a row from one table to another but I keep getting an invalid identifier in the WHERE clause. I'm using oracle apex.
Here is my code:
INSERT INTO CRIMECLOSED (crimeClosedID, crimeName, crimeDate, crimeNotes,
outsideSourceDescription, dateClosed, relatedCrimes,
staffID, crimeTypeID, locationID)
SELECT CRIMEOPEN.crimeOpenID, CRIMEOPEN.crimeName, CRIMEOPEN.crimeDate,
CRIMEOPEN.crimeNotes, CRIMEOPEN.outsideSourceDescription, CURDATE(),
CRIMEOPEN.relatedCrimes, CRIMEOPEN.staffID, CRIMEOPEN.crimeTypeID,
CRIMEOPEN.locationID
FROM CRIMEOPEN
WHERE CRIMEOPEN.crimeOpenID = '1';
CRIMEOPEN table
CREATE TABLE "CRIMEOPEN"
( "crimeOpenID" VARCHAR2(5),
"crimeName" VARCHAR2(20),
"crimeDate" DATE,
"crimeNotes" VARCHAR2(200),
"outsideSourceDescription" VARCHAR2(200),
"relatedCrimes" VARCHAR2(5),
"staffID" VARCHAR2(5),
"crimeTypeID" VARCHAR2(5),
"locationID" VARCHAR2(5),
CONSTRAINT "CRIMEOPEN_PK" PRIMARY KEY ("crimeOpenID") ENABLE
)
The error I get is:
ORA-00904: "CRIMEOPEN"."CRIMEOPENID": invalid identifier
I think the error is trying to say that 'crimeOpenID' is not a column in 'CRIMEOPEN' but it is.
Any help please?
If you use double quotes when declaring the columns of the table then the same format needs to be used also when "using" the columns in various statements.
So in order to fix the issue you should wrap all columns in double quotes in the select statement (something like below - applied only to the select statement).
INSERT INTO CRIMECLOSED (crimeClosedID, crimeName, crimeDate, crimeNotes,
outsideSourceDescription, dateClosed, relatedCrimes,
staffID, crimeTypeID, locationID)
SELECT "CRIMEOPEN"."crimeOpenID", "CRIMEOPEN"."crimeName", "CRIMEOPEN"."crimeDate",
"CRIMEOPEN"."crimeNotes", "CRIMEOPEN"."outsideSourceDescription", CURDATE(),
"CRIMEOPEN"."relatedCrimes", "CRIMEOPEN"."staffID", "CRIMEOPEN"."crimeTypeID",
"CRIMEOPEN"."locationID"
FROM "CRIMEOPEN"
WHERE "CRIMEOPEN"."crimeOpenID" = '1';

mysql syntax error for timestamp

I have this piece of SQL that is being fed to Mysql.
CREATE TABLE pn_history(
member INT,
action INT,
with INT,
timestamp DATETIME,
details VARCHAR(256)
)
But is comes back as an error about the syntax.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'with INT,
timestamp DATETIME,
details VARCHAR(256)
)' at line 4
Why is this failing?
Both 'with' and 'timestamp' are reserved words in MySQL. So to get this to work, you'd need to escape each one:
CREATE TABLE pn_history(
member INT,
action INT,
`with` INT,
`timestamp` DATETIME,
details VARCHAR(256)
)
Really though, you need to consider changing the names of your columns identifiers.
Read more about MySQL Reserved Words.
EDIT: Actually, TIMESTAMP is not a reserved word. The documentation says:
MySQL allows some keywords to be used
as unquoted identifiers because many
people previously used them. Examples
are those in the following list:
ACTION
BIT
DATE
ENUM
NO
TEXT
`TIME
TIMESTAMP
So I guess that means peer pressure took TIMESTAMP off the reserved word list. Hah!
The problem is the name of the with column. Change the name into something like withValue.
CREATE TABLE pn_history(
member INT,
action INT,
withValue INT,
timestamp DATETIME,
details VARCHAR(256)
)
timestamp is a keyword (it is a data type in mysql) which may be causing you problems.
I would suggest using a different name, but if it must be named timestamp, try using backticks to quote it.