Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello, i have this stored producer that union many tables and i want to order by apartemetnnoen as u see down and still give me error near union

What I have tried:

C#
SELECT 
	   [Spare6]
      ,[Spare5]
      ,[Spare4]
      ,[Spare3]
      ,[Spare2]
      ,[Spare1]
      ,[details]
      ,[note]
      ,[documents]
      ,[termsofpaymneten]
      ,[termsofpaymnet]
      ,[aprtmentstatusen]
      ,[aprtmentstatus]
      ,[amountdateen]
      ,[amountdate]
      ,[amountleft]
      ,[amountpaid]
      ,[rent]
      ,[noofpayemtsen]
      ,[noofpayemts]
      ,[sourceen]
      ,[source]
      ,[iddateen]
      ,[iddate]
      ,[idnumber]
      ,convert(datetime, contractenen, 111) as contractenen
      ,[contracten]
      ,[contractstarten]
      ,[contractstart]
      ,[contractnoen]
      ,[contractno]
      ,[nationalityen]
      ,[nationality]
      ,[secondpartyen]
      ,[secondparty]
      ,[firstpartyen]
      ,[firstparty]
      ,[apartemetnnoen]
      ,[apartemetnno]
      ,[Bulidingnoen]
      ,[Bulidingno]
      ,[No] FROM [All_Vech].[dbo].[Bulding_Sharfya]  
	WHERE [idnumber] LIKE '%' + @idnumber + '%' OR [Bulidingno] Like '%' + @Bulidingno + '%' OR [Bulidingnoen] LIKE '%' + @Bulidingnoen + '%' OR [apartemetnno] LIKE '%' + @apartemetnno + '%'  OR [apartemetnnoen] LIKE '%' + @apartemetnnoen + '%'  OR secondparty LIKE '%' + @secondparty + '%' OR secondpartyen LIKE '%' + @secondpartyen + '%' OR nationality LIKE '%' + @nationality + '%' OR nationalityen LIKE '%' + @nationalityen + '%' OR [contractno] LIKE '%' + @contractno + '%'  OR [contractnoen] LIKE '%' +  @contractnoen + '%' OR [contracten] Like '%' + @contracten + '%' 
	ORDER BY apartemetnnoen DESC;
UNION
Posted
Updated 16-Apr-17 0:42am

1 solution

Since you have only one SELECT statement, remove the UNION clause from the end of you SQL.

In other words

...
	WHERE [idnumber] LIKE '%' + @idnumber + '%' OR [Bulidingno] Like '%' + @Bulidingno + '%' OR [Bulidingnoen] LIKE '%' + @Bulidingnoen + '%' OR [apartemetnno] LIKE '%' + @apartemetnno + '%'  OR [apartemetnnoen] LIKE '%' + @apartemetnnoen + '%'  OR secondparty LIKE '%' + @secondparty + '%' OR secondpartyen LIKE '%' + @secondpartyen + '%' OR nationality LIKE '%' + @nationality + '%' OR nationalityen LIKE '%' + @nationalityen + '%' OR [contractno] LIKE '%' + @contractno + '%'  OR [contractnoen] LIKE '%' +  @contractnoen + '%' OR [contracten] Like '%' + @contracten + '%' 
	ORDER BY apartemetnnoen DESC;
UNION

For more information and examples, have a look at UNION (Transact-SQL) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Learn.net37 16-Apr-17 8:57am    
well,i have more than one select its same as top so i didn't post it
Wendelius 16-Apr-17 10:16am    
If that's the case then remove the semicolon ending the first query. Also the ORDER BY must be only once and in the end. In the order by you cannot refer to columns by their names, instead you need to use ordinal numbers based on the location in your query.

For example

SELECT name , department
FROM employee
UNION
SELECT name, department
FROM contractor
ORDER BY 2, 1

The query above selects the data from both tables and sorts the result first based on department then based on name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900