Solved

SQL Server Update Temporary Table Type - Scalar Variable Error

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
locating an SSIS Package on SQL 2008R2 5 56
point in time restore in SQL server 26 41
Need help with a query 3 36
compare date to getdate() 8 13
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

679 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