Variable T-SQL

  • I know how to get the results of a DBCC command or stored proc into a table.  But my real goal is to get it into a variable. 

    Of course, I could read it into a table and then select from the table into a variable.  But can I do it all in one step somehow, that is put the results of a DBCC command into a variable with something like

    select @inputbuffer = exec ('dbcc inputbuffer(20)') ?

    I assume this will never work because there are essentially three columns output from this?  Anyway, if you know how to make it work, I'd love to know.  Otherwise, I'll read it into a table and then select off that table.

  • What type of variable are you trying to get it into?  I would assume a table variable and if so, you just declare your table variable and then insert into it just like a table.

     

  • I don't think you can insert into a table variable using exec.  You will need to insert into a table and select into your variables.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • declare

    @MyTable TABLE

    (

    [wait type]

    varchar(150),

    Requests

    float,

    [Wait Time]

    float,

    [Signal Wait Time]

    float

    )

    insert

    into @MyTable

    exec

    ('dbcc sqlperf(''waitstats'')')

    select

    * from @MyTable

     

     

    this works fine in sql 2005.  You cannot do a select into with a table variable though

  • Ah, you are correct.  It works fine in SQL Server 2005.  I was working in SQL Server 2000 where this will not work.  Sorry for the confusion.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • Thx guys.  I had a bone-headed syntax error.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply