Sql ..). 如何在查询中使用它?使用UDF…或者…有没有其他方法可以在不插入变量表的情况下将循环写入select?非常好的Jithin,thanx提供了有用的代码,但我要求将其用于查询(select、view…)。如何在查询中使用它?使用UDF…或者…有没有
Sql ..). 如何在查询中使用它?使用UDF…或者…有没有其他方法可以在不插入变量表的情况下将循环写入select?非常好的Jithin,thanx提供了有用的代码,但我要求将其用于查询(select、view…)。如何在查询中使用它?使用UDF…或者…有没有,sql,sql-server,querying,Sql,Sql Server,Querying,..). 如何在查询中使用它?使用UDF…或者…有没有其他方法可以在不插入变量表的情况下将循环写入select?非常好的Jithin,thanx提供了有用的代码,但我要求将其用于查询(select、view…)。如何在查询中使用它?使用UDF…或者…有没有其他方法可以在不插入变量表的情况下将循环写入select?非常好的Jithin,thanx提供了有用的代码,但我要求将其用于查询(select、view…)。如何在查询中使用它?使用UDF…或者…有没有其他方法可以在不插入变量表的情况下将循环写
..). 如何在查询中使用它?使用UDF…或者…有没有其他方法可以在不插入变量表的情况下将循环写入select?非常好的Jithin,thanx提供了有用的代码,但我要求将其用于查询(select、view…)。如何在查询中使用它?使用UDF…或者…有没有其他方法可以在不插入变量表的情况下将循环写入select?非常好的Jithin,thanx提供了有用的代码,但我要求将其用于查询(select、view…)。如何在查询中使用它?使用UDF…或者…有没有其他方法可以在不插入变量表的情况下将循环写入select?非常好的Jithin,thanx提供了有用的代码,但我要求将其用于查询(select、view…)。如何在查询中使用它?使用UDF…或者…有没有其他方法可以在不插入变量表的情况下将循环写入select?
ID | Name | Funct | Phone1 | Phone2 | Phone3
1 | John | boss | 112233 | 114455 | 117788
2 | Jane | manager | NULL | NULL | 221111
3 | Tony | merchant | 441100 | 442222 | NULL
ID | Name | Funct | Phone | Ord
1 | John | boss | 112233 | 1
1 | John | boss | 114455 | 2
1 | John | boss | 117788 | 3
2 | Jane | manager | 221111 | 3
3 | Tony | merchant | 441100 | 1
3 | Tony | merchant | 442222 | 2
ID | Name | Funct | Phones
1 | John | boss | 112233,114455,117788
2 | Jane | manager | 221111
3 | Tony | merchant | 441100,442222
select id, name, funct, phone1 as phone, 1 as ord
from source
where phone1 is not null
union all
select id, name, funct, phone2 as phone, 2 as ord
from source
where phone2 is not null
union all
select id, name, funct, phone3 as phone, 3 as ord
from source
where phone3 is not null;
select so.*
from source s cross apply
(select s.id, s.name, s.funct, s.phone1 as phone, 1 as ord union all
select s.id, s.name, s.funct, s.phone2 as phone, 2 as ord union all
select s.id, s.name, s.funct, s.phone3 as phone, 3 as ord
) so
where phone is not null;
SELECT CONCAT(
'CREATE TABLE New_Table (',GROUP_CONCAT(
DISTINCT CONCAT(Name, ' VARCHAR(50)')
SEPARATOR ','),');')
FROM
Previous_Table
INTO @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;
Declare @table table
(ID int, Name varchar(100),Funct varchar(100),Phones varchar(400))
Insert into @table Values
(1,'John','boss','112233,114455,117788'),
(2,'Jane','manager','221111' ),
(3,'Tony','merchant','441100,442222')
Select * from @table
Declare @tableDest table
([ID] int, [name] varchar(100),[Phones] varchar(400))
Declare @max_len int,
@count int = 1
Set @max_len = (Select max(Len(Phones) - len(Replace(Phones,',','')) + 1)
From @table)
While @count <= @max_len
begin
Insert into @tableDest
Select id,Name,
SUBSTRING(Phones,1,charindex(',',Phones)-1)
from @table
Where charindex(',',Phones) > 0
union
Select id,Name,Phones
from @table
Where charindex(',',Phones) = 0
Delete from @table
Where charindex(',',Phones) = 0
Update @table
Set Phones = SUBSTRING(Phones,charindex(',',Phones)+1,len(Phones))
Where charindex(',',Phones) > 0
Set @count = @count + 1
End
------------------------------------------
Select *
from @tableDest
Order By ID
------------------------------------------