0

I have the following SQL query:

Declare @Total_SysDown as int,
        @Login_SysDown as int



Set @Total_SysDown =    (SELECT SCHED_SYS_DOWN FROM AGT_SC AS S)
Set @Login_SysDown =    (SELECT SYS_DOWN FROM AGT_AC AS A)


Insert Into dbo.DATA(DATE,ID,LNAME,FNAME,Total_SysDown,Login_SysDown)
Select C.DATE,C.ID,E.Last_Name,E.First_Name,@Total_SysDown @Login_SysDown
From dbo.AGT as C Inner Join dbo.EMP as E ON C.ID = E.ID 
Group by C.ID,C.DATE,E.Last_Name,E.First_Name

This or just the variables with the Select statement gives me an error of Subquery returned than 1 value. From what I understand, this means that I should be inserting one record at a time, but I am unsure how to do this. Is there a while statement I should be putting in, or are my variables actually hindering me in the first place?

1
  • How are the values from AGT_SC and AGT_AC related to AGT or EMP? Is there an ID Commented Sep 8, 2011 at 20:23

3 Answers 3

4

At least one of the queries:

(SELECT SCHED_SYS_DOWN FROM AGT_SC AS S)
(SELECT SYS_DOWN FROM AGT_AC AS A)

returns more than 1 row, so you cannot assign it to a scalar variable.

As a temporary solution you can do SELECT TOP 1 to make sure each query returns at most one row.

Sign up to request clarification or add additional context in comments.

4 Comments

+1. Agree with @a1ex07. If you do, SELECT TOP 1 .... will never fail, but I don't know if that's your intention.
Alright, but if I want to insert more than one row, what must I do for the select to progressively go down the table rows?
You can declare variables of type table (or create temp table), populate and use INSERT INTO ... SELECT. You may also avoid creating table variables, and use derived queries. Finally, you may use cursors (usually it's a bad choice, but sometimes unavoidable)
@JP: Your query will execute using TOP 1, but that doesn't necessarily mean that your query will be correct. Just keep that in mind...
1

I don't think the problem is with your INSERT statement at all.

Your problem is in the SET Statements. The SELECT SCHED_SYS_DOWN FROM AGT_SC AS S statement or the other statement is returning more than one value.

When you use SET you are assigning ONE value to the variable. Your SELECT is returning multiple values. Change your query to return only one row.

Comments

1

You're receiving this error because your subqueries return more than one record:

Set @Total_SysDown =    (SELECT SCHED_SYS_DOWN FROM AGT_SC AS S) 
Set @Login_SysDown =    (SELECT SYS_DOWN FROM AGT_AC AS A) 

To use variables here, you will need to ensure that only one record is returned from the query, either by using a WHERE clause, TOP 1, or something else. I can't tell for sure by your example, but it sounds like you should be joining those tables to your SELECT query.

SELECT ...
FROM dbo.AGT agt
     INNER JOIN AGT_SC sc
         ON sc.<joining column> = <joining table>.<joining column>
     INNER JOIN AGT_AC ac
         ON ac.<joining column> =  <joining table>.<joining column>

5 Comments

I tried joining, but I get the same error. Using Top 1, the query works(with obviously the same variable data for every row). I will try to figure out how to insert a where clause.
What are you trying to get from that table? A single value with matching AGT.ID?
Pretty much. I want the date,id,names,and the value for that agent from the variable. I need to do this for every row from my source table.
What is the relationship between AGT and AGT_SC and AGT_AC? One-to-one or one-to-many? Basically, how is it supposed to know which Agent to select from the table?
One-to-many. I have date and ID from both agt_sc and agt_ac which match AGT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.