Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Syntax for IF Else

Author  Topic 

msvdm
Starting Member

17 Posts

Posted - 2011-07-29 : 17:26:01
there are 3 statements and for each statment there is a join associated based on a few conditions ...Please tell me the syntax so that performance is improved...Thanks in advance

if p!=null and t!=null
look into table A by
(join starts here)
condition1
condition2
condition3
p=null and t!=null
look into table A by
(join starts here)
condition1
condition2
condition3
p!=null and t!=null
look into table A by
(join starts here)
condition1
condition2
condition3
if p=null and t=null
(join starts here)
look into table A by
condition1
condition2
condition3

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-07-29 : 17:33:48
Do one insert for each case? Do not join dynamically.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

msvdm
Starting Member

17 Posts

Posted - 2011-07-29 : 21:23:24
Thanks for your valuable reply

Ya ...but we have several million records and we need to add this loop(may be If else) into the exisisting code.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-07-30 : 01:43:40
cant you try using case when for resolving this?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-07-30 : 11:30:57
>> there are 3 statements AND for each statement there is a JOIN associated based on a few conditions ...<<

Really? Where? All I see is some invented language AND no SQL. OH, did you know that
1. SQL uses <> AND not != in your personal language
2. that “<expr> <comp op> NULL” is always UNKNOWN. That is covered in the first two weeks of SQL classes.
3. In SQL we have a CASE expression AND avoid using IF-THEN-ELSE statements. It is like cannibalism – you might do it, but not in public where people can see you.
4. There is no LOOK_INTO operator in SQL.
5. JOIN requires two tables; where is the other one?

Every SQL forum or newsgroup expects that you will post DDL, sample data AND clear specifications which might might include output. This is explained in the FAQ section. We can not read minds; what would we need to know to do your job for you?

Tables must have keys AND should have DRI, constraints, AND all the basic features of a schema. You should know use ISO-8601 Standards for temporal data, avoid needless dialect, basic data modeling AND use ISO-11179 Standards for data element names. Please tell us IF you can change the DDL.

Want to try again, but with something that we can actually read?

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -