473,396 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to pass user defined variable to Select statement

In myTable, there're several columns as Avg1, Avg2 ....Avg92, they all are float type.

in my SQL code:

Expand|Select|Wrap|Line Numbers
  1. declare @counter int
  2. declare @parm nvarchar(20)
  3. set @counter=0
  4.  
  5. while @counter<10
  6. set @counter=@counter+1
  7. set @parm='Avg'+ cast(@counter as char)
  8.  
  9. Select @parm from myTable   ////// error happens here
  10.  
The last line of the code gives cast error.
How can I get the variable passed correctly into select statement?
thanks in advance.
Nov 19 '07 #1
3 2734
deepuv04
227 Expert 100+
In myTable, there're several columns as Avg1, Avg2 ....Avg92, they all are float type.

in my SQL code:

Expand|Select|Wrap|Line Numbers
  1. declare @counter int
  2. declare @parm nvarchar(20)
  3. set @counter=0
  4.  
  5. while @counter<10
  6. set @counter=@counter+1
  7. set @parm='Avg'+ cast(@counter as char)
  8.  
  9. Select @parm from myTable   ////// error happens here
  10.  
The last line of the code gives cast error.
How can I get the variable passed correctly into select statement?
thanks in advance.
use the following query, hope will help you:


declare @counter int
declare @parm nvarchar(20)
set @counter=0
declare @sSql varchar(1000)
while @counter<10
Begin
set @counter=@counter+1
set @parm='Avg'+ cast(@counter as char)

set @sSql = 'Select ' + @parm + ' from myTable '
exec (@sSql)
end
Nov 20 '07 #2
Thank you, deepuv04.
Dec 4 '07 #3
in my SQL code:

Expand|Select|Wrap|Line Numbers
  1. declare @counter int
  2. declare @parm nvarchar(20)
  3. declare @avg float
  4. set @counter=0
  5.  
  6. while @counter<10
  7. set @counter=@counter+1
  8. set @parm='Avg'+ cast(@counter as char)
  9.  
  10. Select @float=AVG(cast(@parm as float)) from myTable   ////// error happens here
  11.  
The last line of the code gives cast error.
How can I get the variable passed correctly into select statement?
thanks in advance.[/quote]
Dec 4 '07 #4

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

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
11
by: DFS | last post by:
Architecture: Access 2003 client, Oracle 9i repository, no Access security in place, ODBC linked tables. 100 or so users, in 3 or 4 groups (Oracle roles actually): Admins, Updaters and ReadOnly....
5
by: Nate | last post by:
Is there any way within asp.net to pass user defined parameters/variables to an executable? Much thanks. nate
9
by: Frank Rizzo | last post by:
I've got a number of user controls on the web page. How can I pass some data to it? I don't see where the user control is instantiated in the page code-behind page. Thanks.
6
by: Grainne Reilly | last post by:
I am new to PostgreSQL and am porting some scripts written for MySQL over to psql. There is one MySQL feature which I cannot find in psql - user defined SQL variables. In MySQL I can use these to...
2
by: phil | last post by:
Hi, I want to put a recordset in a gridview but i don't know how to pass the value of the variable in the 'where' statement. The value of the variable is set in the code-behind. i added a tag...
4
by: Mahesh S | last post by:
Hi I want to write a user defined function which does the following. 1. Take a string value as input parameter 2. Should execute a sql statement: select col1, col2, col3, col4 from table_name...
5
by: jjeanj1 | last post by:
I am having issue using a user defined variable i an a mysql statement. here is the issue that i am facing: i have a variable called $date in php.This variable is assigned a valued from a dropdown...
14
by: bill | last post by:
Can someone please show me an example of passing a string value into an sql statement in vb 2005? Something like this is what I'm after: Dim sqlButton1 As String = "Select * from tblAssets where...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.