Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SELECT x AS 'y'
Message
 
À
03/02/2009 12:54:36
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Divers
Thread ID:
01379000
Message ID:
01379003
Vues:
22
>I bet Sergey can figure out a way to do this!
>
>If I have the following SQL statement, it works fine:
>
>SELECT CodesKey AS 'codesdeptkey', * from codes
>
>I'd like to parameterize this, but if I try this it gives an incorrect syntax error:
>
>DECLARE @KeyName varchar(50)
>SET @KeyName = 'codesdeptkey'
>SELECT CodesKey AS @KeyName, * from codes
>
>Any ideas??? (other than obvious setting a string to the whole SELECT string and then EXECing that ... the SELECT string is obviously more complicated than what I've indicated above).
>
>TIA,
>~~Bonnie

Unfortunately you should use so called Dynamic SQL for this:
DECLARE @KeyName varchar(50)
SET @KeyName = 'codesdeptkey'
DECLARE @sql nvarchar(max) -- for SQL 2005 or 2008 (or (4000) for 2000)
SET @sql = 'SELECT CodesKey AS '+ @KeyName + ', * from codes'
EXEC (@sql)
--- If @sql is larger than 4000 chars you should use this:
--- EXEC sp_executesql @sql
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
Répondre
Fil
Voir

Click here to load this message in the networking platform