Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stored Procedure Problem

Posted on 2013-11-07
4
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
Saurabh Bhadauria earned 2000 total points
ID: 39629952
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
ID: 39629977
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 66

Expert Comment

by:Jim Horn
ID: 39630116
>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 66

Expert Comment

by:Jim Horn
ID: 39630290
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

618 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