English 中文(简体)
TSQL:一行所有领域的价值
原标题:TSQL: Values of all field in a row into one string
  • 时间:2011-08-22 09:33:21
  •  标签:
  • sql
  • tsql

i 有必要将单一行文的所有价值,而不是否定的价值观,如:

table:

CustomerName  Address Zip
Alex          Moscow  1234

结果:

CustomerName: Alex
Address: Moscow
Zip: 1234

重要的说明——即不了解外地名称/类型,因此,它应当贯穿所有领域,而且所有价值都不会增加。

Looks like it can do this using xquery, but can t find right syntax. Any hints?

感谢!

最佳回答
select T2.N.value( local-name(.) ,  nvarchar(128) )+ :  +
       T2.N.value( . ,  nvarchar(max) )
from (select *
      from YourTable
      for xml path(  ), type) as T1(X)
  cross apply T1.X.nodes( /* ) as T2(N)
问题回答
select  CustomerName:   + isNull(CustometName,   ) +  Address:   
+ isNull(Address,   ), +  Zip:  + isNull(Zip,   ) from [TableName]

也许你们需要把一些价值观带给var。

不是作为Mikael的解决办法。 但是,我仍然希望列入。

DECLARE @yourtable nvarchar(128)
DECLARE @sql as nvarchar(2100)
DECLARE @col as nvarchar(2000)

SET @yourtable =  <tablename> 

SELECT @col = coalesce(@col,    ) +  +   +t2.column_name+ :    + cast([  + t2.column_name +  ] as varchar) + char(32) 
FROM INFORMATION_SCHEMA.TABLES t1 join 
INFORMATION_SCHEMA.COLUMNS t2 on t1.table_name = t2.table_name
where t2.is_nullable =  NO  and t1.table_name = @yourtable
and t1.table_type =  BASE TABLE  and t1.table_schema = t2.table_schema
and t2.table_schema =  dbo 
SET  @sql =  select   + stuff(@col, 1,1,  ) +  from   + @yourtable
EXEC (@sql)




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签