1

I have written below SQL Server Scalar-Valued function to get the data of the specified column in the specified table. I'm passing the table name and its column name to the function. but according to this systax I can't give the @table parameter to the function and it asks me to declare it. This could be due to wrong syntax I have used here. Can anyone help me to figure it out?

USE [mydatabse] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER FUNCTION [dbo].[FnTDEVgroupConcat2] (@fieldName varchar(max), @table varchar(max) ) RETURNS varchar(max) AS BEGIN -- Declare the return variable here DECLARE @result varchar(max) set @result = (SELECT @fieldName + ',' FROM @table FOR XML PATH('')); -- Return the result of the function RETURN @result; END 

2 Answers 2

2

You can not specify the table name with a variable/parameter. You have to use the @table and build your query dynamically.

You can not execute dynamic SQL in a function so you need a stored procedure with output parameter.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no such thing as table type in SQL. Create User Defined Table Type with same structure as the one which you want to use in the function. Explore on how to create user defined type like INT,VARCHAR and other's create table type and then you can use same type in your function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.