Populate a temporary table with data ordered randomly???

Okay,

I have a stored procedure. In this stored procedure I have created a temporary table..

...Now I need to populate this temp table with data from another table. BUT I need to randomly order the data before populating the temp table?? Can anyone help with this. My simple stored procedure looks like this:

CREATE PROCEDURE sp_CreateRestTable

AS

CREATE TABLE #temp_tbl_Restaurants (
RestID int,
RestName varchar(50),
ImageURL varchar(50) )


INSERT INTO #temp_tbl_Restaurants (RestID, RestName, ImageURL)  (SELECT RestID, RestName, ImageURL FROM tbl_Restaurants  ORDER BY NEWID() )

GO

Trouble is the insert clause doesn't like the order by part??? Any help would be greatly appreciated!!

Joe
swerver69Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DonKronosCommented:
Since there is no concept of order in a relational table why are you trying to order the inserts????
swerver69Author Commented:
Thanx for your reply..........My delimma is..........

I have to display a whole bunch of images on a webpage randomly built in asp.net vb. The only real way I can do this is with a datalist and the datalist object does not allow paganation.
I can write my SELECT statement to generate the dataset randomly using the ORDER BY NEWID() clause.

I've written a routine to allow kinda psuedo pagination of my datalist. But to do this I have to rebind the data everytime the function is called. So if you click on "page two" instead of getting the next ten records from my randomly generated dataset, I get ten MORE records because the dataset gets rebuilt.

Thats why I had the idea of creating a temp table first and scrabbling the records there. Then I would have a stable table to generate my dataset from..

Any ideas???
Scott PletcherSenior DBACommented:
The extra parentheses may be "confusing" SQL, please try this:


INSERT INTO #temp_tbl_Restaurants (RestID, RestName, ImageURL)  
SELECT RestID, RestName, ImageURL
FROM tbl_Restaurants  
ORDER BY NEWID()

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

swerver69Author Commented:
ScottPletcher,

Thanx, that worked. Can you help me further (or if not I'll happily award you the points and post again). Works just fine if I use the query analyser.....

Now I can't seem to trigger the stored procedure to fire (regularly) in my page code. I've got a button_click event that is supposed to fire the stored procedure and thus create the temp table. For now, in testing I'm simply creating a new table (the temp part will come later)... I guess I need some catch or something.

Anyway, I've built a sub routine and wired the routine to fire on button click if login credentials match:

.........................................................

      Sub CreateTempTable()

            
            Dim strConnString as String = System.Configuration.ConfigurationSettings.AppSettings("DBConn")
            Dim objConn as New SqlConnection(strConnString)
            Const strSql as String = "sp_CreateRestTable"
            Dim objCmd as New SqlCommand(strSql, objConn)
            objCmd.CommandType = CommandType.StoredProcedure
            objConn.Open()

            ObjConn.Close()
            
      
      End Sub

................................................................................


..but its hit or miss. Can't seem to get it to work in a regular way. I'm sure my Sub () leaves alot to be desired. I don't know. What do you think??
Scott PletcherSenior DBACommented:
Sorry, I would help if I could, but I'm now just a SQL guy, not a VB/pgmr type any more :-( .
swerver69Author Commented:
jeez, Im an idiot.................don't know how I missed that..........thanx
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.