27,579
社区成员
发帖
与我相关
我的任务
分享
create table tb(col1 varchar(20),col2 varchar(20))
insert into tb values('CN' , 'China')
insert into tb values('JP' , 'Japan')
go
create proc my_proc @col as varchar(50) , @return as varchar(50) OUTPUT
as
begin
set @col = @col + ' '
declare @col3 as varchar(50)
declare @col4 as varchar(50)
set @return = ''
set @col3 = ''
set @col4 = ''
while charindex(' ' , @col) > 0
begin
set @col4 = ''
set @col3 = left(@col , charindex(' ' , @col) - 1)
if right(@col3 , 1) <> ','
select @col4 = col2 from tb where ' ' + col1 + ' ' = ' ' + @col3 + ' '
else
select @col4 = col2 + ',' from tb where ',' + col1 + ',' = ',' + @col3
if @col4 is not null and @col4 <> ''
set @return = @return + @col4 + ' '
else
set @return = @return + @col3 + ' '
set @col = substring(@col , charindex(' ' , @col) + 1, len(@col))
end
end
go
declare @return as varchar(50)
set @return = ''
exec my_proc 'I am from CN, not JP' , @return OUTPUT
select @return
/*
--------------------------------------------------
I am from China, not Japan
(所影响的行数为 1 行)
*/
exec my_proc 'I am from CN , not JP' , @return OUTPUT
select @return
/*
--------------------------------------------------
I am from China , not Japan
(所影响的行数为 1 行)
*/
drop table tb
drop proc my_proc
create table tb(col1 varchar(20),col2 varchar(20))
insert into tb values('CN' , 'China')
insert into tb values('JP' , 'Japan')
go
create proc my_proc @col as varchar(50) , @return as varchar(50) OUTPUT
as
begin
set @col = @col + ' '
declare @col3 as varchar(50)
declare @col4 as varchar(50)
set @return = ''
set @col3 = ''
set @col4 = ''
while charindex(' ' , @col) > 0
begin
set @col4 = ''
set @col3 = left(@col , charindex(' ' , @col) - 1)
select @col4 = col2 from tb where ' ' + col1 + ' ' = ' ' + @col3 + ' '
if @col4 is not null and @col4 <> ''
set @return = @return + @col4 + ' '
else
set @return = @return + @col3 + ' '
set @col = substring(@col , charindex(' ' , @col) + 1, len(@col))
end
end
go
declare @return as varchar(50)
set @return = ''
exec my_proc 'I am from CN , not JP' , @return OUTPUT
select @return
/*
--------------------------------------------------
I am from China , not Japan
(所影响的行数为 1 行)
*/
exec my_proc 'I am from JP , not CN' , @return OUTPUT
select @return
/*
--------------------------------------------------
I am from Japan , not China
(所影响的行数为 1 行)
*/
drop table tb
drop proc my_proc