Allowing a null parameter as an argument in a SQL Select - sql-server-2008

I have a table that has a foreign key to itself to keep track of ParentID. This is used to make a treelike hierarchy in the database. I'm trying to create a function that will find all rows that have a particular ParentID. However, if a record is a Root record it's parent is NULL. Therefore if I want to find all root tables I need to be able to enter NULL as an option as well. What I have so far is:
SELECT * FROM X.Y
WHERE
CASE
WHEN #ParentID IS NULL THEN ParentID IS NULL
WHEN #ParentID IS NOT NULL THEN ParentID = #ParentID
END
I think I'm on the right track, but haven't quite figured it out. It is saying Incorrect syntax near the keyword 'IS' indicating the IS in THEN ParentID IS NULL.
Any help would be great.

You're close. Try this:
SELECT * FROM X.Y
WHERE (#ParentID IS NULL AND ParentID IS NULL )
OR (ParentID = #ParentID )
About the first clause in my WHERE statement: any comparison to a NULL or a field whose value is NULL will always return false. Even WHERE NULL = NULL will evaluate as false.
So if you want to say, "Do this if both my variable and the field itself are null" you need to use something like WHERE (#ParentID IS NULL AND ParentID IS NULL ).
Some people like to do something like this:
WHERE (ISNULL(#ParentID,0) = ISNULL(ParentID,0)
It's tidy, but it doesn't allow the query engine to take advantages of the indexes that you might have on your table. It's considered a bad idea.

Try replacing the second WHEN to an ELSE or simply using an or like this
SELECT * FROM X.Y
WHERE ParentID IS NULL OR ParentID = #ParentID

Related

Choose which field based on IFNULL

I have the following query which I am trying to run in MySQL. Basically I am trying to work out how to update the corresponding field which isn't null. I know its going to be one of two possible fields but I want to update the one which isn't empty. Maybe I am missing something blindingly obvious, but this is what I've tried thus far:
UPDATE `table`
#SET IF(FieldA IS NULL,FieldB,FieldA) = 1234
#SET IFNULL(FieldA,FieldB) = 1234
WHERE `FieldC` = '5678'
AND (
`FieldA` = '1234'
OR `FieldB` = '1234'
)
I suspect there may be a CASE solution but I'd prefer a shorthand/simple version option if it exists.
(My)SQL has no syntax that allows you to dynamically change the column you want to update, e.g. something like update ... set {PickFieldBaseOnCondition:FieldA|FieldB} = 1234. You have to specify a column there. The moment you start your query, the basic structure of the query (and all fields involved) have to be clear and fixed, only the values can change.
So you need to update both fields in your query if it shall be able to modify two different columns. But you can of course decide to just not modify the content of a field based on its content, e.g. keep it if it is null already:
update `table`
set FieldA = IF(FieldA IS NOT null, 1234, FieldA),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
Note that the requirement "update the other field if a field is null" only works if your initial condition that one field is null and one is not null is fulfilled, which you said is given. Otherwise, you should include a test if both fields are null or both fields are not null (in the comparison for the first field), which you could e.g. do with
update `table`
set FieldA = IF(FieldB IS null, 1234, null),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
FieldA can now be changed from content to null if both fields are not null (and from null to content if both fields are null), to enforce the condition that exactly one field is not null.
Please also note that IF() is a MySQL-only shorthand for CASE and doesn't work in all databases. You prefered a non-case solution, but it can trivially be rewritten using the sql-standard CASE.

MySql Basic table creation/handing

I'm trying to create a simple table where I insert field and I do some checks in MySql. I've used Microsoft SQL relatively easy. Instead, MySql give evrrytime query errors without even specifying what's going on. Poor MySql software design apart, here's what I'm trying to do:
1 table with 4 fields with an autoincremental autogenerated number to det an ID as primary key
CREATE TABLE `my_db`.`Patients_table` (
`ID_Patient` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`Patient_name` VARCHAR( 200 ) NOT NULL ,
`Recovery_Date` DATETIME NOT NULL ,
`Recovery_count` INT NOT NULL
) ENGINE = MYISAM
a simple stored procedure to insert such fields and check if something exist before inserting:
CREATE PROCEDURE nameInsert(IN nome, IN data)
INSERT INTO Patients_table (Patient_name,Recovery_Date) values (nome,data)
IF (EXISTS (SELECT Recovery_count FROM Tabella_nomi) = 0) THEN
INSERT INTO (Patients_table (Recovery_count)
ELSE
SET Recovery_count = select Recovery_count+1 from Patients_table
END
this seems wrong on many levels and MySQL useless syntax checker does not help.
How can I do this? Thanks.
There seems to be a lot wrong with this block of code. (No offense intended!)
First, Procedures need to be wrapped with BEGIN and END:
CREATE PROCEDURE nameInsert(IN nome, IN data)
BEGIN
...[actually do stuff here]
END
Second, since your table is declared with all fields as NOT NULL, you must insert all fields with an INSERT statement (this includes the Recovery_Date column, and excludes the AUTO_INCREMENT column). You can add DEFAULT CURRENT_TIMESTAMP to the date column if you want it to be set automatically.
INSERT INTO Patients_table (Patient_name,Recovery_Date) values (nome,data)
Third, what exactly is your IF predicate doing?
EXISTS (SELECT Recovery_count FROM Tabella_nomi) = 0
If you want to check if a row exists, don't put the = 0 at the end. Also, Tabella_nomi isn't declared anywhere in that procedure. Also, your SELECT statement should have a WHERE clause, since I'm assuming you want to select a specific row (this is going to select a result set of all recovery_counts).
Fourth, the second INSERT statement seems a little messy. It should look more like the first INSERT, and keep the point I made above in mind.
INSERT INTO (Patients_table (Recovery_count)
Fifth, the ELSE statement
SET Recovery_count = select Recovery_count+1 from Patients_table
Has some problems too. SET is meant for setting variables, not values in rows. I'm not 100% sure what your intent is from this statement, but it looks like you meant to increment the Recovery_count column of a certain row if it already exists. In which case, you meant to do something like this:
UPDATE Patients_table SET Recovery_count = Recovery_count+1 WHERE <conditional predicate>
Where the conditional predicate is something like this:
Patients_name = nome
Try these things, and look at the errors it gives you when you try to execute the CREATE STATEMENT. I bet they're more useful then you think!

How to select some default value if there is no such column in a table?

For example, I have a table that doesn't have a column "type". But I need to have my sql query having that column. When I run the query I get an error:
SELECT t.foo, t.boo, t.type FROM tabl AS t;
Unknown column 't.type' in 'field list'
I need something like ternary operator. I tried these solutions but they both do not work:
SELECT f.foo, f.boo, IF(f.type IS NULL, 'x', f.type) AS type FROM tabl AS f
SELECT f.foo, f.boo, (CASE WHEN f.type IS NULL THEN "x" ELSE f.type) AS type FROM tabl AS f
Is there a possibility to implement such a query?
Use something like this. Assume you want to join 2 tables rows and one is missing the column:
SELECT t.foo, t.boo, t.type FROM tabl1 as t1
UNION
SELECT t.foo, t.boo, NULL as type FROM tabl2 AS t2;
You can replace NULL with a string "" or whatever you application desires.
Unfortunately, that is not the way columns work. If you need to introspect your table to determine if it have this column, then you might try using data in the information_schema to get at this. Overall sounds like a weird approach to me. Why not just create all the tables with this column?
I think you mean that you have a table with entries ... Some of your entries has got no "type" column filled in. To have default value, you need to change your table. You can change it using either phpmyadmin (set default value) or through SQL code.
This would be something on these lines:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'London'
)
This sets each entry's city to be London by default

mysql update increment int field that is null

I have a very large table with two INT columns that are null on Default. This is a problem because since they are INT fields, it would help in many cases if they were originally set to 0.
So my questions are, is there a way I can UPDATE and INCREMENT(+1) these fields while they are like this (null on Default)? BTW.. I didn't have luck so far, it seems increment only works when the default=0
..or is my only option to Change the Default to none from null
UPDATE TableName SET column = IFNULL(column, 0) + 1 WHERE ...
More info on IFNULL. It returns the first argument if it is not NULL, the second otherwise.
Try setting the field as NOT NULL to get away with the problem so that default value of 0 is used instead of null. The other option is to set column as zero whenever it is null.
UPDATE TableName SET FieldName = '0' WHERE FieldName IS NULL
Other alternative would be to issue IFNULL to return 0 in case the column is null and then incrementing the column.
UPDATE TableName SET FieldName = IFNULL(FieldName,0)
The SQL standard would be to use COALESCE(); this has been available in MySQL since version 3.23 (which was released into production in 2001):
UPDATE mytable
SET mycolumn = COALESCE(mycolumn, 0) + 1
WHERE my_other_columns = ...
I can't see any reason to choose IFNULL() over COALESCE() here.
Hope this helps.

how do you handle a case when in a table you got a foreign key that cannot be null?

I have a case where I have a linq2sql dbml with 2 table in it
let simplify this by:
Table1
id int not null
fkid int not null (to table2.id)
and
Table2
id int not null
v1 bit not null
in the generated dbml, fkid is not the type nullable and by default it's value is 0
but in table2, I don't have an id 0
when i'm trying to insert a new record without touching fkid field, I'm getting an SQL insert error saying: 0 doesn't exist into table2
thing is I want to make to make my check fully generic so I cannot just do something like:
if column doesn't allow null check if value is 0 and if it's the case throw a custom error
I would like to do something like: if column is null then throw "my custom error"
how would you implement that?
since I'm using t4 template, I changed the line 80 in L2ST4.ttinclude from
if (type.IsValueType && canBeNull)
to
if (type.IsValueType)
so it ALWAYS set nullable type everywhere in the generated file