Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 17th, 2006, 02:46 AM
Registered User
 
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get random column values?

Hi there,

I need to do something which sounds quite simple yet the solution is eluding me. I want to create a stored proc or function that takes a table name and column name as inputs and returns a random value from the table and column specified.

My problem right now is figuring out how to return the value from a dynamic SQL statement to a variable... as a function won't let me create a table to hold it in I'm not sure the best way to go about this. Stored proc with an output parameter?

Help much appreciated.

seis

 
Old November 19th, 2006, 01:38 PM
Registered User
 
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff,

I need it work like this though...

SELECT @OUTPUT_VAR = TOP 1 @COLUMN_NAME
FROM @TABLE_NAME
ORDER BY NEWID()

I make that all part of a string and tried EXEC(@SQLString) but I can't seem to make it work. Trips up on the TOP 1 for some reason when it's dynamic SQL.

 
Old November 20th, 2006, 11:19 AM
Registered User
 
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's part of a script that I'm using to help build a data dictionary, and something I'd like to gather are some example column values. I've tried sp_executeSQL already and ran into the same problems I did using an EXEC() command. I will likely end up gathering the values manually as it's just not going to be worthwhile to put too much time into trying to figure this script out!

Thanks anyways Jeff.

 
Old November 21st, 2006, 06:02 PM
Registered User
 
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your help Jeff. My needs have changed slightly - as you might expect one example value isn't going to be enough so I'm going to try to tweak this script into something outputs 3 example values from the column put into a string like so... "example1, example2, example3".

Based on the code you've provided me with I think that should be doable, though truthfully I'm still a little confused with this code:

EXEC dbo.sp_ExecuteSQL @SQL,
        N'@Output_Var VARCHAR(8000) OUT',
        @Output_Var OUT

I will play more and post should I run into more trouble. Not reading or not playing with the code enough may be true - time is sometimes a luxury I don't have on top of the rest of "life" right now.

Your help IS appreciated though - thanks!

seis






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the Sum of all the values in a column.. jhansib4u ADO.NET 1 December 1st, 2007 09:15 PM
ASP - Random values from database cancer10 Classic ASP Databases 1 December 21st, 2006 01:24 PM
summing column values hydriswall PHP Databases 1 December 8th, 2006 04:54 AM
Concatenating column values arnabghosh Access 1 September 21st, 2005 06:45 AM
Generating random values gatsby VB How-To 3 August 4th, 2004 02:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.