Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 May 25th, 2005, 03:40 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anubhav.kumar
Default Using a Variable in Select Into

Hi to all,

I want to create a table within a stored proc from an existing table.
I am using a 'Select Into' query to achieve this.
The problem is that the name of new table will be decided at the runtime ( through an input parameter) If i give variable name at the place of table name in the Select Into query , my procedure doesn't get compiled.

Something like this

************************************************** ***********
 select * into @tbName from @BIGTABLE where sno>=@fromSno and sno<=@toSno
************************************************** ************

Both @BIGTABLE and @tbName are input parameters of type varchar

Please help

Anubhav Kumar
__________________
Anubhav Kumar
http://anubhavtimepass.blogspot.com/
 
Old May 25th, 2005, 12:55 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

CREATE PROCEDURE Testing123
@FROMTableName varchar(50),
@ToTableName varchar(50)
AS
BEGIN

DECLARE @SQL nVarchar(1000)
SET @SQL = 'SELECT * INTO ' + @ToTableName + ' FROM ' + @FromTableName
exec sp_executesql @SQL

END

--execute this procedure
exec Testing123 @FROMTableName='Customer', @ToTableName='newtable'

 
Old May 26th, 2005, 11:02 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anubhav.kumar
Default

Thanks a lot. it was needed urgently

Anubhav Kumar





Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT VARIABLE Number of columns drani Oracle 4 September 21st, 2008 01:12 AM
passing javascript variable in xsl:value-of select eruditionist XSLT 2 September 19th, 2008 05:23 PM
Using variable as fieldname in SELECT statement elygp SQL Server 2000 3 April 26th, 2007 10:14 AM
Setting a variable using Select Case Adam ASP.NET 1.0 and 1.1 Basics 2 September 4th, 2004 02:09 PM
Using a variable in a SELECT WHERE query scottiegirl MySQL 25 April 15th, 2004 05:28 PM





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