If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Stored Procedure Create Table from Variable

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-06-03, 09:17
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
Stored Procedure Create Table from Variable

I want to create a table from a stored procedure where the name is passed to it:

I have this:
************************************************** ********

CREATE PROCEDURE sp_TP_CreateTable

(
@TABLENAME as varchar
)

AS

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[@TABLENAME]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[@TABLENAME]

CREATE TABLE [dbo].[@TABLENAME] (
[Description] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Catalog Version] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Mailed] [float] NULL ,
[Sales] [money] NULL ,
[Production $] [int] NOT NULL ,
[Orders] [float] NULL ,
[Response] [float] NULL ,
[Response of Test Control] [int] NOT NULL ,
[Average Invoice] [float] NULL ,
[SMP] [float] NULL ,
[SMP of Test Control] [int] NOT NULL ,
[Catalog Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Brand] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Drop Date] [smalldatetime] NULL
) ON [PRIMARY]
GO


But it's createing a table called @TABLENAME How can I get this to work?

Thanks,

Ken
Reply With Quote
  #2 (permalink)  
Old 02-06-03, 09:23
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: Stored Procedure Create Table from Variable

This is just a guess as I don't know SQL Server at all well, but I imagine all those horrible [square brackets] are there to show that values are literals, so maybe the correct syntax would be:

CREATE TABLE [dbo].@TABLENAME (

...

Or maybe I'm totally wrong!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 02-06-03, 09:28
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
Tried that... dbo.@TABLENAME

I get and error incorrect syntax near @TABLENAME
Reply With Quote
  #4 (permalink)  
Old 02-06-03, 09:29
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally posted by GA_KEN
Tried that... dbo.@TABLENAME

I get and error incorrect syntax near @TABLENAME
Why dbo.@TABLENAME and not [dbo].@TABLENAME ?
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 02-06-03, 09:32
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
Unhappy

I tried it both ways and got the same error message.

There has got to be a way to do this. I suppose I can use vb to do what I want, but the stored procedure seemed the logical way to do it in the first place.
Reply With Quote
  #6 (permalink)  
Old 02-06-03, 09:41
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Found this via Google:

A common question asked of SQL Team is "How do I write a stored procedure that will create a table/database. I want to pass in the name"

SQL Server will not allow this

Create Table @TableName (
ID int NOT NULL Primary Key,
FieldName VarChar(10)
)

Once again, dynamic SQL to the rescue

Declare @SQL VarChar(1000)

SELECT @SQL = 'Create Table ' + @TableName + '('
SELECT @SQL = @SQL + 'ID int NOT NULL Primary Key, FieldName VarChar(10))'

Exec (@SQL)

URL: http://www.sqlteam.com/item.asp?ItemID=4619
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #7 (permalink)  
Old 02-06-03, 09:42
GA_KEN GA_KEN is offline
Registered User
 
Join Date: Jan 2003
Posts: 126
THIS WORKED!

I tried this, and it worked:

Code:
CREATE PROCEDURE sp_TP_CreateTable

(
@TABLENAME as varchar (50)
)

AS
DECLARE @SQL varchar(2000)
SET @SQL = "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[@TABLENAME]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[@TABLENAME]

CREATE TABLE [dbo].[" + @TABLENAME + "] (
	[Description] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[Catalog Version] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[Mailed] [float] NULL ,
	[Sales] [money] NULL ,
	[Production $] [int] NOT NULL ,
	[Orders] [float] NULL ,
	[Response] [float] NULL ,
	[Response of Test Control] [int] NOT NULL ,
	[Average Invoice] [float] NULL ,
	[SMP] [float] NULL ,
	[SMP of Test Control] [int] NOT NULL ,
	[Catalog Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[Brand] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[Drop Date] [smalldatetime] NULL 
	) ON [PRIMARY]"

EXEC(@SQL)
GO
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On