Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 April 13th, 2005, 02:12 AM
Authorized User
 
Join Date: Apr 2004
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help me !

 I have about 10 Tables(Table1 to Table10)
And then what i want is creat a Procedure like that:

CREATE PROCEDURE Pro1
 @TableIndex Int
AS
Case @TableIndex
  WHEN @TableIndex=1
SELECT * FROM Table1
 WHEN @TableIndex=2
SELECT * FROM Table2
 WHEN @TableIndex=3
SELECT * FROM Table3

And so on


Can anyone tell me What must i do ?

Thanks very much





 
Old April 13th, 2005, 03:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

EXEC('SELECT * FROM Table'+CAST(@TableIndex AS VARCHAR))

not much point having that in a stored procedure though
 
Old April 13th, 2005, 04:14 AM
Authorized User
 
Join Date: Apr 2004
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks to pgtip
But your code does not work !
does any good idea !

 
Old April 13th, 2005, 06:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

sorry, try this variation instead
DECLARE @sql varchar(1024)
SET @sql='SELECT * FROM Table'+CAST(@TableIndex AS VARCHAR)
EXEC(@sql)
 
Old April 13th, 2005, 08:09 PM
Authorized User
 
Join Date: Apr 2004
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks to pgtips very much !
your code work correctly . But If i have many table with diffirent name(not from table1 to table10) Then you use this script

CREATE PROCEDURE WhatTable

 @sql varchar(1024),
 @Tablename varchar(50)

AS
SET @sql='SELECT * FROM '+ @Tablename
EXEC(@sql)










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