Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 I want to insert the resultset into new table

Author  Topic 

kshahzad
Starting Member

45 Posts

Posted - 2013-07-11 : 14:19:04
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

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-11 : 14:39:28
quote:
Originally posted by kshahzad

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, Time_stamp) -- insert the exact column names from the new table here
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

Go to Top of Page

kshahzad
Starting Member

45 Posts

Posted - 2013-07-11 : 14:47:08
No this is a syntax error
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-11 : 14:55:28
what is the error?
Go to Top of Page

kshahzad
Starting Member

45 Posts

Posted - 2013-07-11 : 15:02:30
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'.
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-11 : 15:21:24
Can you show the definition of the table named "UsersLoggedIn"

quote:
Originally posted by kshahzad

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'.


Go to Top of Page

kshahzad
Starting Member

45 Posts

Posted - 2013-07-11 : 15:23:03
Create TABLE UsersLoggedIn
(
TotalUsers int,
TimeRecorded datetime
);
Go to Top of Page

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-07-11 : 15:25:37
Try this:
quote:
Originally posted by MuMu88

quote:
Originally posted by kshahzad

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 (TotalUsers, TimeRecorded ) -- insert the exact column names from the new table here
SELECT Count(*) As TotalUsers, @dCurrentTime as TimeRecorded

FROM (

SELECT lID

FROM SessionState_Variables

WHERE dLastAccessed BETWEEN @dCurrentTimeMinus5 AND @dCurrentTime

GROUP BY lID

) As SessionsTable

Regards



Go to Top of Page

kshahzad
Starting Member

45 Posts

Posted - 2013-07-11 : 15:53:23
Awesome..Thanks
Go to Top of Page
   

- Advertisement -