I want to insert the count() into new table i created

  • I have this query running perfectly fine and the result set looks like this

    NumberRows | Time_stamp

    ----------- | ----------------------------

    940 | 2013-07-11 18:00:00.357

    Now i want to insert these two columns values in a new table.

    i cant figure out how can i do this.

    DECLARE @dCurrentTime DATETIME

    DECLARE @dCurrentTimeMinus5 DATETIME

    --Declare @counttotal int

    SET @dCurrentTime = GETDATE()

    SET @dCurrentTimeMinus5 = DATEADD(minute, -5, @dCurrentTime)

    --insert into UsersLoggedIn (NumberRows,@dCurrentTime);

    SELECT Count(*) As NumberRows, @dCurrentTime as Time_stamp

    FROM (

    SELECT lID

    FROM SessionState_Variables

    WHERE dLastAccessed BETWEEN @dCurrentTimeMinus5 AND @dCurrentTime

    GROUP BY lID

    ) As SessionsTable

    Regards

  • What can't you figure out? You have the insert there, commented out...is it erroring?

    Sorry, just spotted the insert is wrong...

    insert into UsersLoggedIn(<column name for count>, <column name for time stamp)

    you have a variable in the column list for the insert and you have a semicolon after the insert column list but before the select.

    the structure should be something like this:

    create table UsersLoggedIn( number_of_users int, time_stamp datetime); -- plus whatever else you want plus some indexing...

    insert UsersLoggedIn(number_of_users, time_stamp)

    select count(*), @dCurrentTime

    from ....blah blah blah

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Msg 207, Level 16, State 1, Line 13

    Invalid column name 'NumberRows'.

    Msg 207, Level 16, State 1, Line 13

    Invalid column name 'Time_stamp'.

  • huum (7/11/2013)


    Msg 207, Level 16, State 1, Line 13

    Invalid column name 'NumberRows'.

    Msg 207, Level 16, State 1, Line 13

    Invalid column name 'Time_stamp'.

    That sounds like those columns do not exist in UsersLoggedIn.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Its Resolved,

    THanks for the participation

  • Viewing 5 posts - 1 through 4 (of 4 total)

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