Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
A little SQL brain teaser
Message
 
 
À
29/07/2015 02:46:07
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2005
Application:
Web
Divers
Thread ID:
01622621
Message ID:
01622628
Vues:
67
>I'm actually writing some questions for a T-SQL test, and I dusted off this example from the vault.
>
>This query and example (by itself) doesn't make much sense, but it does illustrate a subtle difference between 2 queries.
>
>If anyone wants to answer this, I ask one thing - don't paste the code and run it...see if you can figure out the results just by reading.
>
>Suppose I have this table...
>
>
>
>CREATE TABLE dbo.MaxTest  (Name varchar(50), PayRate decimal(14,4))
>
>insert into dbo.MaxTest values ('Kevin', 50),  
>                                ('Katy', 60) 
>
>
>
>Here are 2 code samples. What will happen in each of the two examples? (will I get an error, will I get a result, etc.)
>
>sample 1:
>
>
>DECLARE @MaxRate decimal(14,4)
>SET @MaxRate = (SELECT MAX(PayRate) from dbo.MaxTest GROUP BY Name)
>SELECT @MaxRate
>
>
>sample 2:
>
>
>DECLARE @MaxRate decimal(14,4)
>SELECT @MaxRate = MAX(PayRate) FROM dbo.MaxTest GROUP BY Name
>SELECT @MaxRate
>
The first sample will generate an error about subquery produced more than 1 row. The second sample will not generate an error but I can not say which result it will return since in both cases you have GROUP BY Name at the end. In the second case the result can be either of 2 values.
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