有没有“分裂"?用于 SELECT 查询的 t-sql 中的函数

2021-09-10 00:00:00 sql tsql sql-server

我想像这样使用我的功能

I want to use my function like this

SELECT BaseSplit(line,';') FROM table

有没有办法做到这一点???

is there any way to do that ???

我尝试了 CROSS APPLY 但它不是我想要的

I try CROSS APPLY but it not what I want

我的表格有一列名为 line

My table has a column named line

line 
______
15;2;5
NULL
10;3;6

我想将此列拆分为像

15
2
5
Null
10
3
6

是否有自动执行此操作的字符串函数?
如果没有,是否可以编写自己的函数?

Is there a string function that does this automatically?
If not, is it possible to write my own function?

推荐答案

它应该可以正常工作,但您需要对函数进行模式限定.

It should work fine, but you need to schema-qualify functions.

浏览器:

SELECT dbo.BaseSplit(line,';') FROM table

如果您的函数在与 dbo 不同的架构中,您显然应该改用它.

If your function is in a different schema than dbo you should obviously use that instead.

好的 - 假设它是一个表值函数然后......

OK - assuming its a table valued function then...

SELECT t.Id, f.*  FROM table AS t CROSS APPLY dbo.BaseSplit(line,';') AS f

这将为每个分割线返回一行 + 主表中该条目的 ID(假设主表中存在名为 Id 的列).如果您想要更好,我将需要一个您期望的输出示例

That would return a row for each split line + the ID of the that entry in the main table (assuming a column named Id exists in the main table). If you want better that that I'm going to need an example of what output you expect

相关文章