Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2003
    Posts
    16

    Unanswered: How can I insert the results from an EXEC command

    Declare @pTable varchar(30)
    DECLARE @TotRec integer
    select @pTable = 'Salaires'
    SELECT @TotRec EXEC ('SELECT Count(*) FROM ' + @pTable)
    GO
    Print @TotRec

    How can I insert the results from an EXEC command?

  2. #2
    Join Date
    Oct 2003
    Posts
    16

    Re: How can I insert the results from an EXEC command

    [QUOTE][SIZE=1]Originally posted by ericjean
    Declare @pTable varchar(30)
    DECLARE @TotRec integer
    select @pTable = 'Salaires'
    SELECT @TotRec = EXEC ('SELECT Count(*) FROM ' + @pTable)
    GO
    Print @TotRec

    Like that is not correct!

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Unless you are just presenting a simplified example, there is no need to use dynamic sql to get rowcounts. You can look them up directly in the system tables (sysindexes where indid in (0, 1)):
    -----------------------------------------
    set @TotRec =
    (select sysindexes.Rowcnt
    from sysobjects
    inner join sysindexes on sysobjects.id = sysindexes.id
    where sysobjects.name = @pTable and sysobjects.xtype = 'U' and sysindexes.indid in (0, 1)

    Print @TotRec
    -----------------------------------------

    blindman

  4. #4
    Join Date
    Oct 2003
    Posts
    16
    Good idea!

    Thank's

  5. #5
    Join Date
    Oct 2003
    Posts
    16

    I need to do like this

    I need to do like this

    Declare @pTable varchar(30)
    DECLARE @tmp integer
    select @pTable = 'Salaires'
    SELECT @tmp = exec('SELECT count(*) FROM ' + @pTable + ' WHERE ID = 2)

  6. #6
    Join Date
    Oct 2003
    Posts
    8
    Declare @pTable varchar(30)
    DECLARE @tmp integer
    select @pTable = 'Salaires'
    SET @tmp = ('SELECT count(*) FROM ' + @pTable + ' WHERE ID = 2)

    @tmp now has the count of that table

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •