Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Need function which accepts column name as a input and returns comma separated values in that column.

$
0
0

Hi All,

I need function which accepts column name as a input and returns comma separated values in that column.

CREATE TABLE [dbo].[EMP](
	[EMPNO] [numeric](4, 0) NOT NULL,
	[ENAME] [varchar](10) NULL,
	[JOB] [varchar](9) NULL,
	[MGR] [numeric](4, 0) NULL,
	[HIREDATE] [datetime] NULL,
	[SAL] [numeric](7, 2) NULL,
	[COMM] [numeric](7, 2) NULL,
	[DEPTNO] [numeric](2, 0) NULL
) ON [PRIMARY]

INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7369 AS Numeric(4, 0)), N'SMITH', N'CLERK', CAST(7902 AS Numeric(4, 0)), CAST(0x0000738200000000 AS DateTime), CAST(800.00 AS Numeric(7, 2)), NULL, CAST(20 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7499 AS Numeric(4, 0)), N'ALLEN', N'SALESMAN', CAST(7698 AS Numeric(4, 0)), CAST(0x000073C300000000 AS DateTime), CAST(1600.00 AS Numeric(7, 2)), CAST(300.00 AS Numeric(7, 2)), CAST(30 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7521 AS Numeric(4, 0)), N'WARD', N'SALESMAN', CAST(7698 AS Numeric(4, 0)), CAST(0x000073C500000000 AS DateTime), CAST(1250.00 AS Numeric(7, 2)), CAST(500.00 AS Numeric(7, 2)), CAST(30 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7566 AS Numeric(4, 0)), N'JONES', N'MANAGER', CAST(7839 AS Numeric(4, 0)), CAST(0x000073EC00000000 AS DateTime), CAST(2975.00 AS Numeric(7, 2)), NULL, CAST(20 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7654 AS Numeric(4, 0)), N'MARTIN', N'SALESMAN', CAST(7698 AS Numeric(4, 0)), CAST(0x0000749F00000000 AS DateTime), CAST(1250.00 AS Numeric(7, 2)), CAST(1400.00 AS Numeric(7, 2)), CAST(30 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7698 AS Numeric(4, 0)), N'BLAKE', N'MANAGER', CAST(7839 AS Numeric(4, 0)), CAST(0x0000740900000000 AS DateTime), CAST(2850.00 AS Numeric(7, 2)), NULL, CAST(30 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7782 AS Numeric(4, 0)), N'CLARK', N'MANAGER', CAST(7839 AS Numeric(4, 0)), CAST(0x0000743000000000 AS DateTime), CAST(2450.00 AS Numeric(7, 2)), NULL, CAST(10 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7788 AS Numeric(4, 0)), N'SCOTT', N'ANALYST', CAST(7566 AS Numeric(4, 0)), CAST(0x0000765400000000 AS DateTime), CAST(3000.00 AS Numeric(7, 2)), NULL, CAST(20 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7839 AS Numeric(4, 0)), N'KING', N'PRESIDENT', NULL, CAST(0x000074D100000000 AS DateTime), CAST(5000.00 AS Numeric(7, 2)), NULL, CAST(10 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7844 AS Numeric(4, 0)), N'TURNER', N'SALESMAN', CAST(7698 AS Numeric(4, 0)), CAST(0x0000748B00000000 AS DateTime), CAST(1500.00 AS Numeric(7, 2)), CAST(0.00 AS Numeric(7, 2)), CAST(30 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7876 AS Numeric(4, 0)), N'ADAMS', N'CLERK', CAST(7788 AS Numeric(4, 0)), CAST(0x0000767600000000 AS DateTime), CAST(1100.00 AS Numeric(7, 2)), NULL, CAST(20 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7900 AS Numeric(4, 0)), N'JAMES', N'CLERK', CAST(7698 AS Numeric(4, 0)), CAST(0x000074E100000000 AS DateTime), CAST(950.00 AS Numeric(7, 2)), NULL, CAST(30 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7902 AS Numeric(4, 0)), N'FORD', N'ANALYST', CAST(7566 AS Numeric(4, 0)), CAST(0x000074E100000000 AS DateTime), CAST(3000.00 AS Numeric(7, 2)), NULL, CAST(20 AS Numeric(2, 0)))
INSERT [dbo].[EMP] ([EMPNO], [ENAME], [JOB], [MGR], [HIREDATE], [SAL], [COMM], [DEPTNO]) VALUES (CAST(7934 AS Numeric(4, 0)), N'MILLER', N'CLERK', CAST(7782 AS Numeric(4, 0)), CAST(0x0000751400000000 AS DateTime), CAST(1300.00 AS Numeric(7, 2)), NULL, CAST(10 AS Numeric(2, 0)))
SELECT * FROM EMP

I WANTED TO CREATE ONE FUNCTION WHICH ACCEPTS COLUMN NAME AS A INPUT PARAMEter
AND SHOULD RETURN ALL VALUES WITH IN THAT COLUMN BASED ON COMMA SEPARATED.
FOR EXAMPLE: 1
------------
If i pass ENAME(column name) as a input paramer to the function following output as to come
Output:
SMITH,ALLEN,WARD,JONES,MARTIN,BLAKE,CLARK,SCOTT,KING,TURNER,ADAMS,JAMES,FORD,MILLER

FOR EXAMPLE: 2
------------
If i pass EMPNO(column name) as a input paramer to the function following output as to come
Output:
-------
7369,7499,7521,7566,7654,7698,7782,7788,7839,7844,7876,7900,7902,7934

Thanks in advance.


bala krishna


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>