Is it possible to return Dynamic Columns using Table Valued Function in SQL Server 2005? - sql-server-2005

I have written some set of statements which returns a table with some columns. But we dont know exactly which column it returns.
In table valued function, I dont know how to return the Dynamic columns of the table.
How to write the table value function for this scenario? If this is not possible then what would be the alternative for this task?
Any suggestions please.

The table-valued function requires static columns (names and types). If you want dynamic columns in result-set, use procedure instead of function.

Related

Is it possible to return different table variables through SQL Server table valued function based on param value passed in it?

I have written some set of statements which returns a table with some static columns. But I'll required to download csv format using the same function with different column. (Reason is that I created static columns to display high charts and we are using custom code to export chart data so require some different format to download)
In table valued function, I dont know how to return the Dynamic columns of the table based on pram passed to function.
How to write the table value function for this scenario? If this is not possible then what would be the alternative for this task(Stored procedure can't be used in my existing scnerio due to some limitation of code)?
Any suggestions please.

Use of multiple column name as input parameter or wild card in stored procedure

I would want to add an optional parameter to my stored procedure with default *. If the list of columns is provided [delimited by a comma] these columns should be returned back by the procedure. If the wildcard character is provided [star] *, all columns should be returned. Please let me know how to implement it.
First thing - why stored procedure not table UDF?
Anyway it would be easier to pass null instead of "*" - tsql allows default values on UDF parameters.
You would have to construct query dynamically and then use sp_executesql().
The issue is that you should validate columns list to prevent errors.

how to use common function in query expression?

I want to use the "split" function in a simple query on my SSRS 2008 report. However, I get an error "Query execution failed for dataset "SlsmRealNum". "Split" is not a recognized built-in function name". But it's listed as a common function (text) if I open up the Expression box on the query, so not sure why it's failing?
my simple select statement is:
select slsm_num, slsm_msid from Salesman where slsm_msid = split(User.UserID,"\").GetValue(1)
right now to get the report to work, I have one parameter (SlsmnNum) that has the Split expression in it (to get the MSID of the user) and then a 2nd parameter that uses the above query in the Dataset Salesrepum using the #SlsmnNum parameter as the MSID. I'd like to not have to have 2 parapmeters if possible and just get the actualy salesrep # in just one. Any help is greatly appreciated!
Your select statement is executed as SQL so the error you are getting is actually from SQL server. This may be where you are getting confused.
There are two components to SSRS - SQL Statements and Report Expressions. Typically, SQL statements are used to generate datasets by querying the database. Report expressions are used to organize, aggregate, and filter the dataset once obtained from the SQL database. Since the SQL statement is executed IN the SQL database, only the functions that are in the database are available. The code you posted is a SQL statement not a Report Expression.
For example, you can't take a Report Expression and expect it to work in SSMS? No, because they are two different entities with wholly different syntax and purpose. When it comes to using built-in SSRS functions inside a SQL statement it will not work, the database has no concept of what the built in User.UserId is and as such you must use a parameter to transport the value over to the SQL query. This is definition and purpose of a parameter and why they exist.
Split is a function in SSRS which is why you see it in your expression reference, however, it is not a function in SQL. The code you posted is SQL syntax, so I am betting that this is the SQL statement that you are using to obtain your dataset. Therefore the query fails since the SQL DB does not have a Split Function.
You can add this split function to your database and the code is located here: Split String in SQL. You could also use something along the following in your where clause, the following is your updated SQL statement.
SELECT slsm_num, slsm_msid from Salesman where slsm_msid = SUBSTRING(#UserId, PATINDEX('%\%', #UserId), LEN(#UserId))
You would set the #UserId parameter's value to an expression of User!UserID rather than specifying it in your select statement.
The SSRS expression examples have a function similar to what your code is trying to accomplish if you were wanting the same thing in the report side. The function you are looking for is InStr(). On your report side you could use something along the lines of:
=Parameters!User.Value.Substring(Parameters!User.Value.IndexOf("\")+1, Parameters!User.Value.Length-Parameters!User.Value.IndexOf("\")-1)
Expression examples can be found here: MSDN Expression examples.

SQL Server - Passing a table to a function... how? and is it a good idea?

I have a stored procedure that query one of my tables. To that table I want it to add another column with a values that is calculated by a function.
The function is another query with sub queries that is based on user data in another table.
Now since there are a lot of rows to do calculations on, I figure that the same data is being queried over and over for each row by the function.
I thought I might query the needed data for the function in the stored procedure and keep it in a temp table, and pass that table to the function so the function actually queries the in-memory small table instead of making a query to the real table - that way making it faster, and not having too much IO on my real table.
My question is actually 2 parts:
is what I'm saying even makes sense? is it a good solution that will make it faster and more efficient? or is passing table data to a function comes with a high cost?
how do I pass a table to a function?
Thanks
EDIT:
BTW - it's a scalar function
You can use Table-Valued Parameters in a stored procedure or function on MS SQL Server 2008 and up:
http://technet.microsoft.com/en-us/library/bb510489.aspx

Function to return a cursor(result set)

Is it possible to return a cursor from a user defined function in MS SQL Server2005? If it is, how is that done?
Based on the result returned, user-defined functions in SQL Server can be either scalar or table-valued. The former return single values of simple types: int, datetime etc., and the latter return row sets.
There are no cursor-valued functions in SQL Server.