How to use RAISEERROR statement? - tsql

This is something very basic, but I can't understand it, and the manual is not helping:
declare #rule int =
(select id from menu_availability_rules
where (daily_serving_start = null or
(daily_serving_start is null and null is null)) and
(daily_serving_end = null or
(daily_serving_end is null and null is null)) and
(weekly_service_off = 3 or
(weekly_service_off is null and 3 is null)) and
(one_time_service_off = null or
(one_time_service_off is null and null is null)));
print #rule;
-- syntax error here --\/
if (#rule is not null) raiseerror ('test error', 42, 42);
if #rule is not null
begin
delete from menu_availability
where menu_id = 5365 and rule_id = #rule
delete from menu_availability_rules
where (daily_serving_start = null or
(daily_serving_start is null and null is null)) and
(daily_serving_end = null or
(daily_serving_end is null and null is null)) and
(weekly_service_off = 3 or
(weekly_service_off is null and 3 is null)) and
(one_time_service_off = null or
(one_time_service_off is null and null is null))
and not exists
(select rule_id from menu_availability
where rule_id = #rule)
end
Why is it a syntax error? How would I write it? I need to throw error for debugging purposes, just to make sure the code reached the conditional branch.
I can just replace the raiseerror with select 1 / 0 and I will get what I need, but why can't I do it normally?

The correct name is RAISERROR.

Related

Postgres CASE expression then null

The below query is part of a select query which I have got in my application
where
case when o.tracking_id <> '0' then a.account_id is null
and
o.status = 'N'
else
(
o.tracking_id = '0'
and
o.status = 'N'
)
END
I am having hardtime to understand the below line
Can you please tell me what does this exactly mean ?
case when o.tracking_id <> '0' then a.account_id is null
I wouldn't use a CASE expression here but instead would use the following logic:
WHERE
(o.tracking_id <> '0' AND a.account_id IS NULL AND o.status = 'N')
OR
(o.tracking_id = '0' AND o.status = 'N')
The condition is written somewhat clumsily. It is equivalent to this more readable expression:
where (o.tracking_id = '0' or a.account is null)
and o.status = 'N';

how to make a query not depending on the conditions in where clause when they are null in PostgreSQL?

I'd like to make a query from which the result is not depending on the conditions in where clause when conditions are null while it is depending on the conditions when they are not null.
A query I made is
select * from mytable where (num_lot = :num_lot or :num_lot is null) and date_work between :date_start and :date_stop
When :num_lot is null, the result was not depending on the num_lot, which was what I wanted.
But :date_start and :date_stop was null, no rows were returned rather than not depending on :date_start and :date_stop.
SELECT * FROM mytable WHERE
num_lot=COALESCE(:num_lot,num_lot) AND
date_work BETWEEN COALESCE(:date_start,date_work) and COALESCE(:date_stop,date_work)
when the verified value is NULL it is replaced with the column value i.e. always true.
Use coalesce() to check if both :date_start and :date_stop are null:
select *
from mytable
where (num_lot = :num_lot or :num_lot is null)
and (date_work between :date_start and :date_stop or coalesce(:date_start, :date_stop) is null)
or:
select *
from mytable
where (num_lot = :num_lot or :num_lot is null)
and ((date_work between :date_start and :date_stop) or (:date_start is null and :date_stop is null))

Hive select outputs null values

I get the select output as null for the following Hive table.
Describe studentdetails;
clustername string
schemaname string
tablename string
primary_key map<string,int>
schooldata struct<alternate_aliases:string,application_deadline:bigint,application_deadline_early_action:string,application_deadline_early_decision:bigint,calendaring_system:string,fips_code:string,funding_type:string,gender_preference:string,iped_id:bigint,learning_environment:string,mascot:string,offers_open_admission:boolean,offers_rolling_admission:boolean,region:string,religious_affiliation:string,school_abbreviation:string,school_colors:string,school_locale:string,school_term:string,short_name:string,created_date:bigint,modified_date:bigint,percent_students_outof_state:float> from deserializer
deletedind boolean
truncatedind boolean
versionid bigint
select * from studentdetails limit 3;
Output :
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL NULL NULL
I have used the following properties while creating the table.
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ("ignore.malformed.json" = "true")
And the following properties while selecting the data.
SET hive.exec.compress.output=true;
SET io.seqfile.compression.type=BLOCK;
SET mapred.output.compression.codec=org.apache.hadoop.io.compress.GzipCodec;
ADD JAR s3://emr/hive/lib/hive-serde-1.0.jar;
Thank you for the comments, I have found the solution for this.
The issue was that the column name in my json file and the column name that i used while creating the table was different.
When i synched the column names between the Hive table and the Json File the issue was resolved.
Thanks & Regards,
Srivignesh KN

ISNULL usage with NULL database fields

I am trying to do something that should be fairly simple but ISNULL isn't doing what I thought it would.
Basically I have a stored procedure and I am expecting either PARAM1 OR PARAM2 to have a matching value in my table.
SELECT * FROM MyTable WITH (NOLOCK)
WHERE
field1 = ISNULL(#PARAM1 ,field1 )
AND
field2 = #PARAM2
This works fine until I have NULL fields in my row then it excludes those results. Is there a different method that can cater for this?
ISNULL replaces the first value with the second value, so only if your parameter #PARAM1 is NULL does it replace it with PARAM1. I assume you're not passing in NULL values, so that's probably not what you want. More likely you just want to say
WHERE
(Field1 = #PARAM1 OR Field1 IS NULL)
AND
Field2 = #Param2
I Suppose you could use ISNULL in this fashion too:
ISNULL(Field1, #PARAM1) = #PARAM1
null is special in sql. If you do any comparisons on a column any rows that have a null for that column will be excluded.
SELECT * FROM MyTable WITH (NOLOCK)
WHERE
(PARAM1 = #PARAM1 or PARAM1 is null)
AND
(PARAM2 = #PARAM2 or PARAM2 is null)
Use
field1 = ISNULL(#PARAM1, A_REPLACEMENT_VALUE_IF_PARAM1_IS_NULL)
This will evaluate to-
field1 = #PARAM1 if #PARAM1 IS NOT NULL.
field1 = A_REPLACEMENT_VALUE_IF_PARAM1_IS_NULL if #PARAM1 IS NULL
EDIT:
Try these:
--If you want to ignore the WHERE clause if PARAM1/2 is null
ISNULL(field1, DEFAULT_VALUE) = ISNULL(#PARAM1, ISNULL(field1, DEFAULT_VALUE))
OR
ISNULL(field2, DEFAULT_VALUE) = ISNULL(#PARAM2, ISNULL(field2, DEFAULT_VALUE))
OR
--To get all rows with field1/2 as PARAM1/2 and ignore everything else
field1 = #PARAM1 OR field2 = #PARAM2

SQL WHERE statement?

What should my WHERE clause be in a SQL Statement in which I want to return those rows where column A is null or column B is null, but not where both are null?
WHERE (ColA is NULL AND ColB is NOT NULL)
OR (ColB is NULL AND ColA is NOT NULL)
(A IS NULL OR B IS NULL) AND NOT (A IS NULL AND B IS NULL)