Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
What is wrong in my sql statement ?
Message
 
 
To
01/08/2005 05:53:23
Stanley Yip
Shun Sang (technology) ltd Hong Kong
Hong Kong, Hong Kong
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01037424
Message ID:
01037430
Views:
24
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--
Previous
Reply
Map
View

Click here to load this message in the networking platform