如何在表的整列上使用东西:

时间:2017-07-25 13:44:28

标签: sql-server vba mssql-jdbc

我执行了以下查询并执行得很好: -

SELECT table2id, stuff((select CHAR(13) + table1name  from table1 where table1id=table2.table2id FOR XML PATH (''), TYPE
).value('.', 'varchar(max)')
, 1, 1, '')
 from table2 where table2id=117 group by id;

但是当我使用count(*)时,如下面的查询: -

SELECT table2id, stuff((select CHAR(13) + count(*)  from table1 where table1id=table2.table2id FOR XML PATH (''), TYPE
).value('.', 'varchar(max)')
, 1, 1, '')
 from table2 where table2id=117 group by id;

我收到以下错误:

 Msg 245, Level 16, State 1, Line 19
Conversion failed when converting the varchar value '
' to data type int.

现在我如何填充table1中的所有列?任何人都可以帮忙!

我想要的结果如下: -

table2id  |  table1name  |  table1id  | table1color
------------------------------------------------------
  117     |  jon, jack   |   117,117  | blue,red

(我正在为table1table2添加我的示例数据): -

表1:

table1id | table1name | table1color | table1city | table1animal |...(I 
   have 25 columns like this !)
--------------------------------------------------------------   
117      | jon        | blue        | city1      | animal1       

117      | jack       | red         | city2      | animal2       

表2:

table2id | table2uniqueid
-------------------------
117      | asbn6383hhh3j3837
118      | kf9s8sujfu6df5d7j 

2 个答案:

答案 0 :(得分:2)

这与东西无关 你得到错误的原因是:
count(*)返回一个int。 char(13)char。每当您尝试int + char时,SQL Server都会尝试将char隐式转换为int。当然,char(13)无法转换为int 您需要将count(*)明确转换为varchar

SELECT table2id, stuff(
     (
     select CHAR(13) + cast(count(*) as varchar(10)) 
     from table1 
     where table1id=table2.table2id 
     FOR XML PATH (''), TYPE).value('.', 'varchar(max)'), 1, 1, '')
from table2 
where table2id=117 
group by id;

答案 1 :(得分:0)

尝试此代码,它将通过使用Dynamic Sql帮助您 首先,我创建了两个带有样本数据的物理表

CREATE TABLE Table1 (table1id INT , table1name Varchar(100) , table1color Varchar(100) , table1city Varchar(100) , table1animal Varchar(100))
INSERT INTO Table1
SELECT 117, 'jon' , 'blue'        , 'city1'      , 'animal1' UNION ALL     
SELECT 117, 'jack', 'red'         , 'city2'      , 'animal2'   

CREATE TABLE Table2( table2id INT, table2uniqueid nvarchar(1000))
INSERT INTO Table2
SELECT 117,'asbn6383hhh3j3837' Union all
SELECT 118,'kf9s8sujfu6df5d7j' 

动态Sql代码以获得预期结果

SET NOCOUNT ON
IF OBJECT_ID('Tempdb..#TEMP') IS NOT NULL
DROP TABLE #TEMP

CREATE TABLE #TEMP(ID INT IDENTITY ,Query nvarchar(max))

IF OBJECT_ID('Tempdb..#TEMP2') IS NOT NULL
DROP TABLE #TEMP2

CREATE TABLE #TEMP2(ID INT IDENTITY ,Query nvarchar(max))

DECLARE @MinID INT,
        @MaxID INT,
        @Sql nvarchar(max),
        @Getcolumn nvarchar(max),
        @Sql2 nvarchar(max),
        @CteSql nvarchar(max),
        @CteSql2 nvarchar(max),
        @FullSql nvarchar(max)

DEClare @COlumn Table 
(
ID INT IDENTITY,
COlumnname varchar(100)
)
INSERT into @COlumn(COlumnname)
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME='Table1'

SELECT @MinID=MIn(Id),@MaxID=MAX(ID)FRom @COlumn

While (@MinID <=@MaxID)
Begin

SELECT @Getcolumn=COlumnname From @COlumn WHERE ID=@MinID

SET @Sql=N' STUFF((SELECT  '', ''+ CAST('+@Getcolumn +' AS VARCHAR(5))   
        FROM cte AS i
        WHERE i.table1id=o.table1id For XML PATH ('''')),1,1,'''') AS '+@Getcolumn 

INSERT INTO #TEMP(Query)
SELECT @Sql

SET @MinID=@MinID+1
END


    SELECT DISTINCT @Sql2= 
        STUFF((SELECT ', '+ CAST(Query AS nvarchar(max)) FROM #TEMP i For Xml Path(''), type
        ).value('.', 'nvarchar(max)')
        , 1, 2, '')
FROM #TEMP o

SET @Sql2=@Sql2 +' FRom Cte o'

 SET @CteSql= 'SELECT Top 1 '+''' ;With cte
AS
(SELECT  T2.table2id,''+ STUFF((SELECT '', ''+''T1.''+COLUMN_NAME  FROM  INFORMATION_SCHEMA.COLUMNS 
                    Where TABLE_NAME=''Table1'' For XML PATH ('''')),1,1,'''') +'' From   Table2 T2
 LEFT JOIN Table1 T1
 On T1.table1id=t2.table2id )''' +'+CHAR(13)+CHAR(10)+'+'''SELECT DISTINCT table2id,''
        FROM INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME=''Table1'''

INSERT INTO #TEMP2(Query)
EXECUTE(@CteSql)
SELECT  @CteSql2= Query From #TEMP2

SET @FullSql=@CteSql2+@Sql2

PRINT @FullSql
EXEC(@FullSql)

SET NOCOUNT OFF

运行查询后的结果

table2id    table1id        table1name   table1color    table1city       table1animal
---------------------------------------------------------------------------------------
117         117, 117        jon, jack    blue, red      city1, city2     anima, anima
118         NULL            NULL         NULL           NULL             NULL