如何在每行包含列名而另一行包含值的行中显示一行?

发布于 2024-09-25 16:36:57 字数 387 浏览 1 评论 0原文

我有一个包含大约 60 列的表,如果我在查询分析器中编写一个选择查询来获取一行,它会显示它,并且我必须滚动才能显示数据..
我如何获得显示为 60 行的一行,每行包含 2 列,一列用于列名称,另一列用于值
例如 Select * from table where id = 1 默认情况下它显示为

ID Col1  Col2  Col3 Col4 Col5 ...... Col60  
1  v1    v2    v3   v4   v5   ...... v60

我希望它显示为

ID    1  
Col1  v1  
Col2  v2  
Col3  v3
...  
Col60 v60

I have a table that contain about 60 columns and if i write a select query in query analyser to get a row it show it and i must scroll to show data ..
How can i get one row in which it shows as 60 row, each row contain 2 columns one for column name and other for value
For example Select * from table where id = 1 in default it shown as

ID Col1  Col2  Col3 Col4 Col5 ...... Col60  
1  v1    v2    v3   v4   v5   ...... v60

I want it to be shown as

ID    1  
Col1  v1  
Col2  v2  
Col3  v3
...  
Col60 v60

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

跨年 2024-10-02 16:36:57

下面的存储过程可以满足您的需要,

CREATE PROCEDURE [dbo].[Rotat]
    -- Add the parameters for the stored procedure here
     @Where nvarchar(max),
     @tableName nvarchar(max)
AS
BEGIN

Declare @SqlQuery nvarchar(max),@ColumnName nvarchar(255)
DECLARE @TempTable TABLE 
( 
      ID int IDENTITY(1,1) , 
      ColumnName nvarchar(255), 
      ColumnValue ntext 
)


INSERT INTO @TempTable (ColumnName)
    SELECT      column_name
    FROM information_schema.columns
    WHERE table_name = @tableName 

Declare @index int
Set @index = 1;  

Declare @Count int
Select @Count = Count(ID) from @TempTable
declare @columnValue  nvarchar(255)
declare @paraDef nvarchar(max)
declare @string nvarchar(max)

WHILE @index <= @Count 
BEGIN
    Select @ColumnName = ColumnName from @TempTable where id = @index
    set @string ='select @ret= cast('+ @ColumnName + ' AS nvarchar(255) )  
        from '+@tableName+' WHERE ' + @Where

    set  @paraDef=N'@ret nvarchar(255) output'

    EXECUTE sp_executesql @string, @paraDef,@ret=@ColumnValue  output

    UPDATE  @TempTable 
    SET     ColumnValue = @columnValue
    WHERE   ID =@index

    Set @index = @index + 1
END

Select * from @TempTable

END

只需将其称为传递表名称和将返回一行的条件即可

EXEC    [dbo].[Rotat]
        @Where = 'UserID = 123456',
        @tableName = 'Users'

The below Stored Procedure do What you need

CREATE PROCEDURE [dbo].[Rotat]
    -- Add the parameters for the stored procedure here
     @Where nvarchar(max),
     @tableName nvarchar(max)
AS
BEGIN

Declare @SqlQuery nvarchar(max),@ColumnName nvarchar(255)
DECLARE @TempTable TABLE 
( 
      ID int IDENTITY(1,1) , 
      ColumnName nvarchar(255), 
      ColumnValue ntext 
)


INSERT INTO @TempTable (ColumnName)
    SELECT      column_name
    FROM information_schema.columns
    WHERE table_name = @tableName 

Declare @index int
Set @index = 1;  

Declare @Count int
Select @Count = Count(ID) from @TempTable
declare @columnValue  nvarchar(255)
declare @paraDef nvarchar(max)
declare @string nvarchar(max)

WHILE @index <= @Count 
BEGIN
    Select @ColumnName = ColumnName from @TempTable where id = @index
    set @string ='select @ret= cast('+ @ColumnName + ' AS nvarchar(255) )  
        from '+@tableName+' WHERE ' + @Where

    set  @paraDef=N'@ret nvarchar(255) output'

    EXECUTE sp_executesql @string, @paraDef,@ret=@ColumnValue  output

    UPDATE  @TempTable 
    SET     ColumnValue = @columnValue
    WHERE   ID =@index

    Set @index = @index + 1
END

Select * from @TempTable

END

Just call it an pass table name and your condition that will return one row for example

EXEC    [dbo].[Rotat]
        @Where = 'UserID = 123456',
        @tableName = 'Users'
陪你到最终 2024-10-02 16:36:57

如果您想要一种非常快速的技术来查看转置的数据(正如您的问题似乎所建议的那样),请尝试将结果网格中的输出复制并粘贴到 Excel 中,然后复制 Excel 数据并使用选择性粘贴来转置输出。

If you want a very quick technique just for viewing data transposed (as your question seems to suggest), try copying and pasting the output from the results grid into Excel, then copy the Excel data and use Paste Special to Transpose the output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文