Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
What is wrong in my sql statement ?
Message
 
 
À
01/08/2005 05:53:23
Stanley Yip
Shun Sang (technology) ltd Hong Kong
Hong Kong, Hong Kong
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01037424
Message ID:
01037430
Vues:
26
A local variable cannot be used as a table name. You can use dynamic sql
DECLARE @Sql nvarchar(4000)
SET @Sql = N'SELECT Count(*) AS ''Count'', Sum(bo6_audits.epoints) AS ''Sum'', bo6_audits.PL
FROM ' + @full_table_name + 
'WHERE (bo6_audits.record_period=''7/2005'') 
  AND (bo6_audits.trans_desc Not Like ''%promotion%'' And bo6_audits.trans_desc Not Like ''%platinum%'' 
  And bo6_audits.trans_desc Not Like ''%adjustment%'' And bo6_audits.trans_desc Not Like ''%bonus%'') 
  AND (bo6_audits.PL Is Not Null)
GROUP BY bo6_audits.PL
ORDER BY bo6_audits.PL'
EXEC(@sql)
Notice that single quotes have to be doubled inside a string litera enclosed in the single quotes.

>DECLARE @full_table_name nvarchar(769)
>
>
>SEt @full_table_name = 'hp_200507_01.dbo.bo6_audits'
>
>SELECT Count(*) AS 'Count', Sum(bo6_audits.epoints) AS 'Sum', bo6_audits.PL
>FROM @full_table_name
>WHERE (bo6_audits.record_period='7/2005') AND (bo6_audits.trans_desc Not Like '%promotion%' And bo6_audits.trans_desc Not Like '%platinum%' And bo6_audits.trans_desc Not Like '%adjustment%' And bo6_audits.trans_desc Not Like '%bonus%') AND (bo6_audits.PL Is Not Null)
>GROUP BY bo6_audits.PL
>ORDER BY bo6_audits.PL
>
>prompt "Must declare the variable '@full_table_name'."
>
>How to solve it ?
--sb--
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform