Unexpected token: CASE in statement [CASE] - hsqldb

CREATE TEXT TABLE IF NOT EXISTS PPE_COMMON_LOG (LOG_ID int PRIMARY KEY,COMP_NAME varchar(50),LOG_EVENT varchar(50),LOG_LEVEL varchar(30),LOG_MSG varchar(200),LOG_DATETIME datetime,LOG_ERROR_CODE varchar(80) );
Select COUNT(*) as "TCOUNT" From INFORMATION_SCHEMA.SYSTEM_TABLES Where TABLE_NAME = 'PPE_COMMON_LOG';
CASE TCOUNT WHEN 0 then
SET TABLE PPE_COMMON_LOG SOURCE "PPE_COMMON_LOG.csv;ignore_first=true;all_quoted=true";
SET TABLE PPE_COMMON_LOG SOURCE HEADER "LOG_ID,COMP_NAME,LOG_EVENT,LOG_LEVEL,LOG_MSG,LOG_DATETIME,LOG_ERROR_CODE";
END CASE;
It gives "Unexpected token: CASE in statement [CASE]"

The blocks with CASE TCOUNT WHEN 0 is incorrect. You cannot write a CASE statement in SQL to execute other SQL statements.
If you want to execute the SET TABLE statements only in some situations depending on what exists in INFORMATION_SCHEMA, then execute the SELECT in Java and check the return count and execute the other SQL statements depending on the return count.

Related

How to get row count of select statement inside stored procedure in SQL Server

I have requirement where I want get row count of select statement inside stored procedure and if row count is equal to 0 then only it will execute the next statement
select A, B, C
from AT
where B = 1
If the above statement returns any rows, then it will not execute further but if this statement do not have any row then it will execute next statement. I have tried it using in two ways
##rowcount - it's not working properly
Using temp table by inserting select statement into table getting row count of table but using temp table is not optimize way
Is there any solution?
You could use IF NOT EXISTS:
IF NOT EXISTS (select A,B,C from AT where B=1)
BEGIN
-- sth
END
is there any solution like getting into variable without hitting to database again and again
DECLARE #my_rowcount INT;
select A,B,C from AT where B=1;
SET #my_rowcount = ##ROWCOUNT; -- immediately after select get ##ROWCOUNT
...
IF #my_rowcount = 0
BEGIN
-- sth
END
EDIT:
##ROWCOUNT Globle variable for database so it may return wrong Value if any other select statement processed in other sp in same databe
Nope. ##ROWCOUNT:
Returns the number of rows affected by the last statement.
You could easily check it with your SSMS(open 2 tabs, select 1 and 2 rows on each of them and then get ##ROWCOUNT respectively).

Update or otherwise create an entry in table

I want to update or create a new entry in a table with normal SQL (not MySQL).
My table has columns: T_ID, ITERATION, COMMENT
I know that I can update my table with:
UPDATE TABLE
SET ITERATION = ITERATION + 1
WHERE T_ID = 1;
But if no entry with T_ID = 1 exists I want to create an entry with ITERATION = 1 and T_ID = 1 and as COMMENT nothing.
Is this possible by using a normal SQL statement?
Is this possible by using a normal SQL statement?
No - there is no "standard" SQL construct for an "upsert" (or Merge) in one statement. Different SQL systems have implemented mechanism to perform an "update if exists" operation, such as MySQL's ON DUPLICATE KEY UPDATE or Oracle and SQL Server's MERGE syntax, but nothing in "standard" SQL.
You would want to do it in a stored procedure. Something like this where you're checking to see if anything exists where T_ID = 1. Then based off that conclusion you'll either update or insert.
BEGIN TRAN
IF EXISTS (SELECT * FROM table WHERE T_ID = 1)
BEGIN
UPDATE table SET ITERATION = ITERATION + 1 WHERE T_ID = 1
END
ELSE
BEGIN
INSERT INTO table (T_ID, ITERATION, COMMENT)
VALUES (1, 1, '')
END
COMMIT TRAN

sql server queries related to case statement

My query like this
case when statement1 = statement2 then offer1
if offer1 is have value means then i need to display offer1 value will be 'Yes'
How to write the query for this?
You can nest multiple CASE expressions like so:
CASE
WHEN statement1 = statement2
THEN
CASE WHEN offer1 IS NOT NULL THEN 'Yes' ELSE ... END
END
You can use Stored procedures and return a value depending on the conditions you need, in the stored procedures you can design your conditions using normal if statements, take a look at this example from here:
Create procedure dbo.Prc
#Value varchar(50),
#Result bit OUTPUT
AS
Begin
If exists (select 1 from YourTable where Field=#Value)
set #Result=1
Else
set #Result=0
End

DB2 SQL - Cannot successfully insert into table after trigger has been made

I have been running into problems inserting values into a table after having a trigger attached to it.
The trigger is the following:
CREATE TRIGGER trig
AFTER INSERT ON Follows
REFERENCING NEW as N
FOR EACH row
WHEN ((Select email from Celebrity) <> N.followed_email)
UPDATE followSuggestion SET followSuggestion.Suggested_follower = N.followed_email, followSuggestion.Suggested_followee = N.follower_email
;
the insert code is the following:
INSERT INTO follows
VALUES('Michael_Phelps#uss.net','Michael_Phelps#uss.net');
And the error is as follows
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0811N The result of a scalar fullselect, SELECT INTO statement, or VALUES
INTO statement is more than one row. SQLSTATE=21000
SQL0811N The result of a scalar fullselect, SELECT INTO statement, or VALUES INTO statement is more than one row.
Thank you in advanced!
I think this is the problem:
(Select email from Celebrity) <> N.followed_email
The subquery will probably return more than one row so the scalar operator <> will not work.
You will have to rephrase that condition to something like:
WHEN ((Select COUNT(email) from Celebrity WHERE email = N.followed_email) = 0) ...

How to use truncate table and insert into table in user define function for SQL Server 2005?

I'm trying to use a simple function in SQL server 2005 and it generates an error.
CREATE FUNCTION [dbo].[fn_data_stat_cc_update]
(
#result char
)
RETURNS char(1)
AS
BEGIN
truncate table stg_data_cancer_center;
INSERT INTO stg_data_cancer_center(
mrn, pt_city, pt_state, pt_zip, pt_pri_dx, clinic, source
SELECT rtrim(ltrim(mrn)) as mrn,
pat_city, pat_state, zipcode, exact_icd9_code_pri_dx, 'clinic' =
case
when inst_id = 1 then 'CANRAD'
when inst_id = 2 then 'CANHAM'
else #result--''
end, 'MOSAIQ' as Source
from stg_mosaiq_patient;
RETURN LTRIM(RTRIM(#result))
END
Errors are:
Procedure fn_data_stat_cc_update, Line 18
Invalid use of side-effecting or time-dependent operator in 'TRUNCATE TABLE' within a function.
Procedure fn_data_stat_cc_update, Line 25
Invalid use of side-effecting or time-dependent operator in 'INSERT' within a function.
You can't.
You need to use a stored procedure. Functions are not allowed to have side effects such as modifying data.