Solved

Using Select Top (Select From )

Posted on 2014-03-24
5
469 Views
Last Modified: 2014-03-25
I can run this in SQL Server works fine but it errors out in Sql Anywhere

CREATE TABLE #Cal
(
      Site                  char(50),
      Dsc                  char(10),
      Tot                  int,
      TotSel                  int
)

INSERT INTO #Cal (Site, Dsc, Tot, TotSel)
VALUES( 'Dept1','Sales',3,1)

INSERT INTO #Cal (Site, Dsc, Tot, TotSel)
VALUES( 'Dept2','CallCenter',4,3)

INSERT INTO #Cal (Site, Dsc, Tot, TotSel)
VALUES( 'Dept3','Office',2,1)

CREATE TABLE #Data
(
      Site                  char(100),
      EmpFNam                  char(50),
      EmpLNam                  char(50),
      RandNum                  numeric(12,0),
      Sav                        Char(1),
      TotSel                  int

)

INSERT INTO #Data (Site, EmpFNam, EmpLNam, RandNum, Sav, TotSel)
VALUES( 'Dept1','Ann','Test',87693,'N',1)
INSERT INTO #Data (Site, EmpFNam, EmpLNam, RandNum, Sav, TotSel)
VALUES( 'Dept1','Annie','Test',89893,'N',1)
INSERT INTO #Data (Site, EmpFNam, EmpLNam, RandNum, Sav, TotSel)
VALUES( 'Dept1','Ann','Test',84593,'N',1)
INSERT INTO #Data (Site, EmpFNam, EmpLNam, RandNum, Sav, TotSel)
VALUES( 'Dept2','Ann','Test',87343,'N',2)
INSERT INTO #Data (Site, EmpFNam, EmpLNam, RandNum, Sav, TotSel)
VALUES( 'Dept2','Tom','Test',8793,'N',2)
INSERT INTO #Data (Site, EmpFNam, EmpLNam, RandNum, Sav,TotSel)
VALUES( 'Dept2','Bill','Test',6893,'N',2)
INSERT INTO #Data (Site, EmpFNam, EmpLNam, RandNum, Sav, TotSel)
VALUES( 'Dept2','John','Test',69793,'N',2)
INSERT INTO #Data (Site, EmpFNam, EmpLNam, RandNum, Sav, TotSel)
VALUES( 'Dept3','Lisa','Test',68783,'N',2)
INSERT INTO #Data (Site, EmpFNam, EmpLNam, RandNum, Sav, TotSel)
VALUES( 'Dept3','Jim','Test',66783,'N',2)
Select * from #Cal
Select * from #Data

Update #Data Set Sav = 'Y'
      From #Data
      Join (Select top (Select TotSel from #Cal Where Site = 'Dept2') RandNum from #Data Where Site = 'Dept2' order by RandNum desc) as p on p.RandNum =#Data.RandNum



--Syntax error near 'Select' on line 3

Select * from #Data
0
Comment
Question by:itdeptvnam
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

by:Joe Woodhouse
ID: 39952516
"Select top"... top how many? I would usually expect to see something like "select top 3 ...".
0
 

Author Comment

by:itdeptvnam
ID: 39953162
I'm trying to get the How many from another select statement ie. a number from Table #Cal to fill in the value of how many I want in my Select Top statement.
Its works fine in SQL Server but I need it to work in SQL Anywhere.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39953371
afaik, this is not possible in SQL Anywhere.
you can query the value into a variable (using the select @variable = xxx from ... where ), and use that variable in the select TOP @variable  ... statement
0
 
LVL 24

Expert Comment

by:Joe Woodhouse
ID: 39953389
Ah I see, using the nested SELECT as an expression to supply the value for the TOP.

I'm not even sure you can use a variable there... ah yes, an integer variable is allowed there.

angelIII has given you the correct answer. :)
0
 

Author Comment

by:itdeptvnam
ID: 39953460
Thanks angelII this works

Declare @Dept2 int
Set @Dept2 = (Select TotSel from #Cal Where Site = 'Dept2')

Update #Data Set Sav = 'Y'
      From #Data
      Join (Select top @Dept2 RandNum from #Data Where Site = 'Dept2' order by RandNum desc) as p on p.RandNum =#Data.RandNum
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

12 Experts available now in Live!

Get 1:1 Help Now