tips & tricks


Calendario
dicembre 2024
lmmgvsd
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

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: 15.072
  • Views Posts: 58.319
  • 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