December 3, 2003 at 5:56 pm
For the following sql script in a stored procedure, is it possible to dynamically determinate the top number following the Top keyword? That means the stored procedure can run for top 10, top 50, top 100 etc. depending on the variable passed by a user. Thanks a lot
Select Top 10 drug_name, Count(drug_name)
From Orders
Group by drug_name
Order by Count(drug_name) desc
December 4, 2003 at 12:38 am
DECLARE @top NVARCHAR(4) --or more if needed
DECLARE @stmt NVARCHAR(200)
SET @top = '10'
SET @stmt ='SELECT TOP '+ @top +'drug_name, Count(drug_name) From Orders Group
by drug_name Order by Count(drug_name) desc '
EXEC sp_executeSQL @stmt
You might to read this http://www.algonet.se/~sommar/dynamic_sql.html for the negative effects of dynamic sql.
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 4, 2003 at 7:02 am
CREATE PROC p_TopDrugs @Rows int AS
SET NOCOUNT ON
SET ROWCOUNT @Rows
SELECT Drug_Name, COUNT(*)
FROM Orders
GROUP BY Drug_Name
ORDER BY COUNT(*) DESC
--Jonathan
--Jonathan
December 4, 2003 at 9:18 am
That's what I really like about your replies, Jonathan!
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 4, 2003 at 6:47 pm
Thanks a lot Jonathan ! The script helps me to solve the problem. But if I want use the top 10 drug name in a sub-query, I can not use 'order by'. In addition, the outsite query will retirve more than 10 rows; so I can not set @rowcount in the strored procedure. Than you.
quote:
CREATE PROC p_TopDrugs @Rows int AS
SET NOCOUNT ON
SET ROWCOUNT @Rows
SELECT Drug_Name, COUNT(*)
FROM Orders
GROUP BY Drug_Name
ORDER BY COUNT(*) DESC--Jonathan
December 4, 2003 at 6:51 pm
Thank you very match, Frank. It really helps me a lot. But what shoul I do if I want to use the returned drug names in a sub query?
quote:
DECLARE @top NVARCHAR(4) --or more if needed
DECLARE @stmt NVARCHAR(200)
SET @top = '10'
SET @stmt ='SELECT TOP '+ @top +'drug_name, Count(drug_name) From Orders Group
by drug_name Order by Count(drug_name) desc '
EXEC sp_executeSQL @stmtYou might to read this http://www.algonet.se/~sommar/dynamic_sql.html for the negative effects of dynamic sql.
Frank
December 5, 2003 at 1:40 am
You can execute almost all T-SQL commands dynamically.
I would write your query including your subquery first in plain T-SQL and translate it then into dynamic sql.
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 5, 2003 at 6:49 am
quote:
Thanks a lot Jonathan ! The script helps me to solve the problem. But if I want use the top 10 drug name in a sub-query, I can not use 'order by'. In addition, the outsite query will retirve more than 10 rows; so I can not set @rowcount in the strored procedure. Than you.
CREATE PROC p_TopDrugs @Rows int AS
SET NOCOUNT ON
SET ROWCOUNT @Rows
SELECT Drug_Name, COUNT(*) Cnt
INTO #t
FROM Orders
GROUP BY Drug_Name
ORDER BY COUNT(*) DESC
SET ROWCOUNT 0
SELECT ...
FROM ... x JOIN #t t ON t.Drug_Name = x.Drug_Name
WHERE ...
I have a couple of concerns, though.
If you can live with the issues raised by the article Frank noted, then using dynamic SQL and the WITH TIES option would probably be best. I can think ways to mimic WITH TIES without using dynamic SQL, but none of them are pretty.
--Jonathan
--Jonathan
December 5, 2003 at 6:52 am
What are sticklers?
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 5, 2003 at 6:53 am
Forget it, I had a typo when searching my online dictionary
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 5, 2003 at 7:11 am
quote:
Forget it, I had a typo when searching my online dictionaryFrank
Just trying to expand your vocabulary, Frank.
--Jonathan
--Jonathan
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply