473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

table name as parameter

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:
Expand|Select|Wrap|Line Numbers
  1. create function percentile (@src as varchar(64), @col as varchar(64), @perc as float) returns float
  2. begin
  3. declare @rowcnt int
  4. declare @index float
  5. declare @out float
  6.  
  7. select @rowcnt=count(@col) from @src
  8. declare percentile_cursor cursor local for
  9.     select @col from @src order by 1 asc
  10. open percentile_cursor
  11. set @index=cast((cast(@rowcnt as float)*perc) as int)
  12. fetch absolute @index from percentile_cursor into @out
  13. return @out
  14. end
  15.  
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?
Jul 31 '07 #1
4 3312
DonlonP
25 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:
Expand|Select|Wrap|Line Numbers
  1. create function percentile (@src as varchar(64), @col as varchar(64), @perc as float) returns float
  2. begin
  3. declare @rowcnt int
  4. declare @index float
  5. declare @out float
  6.  
  7. select @rowcnt=count(@col) from @src
  8. declare percentile_cursor cursor local for
  9.     select @col from @src order by 1 asc
  10. open percentile_cursor
  11. set @index=cast((cast(@rowcnt as float)*perc) as int)
  12. fetch absolute @index from percentile_cursor into @out
  13. return @out
  14. end
  15.  
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)
Aug 1 '07 #2
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?
Aug 1 '07 #3
azimmer
200 Recognized Expert New Member
For what it may be worth here's the solution (non-interpolating, no error handling):
Expand|Select|Wrap|Line Numbers
  1. IF EXISTS (SELECT name FROM sysobjects 
  2.       WHERE name = 'percentile' and type = 'P')
  3. DROP PROCEDURE percentile
  4. GO
  5.  
  6. create procedure percentile (@src as varchar(64), @col as varchar(64), @perc as float, @out as float OUTPUT)
  7. as
  8. declare @rowcnt int
  9. declare @result float
  10. declare @sql as nvarchar(400)
  11.  
  12. set @sql = 'select @rowcnt=count(*) from ' + @src
  13. --print @sql
  14. EXEC sp_executesql @sql, N'@rowcnt INT OUTPUT', @rowcnt OUTPUT
  15.  
  16. set @sql = 'declare @index int' + char(13)
  17.     + 'declare percentile_cursor cursor local scroll for select '+@col+' from '+@src+' order by 1 asc'  + char(13)
  18.     + 'open percentile_cursor' + char(13)
  19.     + 'set @index=cast((1.0*cast('+cast(@rowcnt as varchar)+' as float)*cast('+cast(@perc as varchar)+' as float)) as int)' + char(13)
  20. --    + 'print @index' + char(13)
  21.     + 'fetch absolute @index from percentile_cursor into @result'
  22. --print @sql
  23. EXEC sp_executesql @sql, N'@result float OUTPUT', @result OUTPUT
  24.  
  25. set @out = @result
  26. GO
  27.  
Notes:
  1. Will not work as a function only as a stored procedure
  2. Uncomment comments to see debug messages
  3. Ugly code, I hate it. If you have any better, please, let me know.
Aug 2 '07 #4
Motoma
3,237 Recognized Expert Specialist
Hey azimmer, you should turn your PM on. Let me know when you do.
Aug 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

35
3262
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 =...
6
6291
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. ...
6
7579
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...
0
3562
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)
7
6978
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
2
1780
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...
1
16951
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
0
4052
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...
3
2838
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
3
4205
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">
0
8326
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,...
1
8522
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,...
0
8622
tracyyun
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...
0
7355
agi2029
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...
1
6177
isladogs
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...
0
5647
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();...
0
4173
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...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.