Solved

SQL Server Update Temporary Table Type - Scalar Variable Error

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now