Tuesday, March 27, 2012

Create a table using a Stored Procedure

Hi,
I am someone new to SQL Server in general. I am using the Trial Version of
SQL Server 2005. I want to create a stored procedure to create a table. The
table name would be the only parameter of the stored procedure. I was
thinking of something like this (I dont know if even the syntax is parcially
correct, sorry first time with Stored Procedures):
CREATE PROCEDURE CrearTabla
(
@.NyA nvarchar(100)
)
AS
BEGIN
CREATE TABLE dbo.@.NyA
(
Table's Columns
)
END
But SQL Server says that there is an error "near" @.NyA in the line with the
CREATE TABLE.
Any thoughts?
Thanks
"Cargula" <Cargula@.discussions.microsoft.com> wrote in message
news:99F1F504-8C88-47BD-A0AA-A593DBA2B370@.microsoft.com...
> I am someone new to SQL Server in general. I am using the Trial Version of
> SQL Server 2005. I want to create a stored procedure to create a table.
> The
Why? There is probably a better way to do what you're trying to do.
Can you let us know what your requirements are?
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
|||In your exmaple you would create a table with the name @.Nya which is
not possible beacause @. is not allowed as part of an identifier. You
have to create a string which you can execute:
See below for an example
CREATE PROCEDURE CrearTabla
(
@.NyA nvarchar(100)
)
AS
BEGIN
DECLARE @.cmd as varchar (255)
SET @.cmd = 'CREATE TABLE dbo.'+ @.NyA + '
(
Table's Columns
) '
Exec @.cmd
END
|||Well, I have an application that erases all the data in certain columns of a
table when the users decides. I have alredy done this. My problem is that I
wish to backup the table (the whole table, it is small, so size isnt a
problem) before the app deletes that data. I was thinking of using stored
procedures to do this, but if there is another (hopefully more efficient) way
to do this, I would be extremely happy to hear it...
Thanks a lot
"Adam Machanic" wrote:

> "Cargula" <Cargula@.discussions.microsoft.com> wrote in message
> news:99F1F504-8C88-47BD-A0AA-A593DBA2B370@.microsoft.com...
> Why? There is probably a better way to do what you're trying to do.
> Can you let us know what your requirements are?
>
> --
> Adam Machanic
> Pro SQL Server 2005, available now
> http://www.apress.com/book/bookDisplay.html?bID=457
> --
>
>
|||"Cargula" <Cargula@.discussions.microsoft.com> wrote in message
news:2B8CE28B-3D8B-4214-B482-949CF7448CAC@.microsoft.com...
> Well, I have an application that erases all the data in certain columns of
> a
> table when the users decides. I have alredy done this. My problem is that
> I
> wish to backup the table (the whole table, it is small, so size isnt a
> problem) before the app deletes that data. I was thinking of using stored
> procedures to do this, but if there is another (hopefully more efficient)
> way
> to do this, I would be extremely happy to hear it...
The usual way this is handled is by creating an after trigger, for
delete, that inserts all of the rows from the 'deleted' table into a backup
table that's already been created -- this way, you'll keep a running log of
what's been deleted, in a single place. Much more manageable than creating
a new table every single time.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457

No comments:

Post a Comment