简体   繁体   English

如何在T-SQL的变量中存储动态SQL结果?

[英]How to store a dynamic SQL result in a variable in T-SQL?

I have a stored procedure which takes 'table name' as parameter. 我有一个以“表名”为参数的存储过程。 I want to store my 'exec' results to a variable and display using that variable. 我想将我的“ exec”结果存储到变量中并使用该变量显示。 Here is my T-SQL stored procedure.. 这是我的T-SQL存储过程。

create procedure  DisplayTable( @tab varchar(30))
as
begin
   Declare @Query VARCHAR(30)
   set @Query='select * from ' +@tab
   EXEC (@Query)
END

I want to do something like this.. 我想做这样的事情。

SET @QueryResult = EXEC (@Query)
select @QueryResult

How do i achieve this.. Please help.. I am a beginner.. 我该如何实现..请帮助..我是初学者..

You can use XML for that. 您可以为此使用XML。 Just add eg "FOR XML AUTO" at the end of your SELECT. 只需在SELECT的末尾添加“ FOR XML AUTO”即可。 It's not tabular format, but at least it fulfills your requirement, and allows you to query and even update the result. 它不是表格格式,但至少可以满足您的要求,并允许您查询甚至更新结果。 XML support in SQL Server is very strong, just make yourself acquainted with the topic. SQL Server对XML的支持非常强大,只是使您熟悉该主题。 You can start here: http://technet.microsoft.com/en-us/library/ms178107.aspx 您可以从这里开始: http : //technet.microsoft.com/en-us/library/ms178107.aspx

alter procedure  DisplayTable( 
    @tab varchar(30)
    ,@query varchar(max) output
)
as
BEGIN
  Declare @execution varchar(max) = 'select * from ' +@tab

    declare @tempStructure as table (
        pk_id int identity
        ,ColumnName varchar(max)
        ,ColumnDataType varchar(max)
    )

    insert into
        @tempStructure
    select
        COLUMN_NAME
        ,DATA_TYPE
    from 
        INFORMATION_SCHEMA.columns
    where TABLE_NAME= @tab

  EXEC(@execution)

  declare @ColumnCount int = (SELECT count(*) from @tempStructure)
  declare @counter int = 1
  while @counter <= @ColumnCount
  BEGIN
    IF @counter = 1
    BEGIN
        set @query = (SELECT ColumnName + ' ' + ColumnDataType FROM @tempStructure where pk_id= @counter)
    END
    IF @counter <> 1
    BEGIN
        set @query = @query +  (SELECT ',' + ColumnName + ' ' + ColumnDataType FROM @tempStructure where @counter = pk_id)
    END
    set @counter = @counter + 1
  END
END

When you execute the SP, you'll now get a return of the structure of the table you want. 执行SP时,现在将返回所需表的结构。

This should hopefully get you moving. 希望这可以使您感动。

If you want the table CONTENTS included, create yourself a loop for the entries, and append them to the @query parameter. 如果要包含表CONTENTS,请为条目创建一个循环,并将其附加到@query参数。

Remember to delimit the @query, else when you read it later on, you will not be able to restructure your table. 请记住要对@query进行定界,否则稍后再阅读时,将无法重组表。

First of all you have to understand that you can't just store the value of a SELECT on a table in simple variable. 首先,您必须了解您不能只将SELECT的值存储在简单变量表中。 It has to be TABLE variable which can store the value of a SELECT query. 它必须是TABLE变量,可以存储SELECT查询的值。

Try the below: 请尝试以下方法:

select 'name1' name, 12 age
into MyTable
union select 'name2', 15 union
select 'name3', 19

--declaring the table variable and selecting out of it..

declare @QueryResult table(name varchar(30), age int)
insert @QueryResult exec DisplayTable 'MyTable'
select * from @QueryResult

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM