I have a stored procedure to create a table. In my program I want the user
to name the table to be created, therefore I pass a parameter to the SP for
the table name. I cannot get it to work.
It creates a table called "@NewTableName" .
Any ideas?
CREATE PROCEDURE dbo.sp_FFProduc tion_CreateTabl e
(
@NewTableName nvarchar(128)
)
AS
CREATE TABLE [dbo].[@NewTableName]
(
[ProductionID] [int] IDENTITY (1, 1) NOT NULL ,
[ProductionName] [nvarchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NOT
NULL ,
[ProductionDate] [datetime] NOT NULL ,
[DocumentID] [int] NOT NULL,
[FileName] [nvarchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NOT NULL
)
ON [PRIMARY]
GO 4 4433
Why would you want to create permanent tables at runtime? What is the
point have having multiple tables of the same structure? Why not just
add a user_name column to this table structure and then have all users
point to the same table?
The way to do what you have asked for is to use dynamic SQL. There are
lots of reasons why that is a really bad idea but then creating tables
at runtime seems like a pretty bad idea already!
See: http://www.sommarskog.se/dynamic_sql.html
for more info.
P.S. You have used the prefix sp_ for your stored procedure. sp_ is
reserved for system stored procedures. It has special meaning and
should not be used except for procs in Master.
--
David Portas
SQL Server MVP
--
Hello,
Michael Jackson wrote: I have a stored procedure to create a table. In my program I want the user to name the table to be created, therefore I pass a parameter to the SP for the table name. I cannot get it to work.
It creates a table called "@NewTableName" .
Any ideas?
CREATE PROCEDURE dbo.sp_FFProduc tion_CreateTabl e ( @NewTableName nvarchar(128) ) AS CREATE TABLE [dbo].[@NewTableName] ( [ProductionID] [int] IDENTITY (1, 1) NOT NULL , [ProductionName] [nvarchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NOT NULL , [ProductionDate] [datetime] NOT NULL , [DocumentID] [int] NOT NULL, [FileName] [nvarchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NOT NULL ) ON [PRIMARY] GO
The only way to create a dynamically named table is using EXECUTE or
sp_executesql stored procedure.
CREATE PROCEDURE dbo.sp_FFProduc tion_CreateTabl e ( @NewTableName nvarchar(128) ) AS EXECUTE ('CREATE TABLE [dbo].[' + @NewTableName + '] ( [ProductionID] [int] IDENTITY (1, 1) NOT NULL , [ProductionName] [nvarchar] (50) COLLATE
SQL_Latin1_Gene ral_CP1_CI_AS NOT NULL , [ProductionDate] [datetime] NOT NULL , [DocumentID] [int] NOT NULL, [FileName] [nvarchar] (255) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NOT
NULL ) ON [PRIMARY]' GO
But these methods have the disadvantage, that the user calling the
SP must have the create table privilege or in your case must be member
of the db_owner role.
You should avoid creating tables, if this is possible in your
application. For example you could use an additional column in your table.
Rainer
Thanks for the help.
A table has to be produced which can be 'bundled up" with related files and
given to a client. Each table stands alone and cannot contain info from
other process.
"David Portas" <RE************ *************** *@acm.org> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com... Why would you want to create permanent tables at runtime? What is the point have having multiple tables of the same structure? Why not just add a user_name column to this table structure and then have all users point to the same table?
The way to do what you have asked for is to use dynamic SQL. There are lots of reasons why that is a really bad idea but then creating tables at runtime seems like a pretty bad idea already!
See: http://www.sommarskog.se/dynamic_sql.html for more info.
P.S. You have used the prefix sp_ for your stored procedure. sp_ is reserved for system stored procedures. It has special meaning and should not be used except for procs in Master.
-- David Portas SQL Server MVP --
In that case I suggest that sp_rename would be an eaiser method than
dynamic SQL.
CREATE TABLE new_table (...
EXEC sp_rename 'new_table', @NewTableName, 'OBJECT'
Or consider using DTS.
--
David Portas
SQL Server MVP
-- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Phil Powell |
last post by:
create table if not exists nnet_produkt_varegruppe (
nnet_produkt_varegruppe_id int not null auto_increment,
primary key(nnet_produkt_varegruppe_id),
nnet_produkt_varegruppe_navn varchar(255) not null
);
create table if not exists nnet_produkt_farge (
nnet_produkt_farge_id int not null auto_increment,
primary key(nnet_produkt_farge_id),
|
by: dev |
last post by:
how create a temp table as a copy of a existing table and then update1 field
and insert the hole temp table back in the existing table?
please any help?
if i have 10 fields in 1 record and about 100 records and a field.status=1
in a existing_table and
i want to create a temp_table with all the recordse and values of the
existing_table and then update the field.status to 2 and insert in 1 query
the temp_table in the existing_table
|
by: Wolfgang Kreuzer |
last post by:
Hello all,
I have two tables - Projects and ProjectStruct
Table Projects contains master records of the projects, ProjectStruct
allows to define a project herarchie and contains the fields
PrjStructId, ProjectId, PrjStructName, ..., ParentId
PrjStructParent contains a reference to the parent or to itselves if
record is top-level-record for a project.
|
by: poohnie08 |
last post by:
i have a excel spreadsheet showing staff name, date,work hour, ot
hour, slot1, slot2, slot3, slot4 and others). The "()" will keep
repeating from day 1 until end of month.
eg in excel spreadsheet,
==============================================================================
A1 |A2 A3 A4 A5 A6 A7 A8 |A9
A10 A11
| 01/02/04 |02/02/04
StaffName |Work Hr OT Hr Slot1...
|
by: flkeyman |
last post by:
Work in legal office. Trying to create solid designed database
structure. This is list of tables w/fields and primary keys. Any
comments/advice greatly appreciated.
tbl-Defendants
CaseNumber (primary key)
FirstName
MiddleName
LastName
| |
by: Peter Nurse |
last post by:
For reasons that are not relevant (though I explain them below *), I
want, for all my users whatever privelige level, an SP which creates
and inserts into a temporary table and then another SP which reads and
drops the same temporary table.
My users are not able to create dbo tables (eg dbo.tblTest), but are
permitted to create tables under their own user (eg MyUser.tblTest). I
have found that I can achieve my aim by using code like...
|
by: max |
last post by:
Hello,
I am a newbye, and I'm trying to write a simple application.
I have five tables with three columns; all tables are identical; I need to
change some data in the first table and let VB updates the same data in all
other four tables in the right places.
I know it would be possible by using the ForeignKeyConstraint object. I have
created the tables using the DataSet Visual Tool and I know it doesn't
create any ForeignKeyConstraint obj....
|
by: etuncer |
last post by:
Hello All,
I have Access 2003, and am trying to build a database for my small
company. I want to be able to create a word document based on the data
entered through a form. the real question is this: can Access create
the document and place it as an OLE object to the relevant table?
Any help is greatly appreciated.
Ricky
|
by: lakuma |
last post by:
Hi,
I have a table called A (say) with columns called name, place, animal and thing.
I would want to write an on insert trigger on this table, which would create a table with the name of the value entered in the name column.
Let's say a new column is entered with the value of name column as mickey. I want to create a table called mickey, with the help of triggers.
I'm really new to this and i would like some help. Thanks in advance for...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |