capture sql statements

  • hi, I have written this script but there is an error by dbcc.

    how can I do this ?thank you for any help.

    CREATE TABLE #sql (dateadded datetime, sql varchar(max) )

    declare @sql varchar(max)

    declare @int int

    set @int= 1

    while @int < 200
    begin
    set @sql = dbcc inputbuffer(@int)

    insert into #sql (dateadded,sql)

    Select getdate(), @sql

    set @int=@int+1

    end

    select * from #sql

  • you had a few errors in what you were trying to do;

    dbcc inputbuffer returns a table with 3 columns...you have to capture the columns.

    your concatenation was not quite right;

    hre's a working example of what you were trying to do, but there are better ways to do this:

    CREATE TABLE #sql (dateadded datetime default getdate(), Eventtype varchar(100),Parameters int,EventInfo varchar(max) )

    --drop table #sql

    declare @sql varchar(max)

    declare @int int

    set @int= 1

    while @int < 200

    begin

    set @sql = 'dbcc inputbuffer(' + convert(varchar(30),@int) + ' ) '

    insert into #sql (Eventtype,Parameters,EventInfo)

    exec( @sql)

    set @int=@int+1

    end

    select * from #sql

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • hi, I get this error :

    Msg 207, Level 16, State 1, Line 9

    Invalid column name 'EventInfo'.

  • did you drop your temp table so it can be recreated with the new column?

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • If you are really running SQL Server 2005 then as Lowell mentions there are better ways to get this information using the DMV's and DMF's available. Here's a link to a script here on SSC that gets what you want I think.

  • thank you for your help

    this script now works, I'll also look at the link.

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

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