Solved

Stored Procedure Problem

Posted on 2013-11-07
4
227 Views
Last Modified: 2013-11-22
Hi,

I have stored procedure that when I run it produces this error message:

Variable assignment is not allowed in a statement containing a top level UNION, INTERSECT or EXCEPT operator.

The only Union statement within the stored procedure is this:

      SELECT @li_item_id = child_id FROM work_references WHERE parent_id=@li_wo_id AND parent_type=140 AND child_type=1 AND subject_data ='Y'
      UNION
      SELECT @li_item_id = parent_id FROM work_references WHERE child_id=@li_wo_id AND child_type=140 AND parent_type=1 AND subject_data ='Y'

Can the same result be achieved by removing the union bit?

Thanks.
0
Comment
Question by:andyw27
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
Saurabh Bhadauria earned 500 total points
Comment Utility
Try it like this..

select @li_item_id = Child_id  from 
(SELECT  child_id FROM work_references WHERE parent_id=@li_wo_id AND parent_type=140 AND child_type=1 AND subject_data ='Y'
      UNION
      SELECT parent_id FROM work_references WHERE child_id=@li_wo_id AND child_type=140 AND parent_type=1 AND subject_data ='Y'
) x 

Open in new window


Thanks,
saurabh
0
 
LVL 11

Expert Comment

by:Louis01
Comment Utility
To do it whithout a union:
SELECT @li_item_id = CASE WHEN parent_id=@li_wo_id 
                           AND parent_type=140 
                           AND child_type=1 
                          THEN child_id 
                          WHEN child_id=@li_wo_id 
                           AND child_type=140 
                           AND parent_type=1 
                          THEN parent_id 
                     END
  FROM work_references 
 WHERE subject_data ='Y'
   AND (   (    parent_id=@li_wo_id 
            AND parent_type=140 
            AND child_type=1 )
        OR (    child_id=@li_wo_id 
            AND child_type=140 
            AND parent_type=1 ))

Open in new window


Edit 1:
Personally though, I'd use the UNION like saurv suggested.
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
>SELECT @li_item_id = child_id ...
>UNION
>SELECT @li_item_id = parent_id ...

What exactly is the point of assigning a value(s) from two different SELECT clauses to a scalar variable, joined by a UNION?

If @li_item_id is a table variable and not a scalar (i.e. single value) variable, please say so.
0
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
Just a thought, explain for us in standard English what you're trying to pull off here.

Something like..
   "Assign to variable @li_item_id the child_id if it exists, and if it doesn't assign the parent_Id"
   "Populate a temp table with all child_id and parent_id values based on these queries"
   "Heck, I have no idea what I'm doing here, and am hoping you guys can help"
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Read about achieving the basic levels of HRIS security in the workplace.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

772 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

10 Experts available now in Live!

Get 1:1 Help Now