How to use a table valued function in select clause? - sql-server

I have a tableA with one of the string columns (rawData) and I have a table-valued function (functionA) that functions will accept 'rawData' as an input parameter and returns five new fields. what will that function do? - this will do some string manipulation and split some data into five values
My motive is to select that string column (rawData) and also pass that table-valued function (functionA) and get those five new fields for every row in tableA
Here is the sample:
Select
(Select * from function (rawData))
from tableA
my expectations are I should get every row with that values from functionA along with rawData, but I'm getting below error:
Only one expression can be specified in the select list when the
subquery is not introduced with EXISTS.
How do I achieve this?

You would use a CROSS APPLY:
SELECT a.rawData, b.*
FROM TableA a
CROSS APPLY FunctionA(a.rawdata) b

Okay, first off, let's examine why you are getting an error. What is wrong with your syntax?
Well, a subquery in the select clause is expected to return at most one column. Your subquery, by using *, is returning all of the columns from the results of your function call, which appears to be more than one in this case.
Next, what are you trying to accomplish? It looks like you are trying to take the value of a column from tableA and apply functionA to the value in that column for each row returned from tableA.
The proper syntax to accomplish that is by using an apply. There is both cross apply and outer apply. The difference between them is like the difference between an inner join and a left join (or left outer join).
In other words, for the cross apply, if no rows are returned from the call to functionA for a particular row from tableA, then that row from tableA will not be included. For the outer apply, that case would be reversed, as the row from tableA would still be included, just the columns from the function call would be null.
An example query with this syntax based on the query in your question would be the following:
select fa.*
from tableA ta
outer apply functionA(ta.rawData) fa;
Note that I used an outer apply in my example, since your query didn't seem to be expecting rows from tableA with no rows returned from functionA to be excluded.

Related

compare column with text array in postgressql

We have a PostgreSQL database where I have two tables with column text[] datatype. When I use an inner join to get details I do not see it is matching with any row.
for e.g.
create table test{
names text[],
id
}
create table test_b{
names text[],
id
}
now when I run below query,
SELECT t.* from test t inner join test_b tt
where t.names=tt.names;
I don't see any results. I even tried normal query with
SELECT * FROM test where names='{/test/test}';
It also did not work.
Any suggestions?
I suspect that you want array overlap operator &&. Also, since you don't need to return anything from test_b, using exists with a correlated subquery is more relevant than a join:
select t.*
from test t
where exists (select 1 from test_b b where t.names && b.names)

How to use 'string to table splitting' using a function in a query?

First query with my function
select *
from [dbo].fnSplitString('1|16|170','|')
Second query with data to split
select m.CategoryTree as s
from tblBuyOnlineMaster m
I want to join these two query to get all the categories from second query like a table column, like the first result
Help will be very useful.
Assuming your function fnSplitString is a table-valued function, you can use the cross apply operator to perform the correlated "join":
select *
from tblBuyOnlineMaster m
cross apply [dbo].fnSplitString(m.CategoryTree, '|') s
Note the cross apply operator is congruent to an inner join. If you want left join semantics, use the outer apply operator instead. See also https://technet.microsoft.com/en-us/library/ms175156%28v=sql.105%29.aspx?f=255&MSPPError=-2147217396 .

Postgresql Select from values in array

i am converting multiple rows in to a array using array_agg() function,
and i need to give that array to a select statements where condition.
My query is,
SELECT * FROM table WHERE id =
ALL(SELECT array_agg(id) FROM table WHERE some_condition)
but it gives error, how can i over come it..
the error has been cleared by type casting the array, using my query like this
SELECT * FROM table WHERE id =
ALL((SELECT array_agg(id) FROM table WHERE some_condition)::bigint[])
reference link
It seems like you are over-complicating things. As far as I can tell, your query should be equivalent to simple:
SELECT * FROM table WHERE some_condition
Or, if you are selecting from 2 different tables, use join:
SELECT table1.*
FROM table1
JOIN table2 ON table1.id = table2.id
WHERE some_condition
Not only this is simpler, it is also faster than fiddling with arrays.

SQL FROM clause using n>1 tables

If you add more than one table to the FROM clause (in a query), how does this impact the result set? Does it first select from the first table then from the second and then create a union (i.e., only the rowspace is impacted?) or does it actually do something like a join (i.e., extend the column space)? And when you use multiple tables in the FROM clause, does the WHERE clause filter both sub-result-sets?
Specifying two tables in your FROM clause will execute a JOIN. You can then use the WHERE clause to specify your JOIN conditions. If you fail to do this, you will end-up with a Cartesian product (every row in the first table indiscriminately joined to every row in the second).
The code will look something like this:
SELECT a.*, b.*
FROM table1 a, table2 b
WHERE a.id = b.id
However, I always try to explicitly specify my JOINs (with JOIN and ON keywords). That makes it abundantly clear (for the next developer) as to what you're trying to do. Here's the same JOIN, but explicitly specified:
SELECT a.*, b.*
FROM table1 a
INNER JOIN table2 b ON b.id = a.id
Note that now I don't need a WHERE clause. This method also helps you avoid generating an inadvertent Cartesian product (if you happen to forget your WHERE clause), because the ON is specified explicitly.

Call TVF on every record of a table and concat results

I thought that must be obvious but I can't figure it out.
Say there is a table tblData with a column ID and a table-valued-function (_tvf) that takes an ID as parameter. I need the results for all ID's in tblData.
But:
SELECT * FROM tblData data
INNER JOIN dbo._tvf(data.ID) AS tvfData
ON data.ID = tvfData.ID
gives me an error: The multi-part identifier "data.ID" could not be bound
What is the correct way to pass all ID's to this TVF and concat the results?
Thanks
I think you might need to use CROSS APPLY instead of an inner join here:
SELECT *
FROM dbo.tblData data
CROSS APPLY dbo._tvf(data.ID) AS tvfData
This will call the TVF function for each data.ID of the base table and join the results to the base table's columns.
See ressources here:
Using CROSS APPLY in SQL Server
Understanding APPLY clause in SQL Server
Using T-SQL CROSS APPLY and OUTER APPLY

Resources