Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 2nd, 2005, 06:58 PM
csillagyitzik csillagyitzik is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 39 csillagyitzik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 49 m 40 sec
Reputation Power: 5
set variable in stored procedure

can I set a value to a variable when the table name and the column are variable? I tried this way and got an error...

select @cur_max_value = MAX(@cur_field) FROM @cur_table

Reply With Quote
  #2  
Old August 3rd, 2005, 08:26 AM
yogaboy yogaboy is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Near London
Posts: 112 yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 37 m 29 sec
Reputation Power: 5
Quote:
Originally Posted by csillagyitzik
can I set a value to a variable when the table name and the column are variable? I tried this way and got an error...

select @cur_max_value = MAX(@cur_field) FROM @cur_table


You could try making the right hand side a function and then using

select @cur_max_value = SELECT * FROM dbo.myMAXfunction(@cur_field, @cur_table)

I've used a sql_variant datatype below because I don't know what you might be maxxing, and you don't have to use a table function (i think) but it's the first thing I thought of.

CREATE FUNCTION dbo.myMAXfunction
(
@cur_field nvarchar(200),
@cur_table nvarchar(200)
)
RETURNS @myTable TABLE
(
[maxcol] sql_variant
)
AS
BEGIN
INSERT @myTable
SELECT MAX(@cur_field) FROM @cur_table
RETURN
END

Reply With Quote
  #3  
Old August 3rd, 2005, 09:39 AM
csillagyitzik csillagyitzik is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 39 csillagyitzik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 49 m 40 sec
Reputation Power: 5
I tried to run the following function (in query analyzer):
Code:
CREATE FUNCTION GET_MAX_VALUE 
(
	@cur_field AS VARCHAR(50), 
	@cur_table AS VARCHAR(50)
)
RETURNS @myTable TABLE
(
[maxcol] INT
)
AS
BEGIN
	INSERT @myTable
	SELECT MAX(@cur_field) FROM @cur_table
	RETURN
END


But I'm getting this error: "Must declare the variable '@cur_table'."

Reply With Quote
  #4  
Old August 3rd, 2005, 09:47 AM
yogaboy yogaboy is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Near London
Posts: 112 yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 37 m 29 sec
Reputation Power: 5
remove the AS after each variable name and see what happens. I'd also recommend putting dbo. in front of all sps and udfs

CREATE FUNCTION dbo.GET_MAX_VALUE
(
@cur_field VARCHAR(50),
@cur_table VARCHAR(50)
)

Reply With Quote
  #5  
Old August 3rd, 2005, 10:36 AM
csillagyitzik csillagyitzik is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 39 csillagyitzik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 49 m 40 sec
Reputation Power: 5
Still doesn't work :-(


Reply With Quote
  #6  
Old August 3rd, 2005, 11:45 AM
yogaboy yogaboy is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Near London
Posts: 112 yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 37 m 29 sec
Reputation Power: 5
Try this

DECLARE @sQuery nvarchar(200)

SET sQuery = 'SELECT MAX(' + @cur_field +') FROM ' + @cur_table

EXEC (@sQuery)

Reply With Quote
  #7  
Old August 3rd, 2005, 11:52 AM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
ASP Free God 16th Plane (12500 - 12999 posts)
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,867 mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)mehere User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 4 Weeks 15 h 21 m 57 sec
Reputation Power: 1762
Try this:
Code:
CREATE FUNCTION dbo.GET_MAX_VALUE
(
DECLARE @cur_field VARCHAR(50), 
DECLARE @cur_table VARCHAR(50)
)
__________________
Come JOIN the party!!!

Quote of the Month:
Retirement: Because you've given so much of yourself to the company that you don't have anything left we can use.

Questions to Ponder:
What do you do when you see an endangered animal eating an endangered plant?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright© 2008 sbenj69

Last edited by mehere : August 3rd, 2005 at 12:19 PM.

Reply With Quote
  #8  
Old August 3rd, 2005, 12:48 PM
yogaboy yogaboy is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: Near London
Posts: 112 yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level)yogaboy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 37 m 29 sec
Reputation Power: 5
Are you using the database with an application or a website? If so this becomes a lot easier because you can return the value back to the application and store it in a variable there. Otherwise I'm not sure it can be done using pure sql server.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > set variable in stored procedure


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT