Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Syntax error near Union

Author  Topic 

divyaram
Posting Yak Master

180 Posts

Posted - 2010-03-12 : 02:01:43
Hi all,

i have to retrive some columns and i have to take a union same columns with differnt conditiopn.
for selecting condition is like this it has to take only the top one and odered by enddate desc
condition .But here its shows syntax error in Union like incorrect syntax near union,
is it because i am using order by clause before union

code which i have done is like this

DECLARE @Field_ID INT
SELECT TOP 1 FIS_Sub_Article.Article_ID,
CONVERT(nvarchar(max),FIS_Sub_Article.Article_Title)AS Article_Title,
CONVERT(nvarchar(max),FIS_Sub_Article.Abstract)AS Abstract,
ISNULL(CONVERT(VARCHAR,FIS_Sub_Article_Status.End_Date , 101),'')AS PublishedDate
FROM FIS_Sub_Article WITH(NOLOCK)
INNER JOIN FIS_Sub_Article_Status WITH(NOLOCK) ON FIS_Sub_Article_Status.Article_ID=FIS_Sub_Article.Article_ID
WHERE FIS_Sub_Article.Stage_ID=18
--AND FIS_Sub_Article.IsDeleted=0
AND FIS_Sub_Article.Field_ID =71
ORDER BY FIS_Sub_Article_Status.End_Date DESC

UNION

SELECT
TOP 1 FIS_Sub_Article.Article_ID,
CONVERT(nvarchar(max),FIS_Sub_Article.Article_Title)AS Article_Title,
CONVERT(nvarchar(max),FIS_Sub_Article.Abstract)AS Abstract,
ISNULL(CONVERT(VARCHAR,FIS_Sub_Article_Status.End_Date , 101),'')AS PublishedDate
FROM FIS_Sub_Article WITH(NOLOCK)
INNER JOIN FIS_Sub_Article_Status WITH(NOLOCK)
ON FIS_Sub_Article_Status.Article_ID=FIS_Sub_Article.Article_ID
WHERE FIS_Sub_Article.Stage_ID=18
AND FIS_Sub_Article.IsDeleted=0
AND FIS_Sub_Article.Field_ID =86
ORDER BY FIS_Sub_Article_Status.End_Date DESC

Regards,
Divya

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-12 : 02:09:42
Use derived tables, that should work:

DECLARE ...

select * from
(your_first_select_comes_here) as dt
union
select * from
(your_second_select_comes_here) as dt2


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

divyaram
Posting Yak Master

180 Posts

Posted - 2010-03-12 : 02:19:10
quote:
Originally posted by webfred

Use derived tables, that should work:

DECLARE ...

select * from
(your_first_select_comes_here) as dt
union
select * from
(your_second_select_comes_here) as dt2


No, you're never too old to Yak'n'Roll if you're too young to die.




Thank you webfred it worked......

Regards,
Divya
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-12 : 02:34:47
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -