It will be useful especially while making generic insert statements with column names as values.
usage : select dbo.fnColList('MyTable')
create function fnColList(@in_vcTbl_name varchar(8000))
returns varchar(8000)
as
begin
declare @colList2BuildAuditTable varchar(max)
SELECT @colList2BuildAuditTable = coalesce(@colList2BuildAuditTable+ ',', '')+ '['+ B.NAME +']'
FROM SYSOBJECTS A JOIN SYSCOLUMNS B ON A.ID = B.ID
WHERE A.ID = OBJECT_ID(@in_vcTbl_name)
ORDER BY B.COLORDER
return @colList2BuildAuditTable
end
|
Its very good man..!!!
ReplyDelete