|
-
Aug 1st, 2000, 01:32 PM
#1
Thread Starter
Addicted Member
I am kind of new to SPROCs, can anyone see why won't this work?
Code:
CREATE PROCEDURE sp_emptytable
@MyTable varchar(50)
AS
DELETE FROM @MyTable WHERE id > 0
I am entering the SPROC using SQL Server Enterprise Manager.
-
Aug 1st, 2000, 02:38 PM
#2
Hyperactive Member
A stored procedure is a precompiled SQL statement. While SQL is a language, it is not a programming language; and you have to use valid SQL syntax. Since @MyTable is an invalid object in your database, the SQL cannot be precompiled. With MS SQL Server you can use a sexy CASE statement or a series of IF statements to create a psuedo-generic stored procedure for deleting.
Assuming MyNullTable, MyCatTable and MyDogView are valid objects in your database:
Code:
CREATE PROCEDURE sp_emptytable (@MyTable varchar(50) = null) AS
IF ( (@MyTable = null )
BEGIN
DELETE FROM MyNullTable WHERE id > 0
RETURN
END
IF ( (@MyTable = 'Cat' )
BEGIN
-- mo' betta way to empty the entire table...
TRUNCATE TABLE MyCatTable
RETURN
END
IF ( (@MyTable = 'Dog' )
DELETE FROM MyDogView WHERE id > 0
RETURN
-
Aug 1st, 2000, 03:51 PM
#3
Thread Starter
Addicted Member
-
Aug 1st, 2000, 11:05 PM
#4
Guru
This works fine in SQL 7
Code:
CREATE PROCEDURE spu_NukeTable
@TableName varchar(200)
AS
exec('Truncate Table ' + @TableName)
I'm pretty sure you can pass table/column names as parameters to a SP as well, if I find that info I'll be sure to post it....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|