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 |
|
ganeshkumar08
Posting Yak Master
187 Posts |
Posted - 2008-08-11 : 04:57:19
|
| Hi All,This is my codeALTER FUNCTION [dbo].[fn_GetTabSelectionInfo]( @SessionID Int)RETURNS @Results TABLE (MatchID Int,ClubID Int,PlayerID Int,MatchDate Varchar(50))ASBEGIN-- Declare @SessionID Int-- Set @SessionID = 11 Declare @QueryStr Varchar(1000) Set @QueryStr = ' ' IF Exists(Select 'T' From TrendSELECTED_COMPETITIONS Where SessionID=@SessionID) Set @QueryStr=@QueryStr + ' Inner Join TrendSELECTED_COMPETITIONS TSC on TSC.CompetitionID=VMI.CompetitionID and TSC.SessionID= ' + cast(@SessionID as varchar) IF Exists(Select 'T' From TrendSELECTED_Matches Where SessionID=@SessionID) Set @QueryStr=@QueryStr + ' Inner Join TrendSELECTED_Matches TSM on TSM.MatchID=VMI.MatchID and TSM.SessionID= ' + cast(@SessionID as varchar) IF Exists(Select 'T' From TrendSELECTED_TEAMS Where SessionID=@SessionID) Set @QueryStr=@QueryStr + ' Inner Join TrendSELECTED_TEAMS TST on TST.ClubID=VMI.ClubID and TST.SessionID= ' + cast(@SessionID as varchar) IF Exists(Select 'T' From TrendSELECTED_PLAYERS Where SessionID=@SessionID) Set @QueryStr=@QueryStr + ' Inner Join TrendSELECTED_PLAYERS TSP on TSP.PlayerID=VMI.PLayerID and TSP.SessionID=' + cast(@SessionID as varchar) IF Exists(Select 'T' From TrendSELECTED_POSITIONS Where SessionID=@SessionID) Set @QueryStr=@QueryStr + ' Inner Join view_MatchPositionsInfoComplete VMP On VMP.PlayerID=VMI.PlayerID ' + ' Inner Join TrendSELECTED_Positions TSP on TSP.PositionID=VMP.PositionID and TSP.SessionID= ' + cast(@SessionID as varchar) Insert into @results EXEC('Select Distinct VMI.MatchID,VMI.ClubID,VMI.PlayerID,VMI.Date From view_MatchInfoMoreComplete VMI ' + @QueryStr) RETURN EndIf i execute the above function i got this errorMsg 443, Level 16, State 14, Procedure fn_GetTabSelectionInfo, Line 35Invalid use of side-effecting or time-dependent operator in 'EXECUTE STRING' within a function.My doubt is cann we use Dynamic query execution in functionsThanksGaneshSolutions are easy. Understanding the problem, now, that's the hard part |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-08-11 : 05:06:48
|
| You cant use dynamic sql inside a function. Make use of stored procedure insteadMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-11 : 05:21:07
|
You probably want this i guess. no need of dynamic querrySelect Distinct VMI.MatchID,VMI.ClubID,VMI.PlayerID,VMI.DateFrom view_MatchInfoMoreComplete VMIleft Join TrendSELECTED_COMPETITIONS TSC on TSC.CompetitionID=VMI.CompetitionID and TSC.SessionID=@SessionID left Join TrendSELECTED_Matches TSM on TSM.MatchID=VMI.MatchID and TSM.SessionID=@SessionID left Join TrendSELECTED_TEAMS TST on TST.ClubID=VMI.ClubID and TST.SessionID=@SessionID left Join TrendSELECTED_PLAYERS TSP on TSP.PlayerID=VMI.PLayerID and TSP.SessionID=@SessionID Left Join TrendSELECTED_Positions TSP on TSP.PositionID=VMP.PositionID and TSP.SessionID=@SessionID ... |
 |
|
|
ganeshkumar08
Posting Yak Master
187 Posts |
Posted - 2008-08-11 : 05:58:15
|
| thanks for your responseSolutions are easy. Understanding the problem, now, that's the hard part |
 |
|
|
|
|
|
|
|