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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 what is wrong with this query

Author  Topic 

pmotewar
Yak Posting Veteran

62 Posts

Posted - 2009-05-12 : 01:22:12
SET @str='SELECT SM.Sector_Name,MAX(CP1.Port_Date) As Date,(Sum(CP3.Mkt_Value) / 431957) * 100 AS NetAsset ,Sum(CP3.Mkt_Value) AS MV,Sum(ISNULL(CP3.No_Of_Shares,0)) AS SH INTO #TempHolding
FROM T_COM_POT CP1 INNER JOIN T_COM_POT_DTLS1 CP2 ON CP1.Port_ID = CP2.Port_ID INNER JOIN T_COM_POT_DTLS2 CP3 ON CP2.Com_Pot_N_DTLS1_ID = CP3.Com_Pot_N_DTLS1_ID INNER JOIN T_FUND_MASTER FM ON CP1.Fund_ID = FM.Fund_Id
INNER JOIN T_COMPANY_MASTER CM ON CP2.Company_ID = CM.Company_Id INNER JOIN T_SECTOR_MASTER SM ON CM.Sector_Id = SM.Sector_Id INNER JOIN T_COMPANY_NATURE CN ON CP2.Com_Nature_ID = CN.Com_Nature_ID
INNER JOIN T_INSTRUMENT I ON CP2.Instrument_ID = I.Instrument_ID
INNER JOIN T_RATING R ON CP2.Rating_ID = R.Rating_ID
Where Month(CP1.Port_Date) = Month(''01/01/2009'') AND Year(CP1.Port_Date) = Year(''01/01/2009'') GROUP BY SM.Sector_Name,Month(CP1.Port_Date), Year(CP1.Port_Date) ORDER BY SM.Sector_Name'

exec(@str)

Pankaj

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-12 : 01:25:39
I don't know. You have the database, tables, data, and you can execute the query there. . .. you tell us what is the error message and then we can tell you what's wrong with it.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

pmotewar
Yak Posting Veteran

62 Posts

Posted - 2009-05-12 : 01:28:06
quote:
Originally posted by khtan

I don't know. You have the database, tables, data, and you can execute the query there. . .. you tell us what is the error message and then we can tell you what's wrong with it.


KH
[spoiler]Time is always against us[/spoiler]





ohhh sorry ,
i got following error

here i want to create temporary table through dynamic query.

Msg 208, Level 16, State 0, Procedure myproc, Line 17
Invalid object name '#TempHolding'.


Pankaj
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-12 : 01:39:32
the temporary table only exists within the scope of the dynamic query. It is automatically dropped when it is out of scope (out of the exec(@str) ).

1. Why are you using dynamic SQL ? I don't see any necessity in using it in your query here
2. You can create the temp table first before running the query


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-12 : 01:59:56
I spot two other errors.

1. Integer division
2. Unneccesary GROUP BY for date column.

SELECT SM.Sector_Name,MAX(CP1.Port_Date) As Date,Sum(CP3.Mkt_Value) / 4319.57 AS NetAsset ,Sum(CP3.Mkt_Value) AS MV,Sum(ISNULL(CP3.No_Of_Shares,0)) AS SH INTO #TempHolding
FROM T_COM_POT CP1 INNER JOIN T_COM_POT_DTLS1 CP2 ON CP1.Port_ID = CP2.Port_ID INNER JOIN T_COM_POT_DTLS2 CP3 ON CP2.Com_Pot_N_DTLS1_ID = CP3.Com_Pot_N_DTLS1_ID INNER JOIN T_FUND_MASTER FM ON CP1.Fund_ID = FM.Fund_Id
INNER JOIN T_COMPANY_MASTER CM ON CP2.Company_ID = CM.Company_Id INNER JOIN T_SECTOR_MASTER SM ON CM.Sector_Id = SM.Sector_Id INNER JOIN T_COMPANY_NATURE CN ON CP2.Com_Nature_ID = CN.Com_Nature_ID
INNER JOIN T_INSTRUMENT I ON CP2.Instrument_ID = I.Instrument_ID
INNER JOIN T_RATING R ON CP2.Rating_ID = R.Rating_ID
Where Month(CP1.Port_Date) = 1 AND Year(CP1.Port_Date) = 2009 GROUP BY SM.Sector_Name ORDER BY SM.Sector_Name


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

pmotewar
Yak Posting Veteran

62 Posts

Posted - 2009-05-12 : 02:10:06
quote:
Originally posted by khtan

the temporary table only exists within the scope of the dynamic query. It is automatically dropped when it is out of scope (out of the exec(@str) ).

1. Why are you using dynamic SQL ? I don't see any necessity in using it in your query here
2. You can create the temp table first before running the query


KH
[spoiler]Time is always against us[/spoiler]





Thanks

Pankaj
Go to Top of Page

pmotewar
Yak Posting Veteran

62 Posts

Posted - 2009-05-12 : 02:23:51
quote:
Originally posted by Peso

I spot two other errors.

1. Integer division
2. Unneccesary GROUP BY for date column.

SELECT SM.Sector_Name,MAX(CP1.Port_Date) As Date,Sum(CP3.Mkt_Value) / 4319.57 AS NetAsset ,Sum(CP3.Mkt_Value) AS MV,Sum(ISNULL(CP3.No_Of_Shares,0)) AS SH INTO #TempHolding
FROM T_COM_POT CP1 INNER JOIN T_COM_POT_DTLS1 CP2 ON CP1.Port_ID = CP2.Port_ID INNER JOIN T_COM_POT_DTLS2 CP3 ON CP2.Com_Pot_N_DTLS1_ID = CP3.Com_Pot_N_DTLS1_ID INNER JOIN T_FUND_MASTER FM ON CP1.Fund_ID = FM.Fund_Id
INNER JOIN T_COMPANY_MASTER CM ON CP2.Company_ID = CM.Company_Id INNER JOIN T_SECTOR_MASTER SM ON CM.Sector_Id = SM.Sector_Id INNER JOIN T_COMPANY_NATURE CN ON CP2.Com_Nature_ID = CN.Com_Nature_ID
INNER JOIN T_INSTRUMENT I ON CP2.Instrument_ID = I.Instrument_ID
INNER JOIN T_RATING R ON CP2.Rating_ID = R.Rating_ID
Where Month(CP1.Port_Date) = 1 AND Year(CP1.Port_Date) = 2009 GROUP BY SM.Sector_Name ORDER BY SM.Sector_Name


E 12°55'05.63"
N 56°04'39.26"





Hi , Thanks for correction,
but can you please tell me advantage of this query over old query. when i saw execution plan it shows me same cost ??

Pankaj
Go to Top of Page
   

- Advertisement -