Solved

Top Percent with varable

Posted on 2007-11-16
9
742 Views
Last Modified: 2012-08-14
I have the following query in a stored procedure:

insert into @tblTopTenPct
Select DLID = t1.DLID, Qty = t1.Qty, Base = t1.Base, xMonth = t1.xMonth, Issue = 'Old type'
FROM (SELECT TOP 10 Percent * FROM @DLList) AS t1
Where Datediff(mm,Base,GetDate()) > (@EldestCrit * 12)

Works fine. But now the users want to be able to change the percent at runtime
FROM (SELECT TOP 10 Percent * FROM @DLList) AS t1
My understanding is in Sql Server 2005 I can replace 10 percent with a varible @pct
But Im using Sql Server 2000.
I was trying to use dynamic sql but run in the problem with using variable tables. My thougt was to take the X percent a store it in a seperate table within my procedure and just select * in my from statement above.
Any Ideas...
0
Comment
Question by:MadIce
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 31

Expert Comment

by:James Murrell
ID: 20301781
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20301782

in sql 2000, you cannot do that with PERCENT, unless you would make it dynamic sql
0
 

Author Comment

by:MadIce
ID: 20301902
I tried this:
set @sql = 'Insert into @TopPct Select Top ' + @EldestPercent + ' Percent * from @DLList'
Execute (@Sql)

@TopPct and @DLList'  are tables and I dont think thats allowed in dynamic sql
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20301920
unfortunately, you cannot use table variables inside dynamic sql unless they are declare altogether inside that same dynamic sql.

0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20301955
Your best bet is to add the entire resultset to a temporary table or variable of type table that has an IDENTITY column in the correct order. You can then easily calculate the percent based on the total number of rows added (@@ROWCOUNT) and so do something like this:

Set @SomeMaxValue = @@ROWCOUNT * @YourPercent / 100

Select *                               -- Replace * with your column names
From @YourTempTable
Where ID Between 1 And @SomeMaxValue
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 20301967
Missed the ORDER BY clause it should be:

Set @SomeMaxValue = @@ROWCOUNT * @YourPercent / 100

Select *                               -- Replace * with your column names
From @YourTempTable
Where ID Between 1 And @SomeMaxValue
Order By ID

This way you avoid the whole dynamic SQL mess.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 20304083
use dynamic SQL...

--Stored procedure creation
create procedure spr_percent_query(@percentage smallint)
as
begin
       declare@string_execute1 varchar(1000)
       set @string_execute1 = 'insert into @tblTopTenPct
       Select DLID = t1.DLID, Qty = t1.Qty, Base = t1.Base, xMonth = t1.xMonth, Issue = 'Old type'
       FROM (SELECT TOP ' + @percentage + ' Percent * FROM @DLList) AS t1
       Where Datediff(mm,Base,GetDate()) > (@EldestCrit * 12)'
       exec (@string_execute1)
end
go

--Usage
exec spr_percent_query 20 --> top 20 percent

Hope this helps...
0
 

Author Closing Comment

by:MadIce
ID: 31409665
Sorry for delay. Had a problem and wasn't sure of the cause. I was trying something simular when you posted your answer. answer gave me what I needed. Thanks
0
 

Author Comment

by:MadIce
ID: 20323150
Thanks, that's worked out fine.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting Teradata SQL to Oracle SQL (exadata) 3 28
SQL for Frequently Bought With 11 45
SQL Server Deadlocks 12 47
SQL Exceptions 3 35
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

930 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

11 Experts available now in Live!

Get 1:1 Help Now