日期:2014-05-18 浏览次数:20629 次
建议你提供详细的资料: 例如表的结构,表之间的关系,测试数据,相关算法及需要的结果。 这样有助于我们理解你的意思,更主要的是能尽快让你获得答案或解决问题的方法。
------解决方案--------------------
是不是这样楼主
IF OBJECT_ID('A') IS NOT NULL DROP TABLE A IF OBJECT_ID('B') IS NOT NULL DROP TABLE B IF OBJECT_ID('C') IS NOT NULL DROP TABLE C GO CREATE TABLE A (aid INT IDENTITY(1,1) ,NAME VARCHAR(10),t1 INT ,t2 INT ,[TYPE] INT) CREATE TABLE B (id INT IDENTITY(1,1),aid INT ,NAME VARCHAR(10),CONTENT VARCHAR(10),type_one INT,type_two INT) CREATE TABLE C (id INT IDENTITY(1,1),aid INT,CONTENT VARCHAR(max),[TYPE] VARCHAR(10)) INSERT INTO A SELECT 'A1',1,2,1 UNION ALL SELECT 'A2',1,2,3 go INSERT INTO B SELECT 1,'B1','B.Content1',1,0 UNION ALL SELECT 2,'B2','B.Content2',0,1 DECLARE @a VARCHAR(max) DECLARE @b VARCHAR(max) SET @a='';SET @b=''; SELECT TOP 1 @a='<p>'+ISNULL(B.NAME,'')+'</p>'+ISNULL(B.CONTENT,'') FROM B WHERE EXISTS(SELECT 1 FROM A WHERE a.aid=b.aid) AND type_one=1 AND type_two=0 SELECT @b=@b+'<p>'+ISNULL(B.NAME,'')+'</p>'+ISNULL(B.CONTENT,'') FROM B WHERE EXISTS(SELECT 1 FROM A WHERE a.aid=b.aid) AND type_one=0 AND type_two=1 INSERT INTO C (aid,content,[type]) SELECT aid,CONTENT=@a+@b,(CASE WHEN [type]=1 THEN '类型1' ELSE '类型2' END) AS [TYPE] FROM A WHERE t1=1 AND t2=2 SELECT * FROM C /* id aid CONTENT type TYPE ----------- ----------- ----------------------------------------- ---------- 1 1 <p>B1</p>B.Content1<p>B2</p>B.Content2 类型1 类型1 2 2 <p>B1</p>B.Content1<p>B2</p>B.Content2 类型2 */