Solved

SQL Server Update Temporary Table Type - Scalar Variable Error

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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.​
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…

746 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

15 Experts available now in Live!

Get 1:1 Help Now