We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

problem with UNION ALL

attipa
attipa asked
on
Medium Priority
472 Views
Last Modified: 2012-08-13
i have the following stored procedure:

declare @BlogID int
set @BlogID = '19321'

declare @DateAdded smalldatetime
Set @DateAdded = (Select DateAdded From Blog Where BlogID = @BlogID)


Select Top 1 BlogID, Headline, Category1NAME, DateAdded
From Blog
Where DateAdded < @DateAdded
Order By DateAdded DESC

UNION ALL
Select Top 1 BlogID, Headline, Category1NAME, DateAdded
From Blog
Where DateAdded > @DateAdded
Order By DateAdded DESC


why does it come up as an error in UNION ALL?  Please help
Comment
Watch Question

You can only ORDER after ALL union statements
declare @BlogID int
set @BlogID = '19321'
 
declare @DateAdded smalldatetime
Set @DateAdded = (Select DateAdded From Blog Where BlogID = @BlogID)
 
 
Select Top 1 BlogID, Headline, Category1NAME, DateAdded
From Blog
Where DateAdded < @DateAdded
 
UNION ALL
Select Top 1 BlogID, Headline, Category1NAME, DateAdded
From Blog
Where DateAdded > @DateAdded
Order By DateAdded DESC

Open in new window

Author

Commented:
that doesn't help because i need the blog row right before and right after the value i bring in.  is this possible?
This will eliminate the error.
declare @BlogID int
set @BlogID = '19321'
 
declare @DateAdded smalldatetime
Set @DateAdded = (Select DateAdded From Blog Where BlogID = @BlogID)
 
select * from 
(Select Top 1 BlogID, Headline, Category1NAME, DateAdded
From Blog
Where DateAdded < @DateAdded
Order By DateAdded DESC)a
UNION ALL
select * from (
Select Top 1 BlogID, Headline, Category1NAME, DateAdded
From Blog
Where DateAdded > @DateAdded
Order By DateAdded DESC)b

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Try like this:
declare @BlogID int
set @BlogID = '19321'
 
declare @DateAdded smalldatetime
Set @DateAdded = (Select DateAdded From Blog Where BlogID = @BlogID)
 
 
select * 
from (
	Select Top 1 BlogID, Headline, Category1NAME, DateAdded
	From Blog
	Where DateAdded < @DateAdded
	Order By DateAdded DESC
) t
UNION ALL
select * 
from (
	Select Top 1 BlogID, Headline, Category1NAME, DateAdded
	From Blog
	Where DateAdded > @DateAdded
	Order By DateAdded DESC
) t

Open in new window

Author

Commented:
thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.