July 11, 2003 at 1:45 am
im trying to code the following but not sure how to do it in sql (not sure on the syntax for the 'if' construct:
if (dob = NULL) then
insert
from [tablename]
else
insert * from [tablename]
end if
any hints anyone? thanx!
If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!
If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!
July 11, 2003 at 3:56 am
The construct is OK. You just have to replace the = NULL by IS NULL.
The difference between the two is that = NULL checks if the value is NULL and IS NULL checks if the variable was not set.
July 11, 2003 at 4:07 am
There is no "THEN" in the T-SQL IF..ELSE statement. Also if you need to do more than one statement in a conditional branch you need to wrap them with BEGIN..END
July 11, 2003 at 4:14 am
Do you mean BEGIN at the very beginning of the statement and END at the end or two lots of BEGIN and END for each condition?
If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!
If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!
July 11, 2003 at 4:24 am
Take the following:
insert * from tablename
Is there a clause which enables me to insert all fields but one,rather than specifiying each of the fields by name and omitting the one i dont want?
If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!
If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!
July 14, 2003 at 3:00 am
Try:
if (dob IS NULL)
BEGIN
insert
from [tablename]
END
else
BEGIN
insert * from [tablename]
END
Although in this case you don't need the BEGIN and END and there is only a single line of code in the IF statement.
-Ed
July 14, 2003 at 5:43 am
quote:
Take the following:insert * from tablename
Is there a clause which enables me to insert all fields but one,rather than specifiying each of the fields by name and omitting the one i dont want?
No there is not. Generaly, it is considered a best practice, never to use a SELECT * anywhere in code.
First of all, if you don't need all the data, you are using precious processing time and bandwidth by fetching data you do not need. This can be especially important if you have to send the data over a network to do processing away from the database server.
Additionally, numerous problems can arise like fields that are sent back in the wrong order and fields being added or deleted from a table.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply