Monday, March 19, 2012

DROP ot TRUNCATE a table

How do I DROP or TRUNCATE a SQL Server's table in a Stored Procedure by
passing a parameter value for the tabble name; something like the following:
CREATE PROCEDURE [dbo].[MyTestTable]
@.MyTable nvarchar(50)=''
AS
If @.MyTable<>''
BEGIN
IF OBJECT_ID(@.MyTable) IS NOT NULL
TRUNCATE TABLE @.MyTable --DROP TABLE @.MyTable
END
GO
The code above returns error message on line:
TRUNCATE TABLE @.MyTableSaima wrote:
> How do I DROP or TRUNCATE a SQL Server's table in a Stored Procedure
> by passing a parameter value for the tabble name; something like the
> following:
> CREATE PROCEDURE [dbo].[MyTestTable]
> @.MyTable nvarchar(50)=''
> AS
> If @.MyTable<>''
> BEGIN
> IF OBJECT_ID(@.MyTable) IS NOT NULL
> TRUNCATE TABLE @.MyTable --DROP TABLE @.MyTable
> END
> GO
> The code above returns error message on line:
> TRUNCATE TABLE @.MyTable
Declare @.sql nvarchar(100)
Set @.sql = N'Truncate Table [' + @.MyTable + N']'
EXEC (@.sql)
David Gugick - SQL Server MVP
Quest Software

No comments:

Post a Comment