Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Quick question - what is better parameter or Field value
Message
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:
01326598
Vues:
12
In my case it doesn't matter because I only select one particular Registration Record (RegID). So, I decided to save one parameter (if I would use two parameters, I would have to change several places to add the second parameter).

>>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
>
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform