943,102 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 733
  • MS SQL RSS
Feb 9th, 2010
0

incorporate the UDF functionality into the Sp

Expand Post »
Good Day All

I have an Sp and UDF. the UDF cant take #tables. so i want to incorporate the functionality that is being provided by the udf and make it part of the code. the first part of my statement creates a solid table that is being used in the UDF

MS SQL Syntax (Toggle Plain Text)
  1. TRUNCATE TABLE temp
  2. INSERT INTO temp
  3. SELECT MTN.ID,S.DESCR,ISNULL(MTN.CYCLETEMPLATE,C.CYCLES) AS CYCLETEMPLATE,MTN.ACTV AS [ACTV]
  4. FROM TBL_STAFF S
  5. INNER JOIN MTM_ACTV_STAFF MTN ON
  6. S.ID = MTN.STAFF
  7. LEFT OUTER JOIN MTM_ACTV_STAFF_CYCLE C
  8. ON C.IDL = MTN.ID
  9. END
  10. ELSE
  11. BEGIN
  12. SELECT MTN.ID,S.DESCR,ISNULL(MTN.CYCLETEMPLATE,C.CYCLES) AS CYCLETEMPLATE,MTN.ACTV AS [ACTV]
  13. INTO temp
  14. FROM TBL_STAFF S
  15. INNER JOIN MTM_ACTV_STAFF MTN ON
  16. S.ID = MTN.STAFF
  17. LEFT OUTER JOIN MTM_ACTV_STAFF_CYCLE C
  18. ON C.IDL = MTN.ID
  19. END

and later in my Sp i have this line of statement

MS SQL Syntax (Toggle Plain Text)
  1. SELECT DESCR, dbo.GetSortedString(Cast(NULL AS varchar(8000))) AS CycleIdList,ACTV
  2. INTO #TempSummary
  3. FROM temp (NOLOCK)
  4. GROUP BY DESCR,ACTV
  5. ORDER BY DESCR,ACTV


which has no problem and later i want to update the Filed in the #TempSummary table like this

MS SQL Syntax (Toggle Plain Text)
  1. UPDATE #TempSummary
  2. SET CycleIdList = dbo.Concat(#TempSummary.Descr,#TempSummary.actv)

now the problem is here, the Concat is the UDF. defined like this
MS SQL Syntax (Toggle Plain Text)
  1. CREATE FUNCTION [dbo].[Concat] (@Name varchar(50),@Actv int)
  2. RETURNS varchar(max)
  3. WITH EXECUTE AS CALLER
  4. AS
  5. BEGIN
  6. Declare @s varchar(max)
  7. SET @s = ''
  8.  
  9. SELECT @s = @s + IsNull(',' + Cast(Cycletemplate AS varchar), '')
  10. FROM temp (NOLOCK)
  11. WHERE temp.Descr = @Name AND temp.Actv = @Actv
  12. GROUP BY Cycletemplate
  13. ORDER BY Cycletemplate
  14. IF (@S IS NOT NULL) AND (@S <> '') SET @S = SubString(@s, 2, Len(@S)-1)
  15. RETURN @S
  16. END

now i want to incorporate the functionality of the UDF and get rid of it. The UDF use the Temp created in the First example code based on the two fields. and generate the string and update the table #TempSummary. How can i incorporate this functionality without using UDF.

Thanks
Last edited by vuyiswamb; Feb 9th, 2010 at 7:44 am.
Similar Threads
Reputation Points: 31
Solved Threads: 14
Posting Whiz
vuyiswamb is offline Offline
310 posts
since Mar 2007
Feb 12th, 2010
0
Re: incorporate the UDF functionality into the Sp
Maybe I'm not reading you correctly. Why can't you do this (take the logic from the concat UDF and put it above the UPDATE statement?:

MS SQL Syntax (Toggle Plain Text)
  1. DECLARE @s varchar(max)
  2. DECLARE @Name varchar(50)
  3. DECLARE @Actv int
  4.  
  5. SET @s = ''
  6.  
  7. SELECT @s = @s + IsNull(',' + Cast(Cycletemplate AS varchar), '')
  8. FROM temp (NOLOCK)
  9. WHERE temp.Descr = @Name AND temp.Actv = @Actv
  10. GROUP BY Cycletemplate
  11. ORDER BY Cycletemplate
  12. IF (@S IS NOT NULL) AND (@S <> '') SET @S = SubString(@s, 2, Len(@S)-1)
  13.  
  14. UPDATE #TempSummary
  15. SET CycleIdList = @S
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Feb 16th, 2010
0
Re: incorporate the UDF functionality into the Sp
Thank you this is Perfect
Reputation Points: 31
Solved Threads: 14
Posting Whiz
vuyiswamb is offline Offline
310 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Zero in Bigint/Numeric/Int
Next Thread in MS SQL Forum Timeline: INSERT problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC