tips & tricks


Calendario
giugno 2024
lmmgvsd
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

Sql Function Return varchar

Sql Function Return varchar



CREATE FUNCTION SchemaName.GetValuesCommaSeparated
(
   @Id int
)
RETURNS varchar(100) -- or whatever length you need
AS
BEGIN
   
declare @values as varchar(100)
declare @result as varchar(100) =''


DECLARE cursorValues CURSOR
FOR      
select T.ColumnName from SchemaName.TableName T  where T.ID = @Id

--OPEN CURSOR.
OPEN cursorValues 

--FETCH THE RECORD INTO THE VARIABLES.
FETCH NEXT FROM cursorValues INTO @values 

--LOOP UNTIL RECORDS ARE AVAILABLE.
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @result += @values +','
FETCH NEXT FROM cursorValues INTO @values
END

--CLOSE THE CURSOR.
CLOSE cursorValues

DEALLOCATE cursorValues


RETURN @result;

END


declare @a as varchar(100)

set @a = SchemaName.GetValuesCommaSeparated(5)

print @a
Categoria: Transact SQL
mercoledì, 10 gen 2018 Ore. 10.33

Messaggi collegati


Archivio Posts
Anno 2020

Anno 2018

Anno 2015

Anno 2013

Anno 2011

Anno 2010

Anno 2009

Anno 2008
Statistiche
  • Views Home Page: 13.579
  • Views Posts: 56.311
  • Views Gallerie: 2.837
  • n° Posts: 117
  • n° Commenti: 1
Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003