azimmer 200
Recognized Expert New Member
I need a function that can calculate percentiles of various tables and columns. I cannot seem, however, to be able to pass table name as a parameter to the function.
Here's the code: -
create function percentile (@src as varchar(64), @col as varchar(64), @perc as float) returns float
-
begin
-
declare @rowcnt int
-
declare @index float
-
declare @out float
-
-
select @rowcnt=count(@col) from @src
-
declare percentile_cursor cursor local for
-
select @col from @src order by 1 asc
-
open percentile_cursor
-
set @index=cast((cast(@rowcnt as float)*perc) as int)
-
fetch absolute @index from percentile_cursor into @out
-
return @out
-
end
-
It won't compile (says: Server: Msg 137, Level 15, State 2, Procedure percentile, Line 8 Must declare the variable '@src'. and for Line 9 the same).
If I replace @src with any tablename, it compiles OK. Where do I go wrong?
4 3312
I need a function that can calculate percentiles of various tables and columns. I cannot seem, however, to be able to pass table name as a parameter to the function.
Here's the code: -
create function percentile (@src as varchar(64), @col as varchar(64), @perc as float) returns float
-
begin
-
declare @rowcnt int
-
declare @index float
-
declare @out float
-
-
select @rowcnt=count(@col) from @src
-
declare percentile_cursor cursor local for
-
select @col from @src order by 1 asc
-
open percentile_cursor
-
set @index=cast((cast(@rowcnt as float)*perc) as int)
-
fetch absolute @index from percentile_cursor into @out
-
return @out
-
end
-
It won't compile (says: Server: Msg 137, Level 15, State 2, Procedure percentile, Line 8 Must declare the variable '@src'. and for Line 9 the same).
If I replace @src with any tablename, it compiles OK. Where do I go wrong?
You need to build your query as a string, inserting the name of your table and then execute it. I.e.
DECLARE @myQuery AS VARCHAR(200)
SET @myQuery = 'select @rowcnt=count(@ col) from ' + @src
EXEC (@myQuery)
azimmer 200
Recognized Expert New Member
You need to build your query as a string, inserting the name of your table and then execute it. I.e.
DECLARE @myQuery AS VARCHAR(200)
SET @myQuery = 'select @rowcnt=count(@ col) from ' + @src
EXEC (@myQuery)
Thx, that's OK for the select -- any idea with the cursor? build the whole process (i.e. multiple statements) as string? i'll try tonight but isn't there a more elegant way?
azimmer 200
Recognized Expert New Member
For what it may be worth here's the solution (non-interpolating, no error handling): -
IF EXISTS (SELECT name FROM sysobjects
-
WHERE name = 'percentile' and type = 'P')
-
DROP PROCEDURE percentile
-
GO
-
-
create procedure percentile (@src as varchar(64), @col as varchar(64), @perc as float, @out as float OUTPUT)
-
as
-
declare @rowcnt int
-
declare @result float
-
declare @sql as nvarchar(400)
-
-
set @sql = 'select @rowcnt=count(*) from ' + @src
-
--print @sql
-
EXEC sp_executesql @sql, N'@rowcnt INT OUTPUT', @rowcnt OUTPUT
-
-
set @sql = 'declare @index int' + char(13)
-
+ 'declare percentile_cursor cursor local scroll for select '+@col+' from '+@src+' order by 1 asc' + char(13)
-
+ 'open percentile_cursor' + char(13)
-
+ 'set @index=cast((1.0*cast('+cast(@rowcnt as varchar)+' as float)*cast('+cast(@perc as varchar)+' as float)) as int)' + char(13)
-
-- + 'print @index' + char(13)
-
+ 'fetch absolute @index from percentile_cursor into @result'
-
--print @sql
-
EXEC sp_executesql @sql, N'@result float OUTPUT', @result OUTPUT
-
-
set @out = @result
-
GO
-
Notes: - Will not work as a function only as a stored procedure
- Uncomment comments to see debug messages
- Ugly code, I hate it. If you have any better, please, let me know.
Motoma 3,237
Recognized Expert Specialist
Hey azimmer, you should turn your PM on. Let me know when you do.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: .:mmac:. |
last post by:
I have a bunch of files (Playlist files for media player) and I am trying to
create an automatically generated web page that includes the last 20 or 30
of these files. The files are created every week and are named XX-XX-XX.ASX
where the X's represent the date i.e. 05-22-05.asx
The files are a specific format and will always contain tags like the
following:
<TITLE>My media file title</TITLE>
<AUTHOR>Media file author</AUTHOR>
<Ref href =...
|
by: dharmadam |
last post by:
Is it possible to pass a column name or the order of the column name
in the DB2 table table function. For example, I want to update the
address of a person by passing one of the address column name like ZIP
CODE or ADDRESS LINE. I will call the function with three
parameter--UpdateAddress(5,zip_code,person_id) where 5 indicates
ZIP_CODE is the fifth column in the table. If 4 is passed, it
indicates the address line is to be updated.
...
|
by: Lewis Veale |
last post by:
I have an Access 2000 front-end pointing at a SQL Server backend, with
around 80 linked tables and views. I frequently need to point the
front-end at different versions of the back-end, and achieve this by
relinking the tables and views to use a different DSN.
I use the Linked Table Manager > Select All > and tick the 'always
prompt for a new location' box, then click OK. It correctly pops up
the 'select data source' box, from which I...
|
by: André |
last post by:
Hi,
Still the same (unsolved problem), but other error now.
I have a detailsview for inserting data, and a dropdownlist which
selectedValue must be used to fill one of the field of the detailsview.
Table 'pc' is related to table 'lok'.
I tried a lot of things, but still without succes. Here are the two methods
i tried with two different errors. It works only when i introduce directly a
value in "DefaultValue" (see below)
|
by: Serge Rielau |
last post by:
Hi all,
Following Ian's passionate postings on problems with ALTOBJ and the
alter table wizard in the control center I'll try to explain how to use
ALTOBJ with this thread. I'm not going to get into the GUI because it is
hard to describe in text.
First of all what is the purpose of ALTOBJ()?
This procedure was created mostly for ISVs who need to do produce change
scripts to upgrade application from release to release, but it can also
| |
by: teddymeu |
last post by:
Hi Guys, this is kinda complicated but ill do my best to explain.
I have two tables. products and categories. Products holds product info and an image, its primary key is ProductID. Category table holds a category name and description and a primary key CategoryID. The producs table holds a Foreign key field called CategoryID which points to the CategoryID Primary key of the catgory table.
Ok i have two asp.net 2 vb forms. One is using a...
|
by: since |
last post by:
I figured I would post my solution to the following.
Resizable column tables.
Search and replace values in a table. (IE only)
Scrollable tables.
Sortable tables.
It is based on a lot examples I found on the web. Works in IE and mozilla.
http://www.imaputz.com/cssStuff/bigFourVersion.html
|
by: troydixon |
last post by:
Hello,
I am new at this, and have been trying to insert data into a table by using the footer of a gridview (which I dont like) or by using a detials view on the same page that is doing the following:
1) gets query string from url and filters records (works great) via a Details View
2) shows notes that have been added to the record shown in the details view using a grid view, and it filters the data by looking at the ID that is selected in...
|
by: Andrea Raimondi |
last post by:
Hello peers!
I'm working on this application and I'm in need for some thoughtful advice
:-p
I have an SQLDataSource with params, select, etc.
One of my params is the table name, which can be programmatically set, this
is necessary because I may have a
simple table name or a union, hence I got to pick the correct one!
Unfortunately, I can't bind this parameter to a control, session or
|
by: azegurb |
last post by:
hi
I have just took from internet dinamic table. this table is dynamic and its rows dynamically can be increased.
but i would like how create SUM function that automatically sums each added row value (text value)
here is the code
if possible please help me
Thanks beforehand
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |