Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Quick question - what is better parameter or Field value
Message
De
25/06/2008 03:29:44
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01326528
Message ID:
01326561
Vues:
9
This message has been marked as the solution to the initial question of the thread.
>Hi everybody,
>
>I'm just wondering what would be better in your opinion:
>
>SELECT Registration.*, Schools.SchoolPhone
>		FROM dbo.Registration LEFT JOIN Schools ON Registration.School = Schools.School
>AND Schools.SiteID = @SiteID
>        WHERE RegistrationID = @RegistrationID
>
>or
>
>SELECT Registration.*, Schools.SchoolPhone
>		FROM dbo.Registration LEFT JOIN Schools ON Registration.School = Schools.School
>AND Registration.SiteID = Schools.SiteID
>        WHERE RegistrationID = @RegistrationID
>
>In other words, is it better to use parameter or field value? In the first case I would need to adjust several pages to add an extra parameter.
>
>Thanks a lot in advance.
>
>On the second thought - why do I care for one record return? LOL.

It depends what you want. When you use variable and use it in JOIN you will filter joined table for only these records that match the variable. If you use Table Field in join you will filter matching fields no matter what value they have:
Registration:
SiteId    School
-------------------------------
1         1
2         1 
3         1 
4         2
5         2

Schools:
SiteId      School 
-------------------------------
1            1
2            1
So this:
SELECT *
       FROM Registration
LEFT JOIN Schools ON Registration.School = Schools.School AND
          Registration.SiteId = Schools.SiteId
will give you this:
Schools:
SiteId      School SiteId      School 
-------------------------------------
1            1       1          1
2            1       2          1
3            1      NULL      NULL 
4            2      NULL      NULL 
5            2      NULL      NULL 
But:
DECLARE @SiteId int 
SET @SiteId = 1
SELECT *
       FROM Registration
LEFT JOIN Schools ON Registration.School = Schools.School AND
          Schools.SiteId = @SiteId
will give you this:
Schools:
SiteId      School SiteId      School 
-------------------------------------
1            1       1          1
2            1      NULL      NULL 
3            1      NULL      NULL 
4            2      NULL      NULL 
5            2      NULL      NULL 
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform