Solved

Using Select Top (Select From )

Posted on 2014-03-24
5
461 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
A procedure for exporting installed hotfix details of remote computers using powershell
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

9 Experts available now in Live!

Get 1:1 Help Now