Tuesday, March 20, 2012

Save The Newbie! SQL help

Hey guys. I have been given the task of converting from oracle to microsoft SQL syntax.

I have this oracle code:
CREATE OR REPLACE PROCEDURE BA_DROP_TABLE (p_TableName IN varchar2) IS
i int;
BEGIN
select count(*) into i from user_tables where table_name = p_TableName;
IF i=1 THEN
EXECUTE IMMEDIATE 'DROP TABLE ' || p_TableName || ' CASCADE CONSTRAINTS';
END IF;
END;
/

I tried to convert it, and this is what I came up with:
if (object_id ('BA_DROP_TABLE') is not null)
drop proc BA_DROP_TABLE
go

CREATE PROCEDURE BA_DROP_TABLE @.p_TableName varchar(30)
AS
DECLARE @.i integer
BEGIN
select @.i = count(*) from user_tables where table_name = p_TableName
IF @.i=1
DROP TABLE p_TableName
END

This doesn't work. I think the problem has something to do with not being able to drop the table because its a parameter. I've heard talk of dynamic sql.

Someone PLEASE help a guy out!

Thanksreplace "DROP TABLE p_TableName" with "exec ('drop table ' + @.p_TableName)

Make sure the user has DROP OBJECTS permission.|||Now I get this error:

Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object 'BA_DROP_TABLE'. The stored procedure will still be created.

No comments:

Post a Comment