Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 27th, 2012  |  syntax: None  |  size: 2.69 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to convert a Tsql scalar function into a table function?
  2. create function dbo.ufn_StripHTML
  3.     (   @Input      varchar(max),
  4.         @Delimiter  char(1)
  5.     )
  6. returns varchar(max)
  7. as
  8.  
  9. begin
  10.  
  11.     declare @Output varchar(max)
  12.     select  @Input = replace(replace(@input, '<', @Delimiter), '>', @Delimiter)
  13.  
  14.     select @Output = isnull(@Output, '') + s
  15.     from    (    select   row_number() over (order by n.id asc) [i],
  16.                  substring(@Delimiter + @Input + @Delimiter, n.id + 1, charindex(@Delimiter, @Delimiter + @Input + @Delimiter, n.id + 1) - n.id - 1) [s]
  17.             from    [evolv_cs].[dbo].[progress_note] n
  18.             where   n.id = charindex(@Delimiter, @Delimiter + @Input + @Delimiter, n.id) and
  19.                  n.id <= len(@Delimiter + @Input)
  20.             ) d
  21.     where i % 2 = 1
  22.  
  23.     return @Output
  24.  
  25. end
  26.        
  27. Msg 156, Level 15, State 1, Line 1
  28. Incorrect syntax near the keyword 'identity'.
  29.        
  30. select [progress_note].[note_text], [progress_note].[event_log_id]
  31. INTO #TEMP_PN
  32. from [evolv_cs].[dbo].[progress_note]
  33. group by [progress_note].[event_log_id], [progress_note].[note_text]
  34.        
  35. CREATE TYPE dbo.MyTableType AS TABLE
  36. (
  37.     col1 int identity(1,1) NOT NULL,
  38.     col2 varchar(max) NULL
  39. )
  40. GO
  41.  
  42. CREATE TABLE [dbo].[MyTable] (
  43.     [col1] [int] identity(1,1) NOT NULL,
  44.     [col2] [varchar](max) NULL
  45.     )  
  46. GO
  47.  
  48. create PROC usp_AddRowsToMyTable @MyTableParam MyTableType READONLY, @Delimiter varchar(30)
  49.     as
  50.     INSERT INTO MyTable([col2])
  51.     SELECT [col2]  
  52.     FROM @MyTableParam
  53.  
  54.     --update MyTable
  55.     --set col2 = replace(replace(MyTable.col2, '<', @Delimiter), '>', @Delimiter)
  56.     select s, i, t
  57.     from(
  58.     select MyTableInput.col1 [i],
  59.     replace(replace(MyTable.col2, '<', @Delimiter), '>', @Delimiter) as t,
  60.     substring(@Delimiter + MyTableInput.col2 + @Delimiter, MyTable.col1 + 1,
  61.     charindex(@Delimiter, @Delimiter + MyTableInput.col2 + @Delimiter, MyTable.col1 + 1) - MyTable.col1 - 1) [s]
  62.     from MyTable
  63.     inner join MyTable as MyTableInput on MyTable.col1 = MyTableInput.col1
  64.     where MyTable.col1 = CHARINDEX(@Delimiter, @Delimiter + MyTableInput.col2 + @Delimiter, MyTable.col1)
  65.      and MyTable.col1 <= LEN(@Delimiter + MyTableInput.col2)
  66. ) d
  67.  
  68.  
  69. DECLARE @MyTable MyTableType
  70.  
  71. INSERT INTO @MyTable(col2)
  72. VALUES ('<h><dsf>2000<h><dsf>'),
  73.     ('<sd><dsf>2001'),
  74.     ('2002<vnv><dsf>'),
  75.     ('<gsd><dsf>2003<h><dsf>'),
  76.     ('<eefs><dsf><h><dsf>2004<dfgd><dsf>')
  77.  
  78. EXEC dbo.usp_AddRowsToMyTable @MyTableParam = @MyTable, @Delimiter = '|'
  79.        
  80. create function dbo.ufn_StripHTML
  81.     (   @Input      varchar(max),
  82.         @Delimiter  char(1)
  83.     )
  84. returns @retYourNewTable table
  85. (
  86.     id int primary key clustered not null,
  87.     yoursecond column varchar(100) null,
  88.     ....
  89. )
  90. as
  91. ....