龙空技术网

sql字符串拼接方法

阿拉不解梦 654

前言:

现时看官们对“oraclesql字段合并”可能比较看重,各位老铁们都需要剖析一些“oraclesql字段合并”的相关资讯。那么小编在网络上收集了一些关于“oraclesql字段合并””的相关内容,希望大家能喜欢,咱们快快来学习一下吧!

方案一:

Create FUNCTION [dbo].Get_Contract_StyleNo(@AutoInc bigint)

RETURNS VARCHAR(Max)

AS

BEGIN

DECLARE @Content NVARCHAR(MAX)

SET @Content = ''

SELECT @Content = case when @Content='' then '' else @Content + ',' end + GoodsNo

FROM (select distinct GoodsNo from EXP_Contract_Clothing_Goods where MainID=@AutoInc) m

RETURN @Content

END

GO

构造标量函数

使用xml

构造通用存储过程

方案三:

------默认条件,MainID=AutoInc

alter procedure [dbo].[SP_SumStr]

(

@MTable Varchar(100) --主表

,@ZTable Varchar(100) --子表

,@MColumn Varchar(100) --主表更新字段

,@ZColumn Varchar(100) --子表拼接字段

,@ConditionSql Varchar(500) --传入的条件语句

)

as

set nocount on

begin

declare @strSql varchar(3000)

select top 1 AutoInc as MainID, Convert(nvarchar(500),CnName) as GoodsNo

into #result

from SYS_Staff where 1<0

set @strSql = 'insert into #result select distinct MainID,'+ @ZColumn +' from '+@ZTable+' ' + @ConditionSql

exec(@strSql)

DECLARE @Content NVARCHAR(MAX)

SET @Content = ''

SELECT @Content = case when @Content='' then '' else @Content + ',' end + GoodsNo FROM #result

set @strSql = 'Update a set a.'+@MColumn+'='+@Content+' from '+ @MTable +' a left join '+@ZTable+' b on a.AutoInc=b.MainID ' + @ConditionSql

exec(@strSql)

----SELECT @Content

end

set nocount off

标签: #oraclesql字段合并