Solved

SQL Server Update Temporary Table Type - Scalar Variable Error

Posted on 2007-04-05
2
229 Views
Last Modified: 2011-09-20
I have the below in a stored procedure.  I needed to have the report information in one recordset because a Crystal Report will be viewing it.  Here's my question, it's on the UPDATE statement... it works if I replace @LogReport.Username with a username, like 'iufits', but when I use @LogReport.Username in the where clause I get the error message:  "Must declare the scalar variable "@LogReport".   I'm using SQL Server 2005.

What am I missing on the Update statement?

Declare @LogReport Table (
      Username varchar (20) NOT NULL ,
      LoginsTotal int NOT NULL,
      LoginsLastWeek int NOT NULL,
      LoginsLast30Days int NOT NULL,
    LoginsLastYear int NOT NULL
)

Insert Into @LogReport (Username, LoginsTotal, LoginsLastWeek, LoginsLast30Days, LoginsLastYear) Select Username, Count(Username), 0, 0, 0 As Logins From LOG_FILE Where LOG_ENTRY='Login' GROUP BY Username

Update @LogReport
      Set LoginsLastWeek = T2.Logins
      From (Select Username, Count(Username) As Logins From LOG_FILE Where LOG_TIME > (getDate() - 7) And LOG_ENTRY='Login' GROUP BY Username) T2
      Where T2.Username = @LogReport.Username
0
Comment
Question by:IUFITS
2 Comments
 
LVL 42

Accepted Solution

by:
dqmq earned 500 total points
ID: 18859504

Try this:

Update @LogReport
      Set LoginsLastWeek = T2.Logins
      From @LogReport LR
, (Select Username, Count(Username) As Logins From LOG_FILE Where LOG_TIME > (getDate() - 7) And LOG_ENTRY='Login' GROUP BY Username) T2
      Where T2.Username = LR.Username
 




0
 
LVL 3

Author Comment

by:IUFITS
ID: 18859596
Yup, that's perfect.  Thanks a ton!  I probably wouldn't have guessed the syntax in that format for a good while.  :P

Again, thank you!  ;)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question