Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

SQL Server Update Temporary Table Type - Scalar Variable Error

Posted on 2007-04-05
2
231 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

837 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