create a function in sql server and call from stroed proc

Deepak Ghule replied to Lokesh M at 04-Jul-08 01:40


Function in sql server like

 


Create    FUNCTION [dbo].[udfGetTempTable] (@List varchar(4000))
RETURNS @TempTable TABLE
(
TempValueInt varchar(500)
)
AS
BEGIN
DECLARE @Value varchar(50), @Pos int, @TempList varchar(4000)

SET @TempList = LTRIM(RTRIM(@List)) + ','
SET @Pos = CHARINDEX(',', @TempList, 1)
SET @Value= ''

IF @Pos < 1
BEGIN
INSERT @TempTable VALUES(@List)
END
ELSE
BEGIN
IF REPLACE(@TempList, ',', '') <> ''
BEGIN
WHILE @Pos > 0
BEGIN
SET @Value = LTRIM(RTRIM(LEFT(@TempList, @Pos - 1)))
IF @Value = ''
BEGIN
BREAK
END
SET @TempList = RIGHT(@TempList, LEN(@TempList) - @Pos)


INSERT @TempTable VALUES(@Value)
SET @Value = ''
SET @Pos = CHARINDEX(',', @TempList, 1)
END
END
END
Return
END

 


u can call it from sp like

 

create proc dbo.spTemp
(
@ClientId INT,
@fdate SmallDateTime,
@tdate SmallDateTime,
@JobStatus varchar(4000),
@Category varchar(4000)
-- @HandOverDetailsCategory Varchar(100)
)
as


  
   SELECT
   isnull(M_Client.Client_Name_System,'') as ClientName ,
   isnull(Job.Job_Name,'') as JobName,
   IsNull(Convert(varchar(10),Job.Job_Date, 103),'') As [Prealert Date],
   isnull(Cast(Job.Job_Id As Varchar(50)),'')as [Citiflow Ref], 
   isnull(Cast(Job.Client_JobReference As Varchar(50)),'')as [Client Job Ref],
   IsNull(Cast(HandOverDetails.No_Of_Rec as varchar(10)),'') As [No of Records],
   IsNull(HandOverDetails.Rack_Loc,'') As [Rack Loc.],
   IsNull(Convert(varchar(10),Job.Job_Created_date, 103),'')  As [Due Date],
   isnull(M_Printer.Printer_Name ,'-')as PrinterName,
   IsNull(Job.Job_Instructions ,'') As [Special Instruction],
   M_Login_2.UserName AS [CS Handled By],
   HandOverDetails.Operations_Comments AS [Operations Comments],
   isnull(Job.Job_Status,'') As [Signed off on Citiflow],
   M_Login_1.UserName AS [Cmpltd Job Bag Signed Off],
   Category = CASE HandOverDetails.Category when 'Not in Handover' then NULL else HandOverDetails.Category end
   
   FROM  M_Login M_Login_1 INNER JOIN
   Job_SignOff ON M_Login_1.Login_ID = Job_SignOff.Login_Id RIGHT OUTER JOIN
   M_Login M_Login_2 INNER JOIN
   M_Client INNER JOIN
   Job ON M_Client.Client_Id = Job.Client_Id ON M_Login_2.Login_ID = Job.Login_Id ON
   Job_SignOff.Job_Id = Job.Job_Id LEFT OUTER JOIN
   M_Printer INNER JOIN
   Job_Insert ON M_Printer.Printer_Id = Job_Insert.Printer_Id ON Job.Job_Id = Job_Insert.Job_Id LEFT OUTER JOIN
   HandOverDetails ON Job.Job_Id = HandOverDetails.Job_Id
   
   
   where Job.Job_Date >= @fdate
   and Job.Job_Date < Dateadd(day,1,@tdate)
   And Job.Client_Id = @ClientId
   And Job.Job_Status  in ( select * from  dbo.udfGetTempTable(@JobStatus))
   And HandOverDetails.Category  in ( select * from  dbo.udfGetTempTable(@Category))
   
   
   Order By Category ,ClientName ,[Prealert Date]
   
   END

 


 



Click here to sign in and reply. You could earn money via our $500 contest just for being helpful.
  SQL Server Function - Lokesh M  04-Jul-08 01:05 1:05:10 AM
      create a function in sql server and call from stroed proc - Deepak Ghule  04-Jul-08 01:40 1:40:18 AM
      create a function in sql server and call from stroed proc - Deepak Ghule  04-Jul-08 01:40 1:40:37 AM
      Use this way - A K  04-Jul-08 01:44 1:44:14 AM
      UDF - mv ark  04-Jul-08 01:47 1:47:42 AM
      SQL Server Function - Umapathy Kaliaperumal  04-Jul-08 02:15 2:15:25 AM
      Check this out - santhosh kumar  04-Jul-08 02:17 2:17:16 AM
      Sql server function - Sanjay Verma  04-Jul-08 05:17 5:17:30 AM
View Posts

promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   


"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class