Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
skybvi
Posting Yak Master
193 Posts |
Posted - 2011-12-14 : 12:04:03
|
my t sql---------------declare @xl1 table (Sales_Amount nvarchar (10),Department char(31),Dept_Margin int);Declare @dt1 datetime, @dt2 datetimeset @dt1 = dateadd(d,-7,GETDATE())set @dt2 = dateadd(d,-1,GETDATE()) Select SUM(SLS_AMT) as Sales_Amount,c.DEP_DESCR as Department,--sum(b.RTL_PRC),--sum(b.COST_CASE_PRC),sum((b.RTL_PRC-b.COST_CASE_PRC))/sum(NULLIF(b.RTL_PRC,0))*100 as Dept_Margininto @xl1from DAY_PLU_SALES_1 a joinPLU_1 bon a.ITM_ID = b.ITM_IDleft join DEP_1 c ONb.STR_HIER_ID=c.STR_HIER_IDwhere DT >= @dt1AND DT <= @dt2group by c.DEP_DESCRorder by c.DEP_DESCRselect * from @xl1----------------ERROR---Incorrect syntax near '@xl1'.What's the error.Regards,SushantDBAWest Indies |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2011-12-14 : 12:11:49
|
The SELECT INTO statement creates a new table and populates it with the result set of the SELECT statement. Your table already exists. Also, you can specify a permanent table or a temp table but you cannot specify a table variable; hence the syntax error.=======================================Faced with the choice between changing one's mind and proving that there is no need to do so, almost everyone gets busy on the proof. -John Kenneth Galbraith |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-12-14 : 12:19:43
|
You can do thisdeclare @xl1 table (Sales_Amount nvarchar (10),Department char(31),Dept_Margin int);Declare @dt1 datetime,@dt2 datetimeset @dt1 = dateadd(d,-7,GETDATE())set @dt2 = dateadd(d,-1,GETDATE())insert into @xl1Select SUM(SLS_AMT) as Sales_Amount,-- but your @xl1 is nvarchar!c.DEP_DESCR as Department,--sum(b.RTL_PRC),--sum(b.COST_CASE_PRC),sum((b.RTL_PRC-b.COST_CASE_PRC))/sum(NULLIF(b.RTL_PRC,0))*100 as Dept_Marginfrom DAY_PLU_SALES_1 a joinPLU_1 bon a.ITM_ID = b.ITM_IDleft join DEP_1 c ONb.STR_HIER_ID=c.STR_HIER_IDwhere DT >= @dt1AND DT <= @dt2group by c.DEP_DESCRorder by c.DEP_DESCRselect * from @xl1JimEveryday I learn something that somebody else already knew |
|
|
skybvi
Posting Yak Master
193 Posts |
Posted - 2011-12-14 : 12:26:42
|
Ok, i Changed it to #xl1 and it worked nicely.My further concern is that i want to put similarily 6 more tables for ex #xl1,#xl2...#xl6And I want to see the output of all tables in an excel file in different sheets.FOr ex #xl1 -sheet1#xl2 -sheet2...#xl6 -sheet6I know it would require SSISSo i would first put step 1 as t sql (to get all 6 tables)and then SSIS package as step 2Which tasks would give teh correct output in the excel sheets.?Thanks.Regards,SushantDBAVirgin Islands(U.K) |
|
|
|
|
|
|
|