hi ppl ,
i'm trying to do a sp that does the same thing in 4 diferent tables...
the best was to create a sp, where the table name is a parameter...
what is the syntax to do that
i've done something like this
create sp_insertX (@table, @Cod, @descr)
AS
DECLARE @ErrorCode int
DECLARE @Result int
BEGIN TRANSACTION
IF @Cod IN (SELECT Code FROM @Table)
BEGIN
UPDATE @Table SET Descri = @Descr.
SET @ErrorCode = @@ERROR
IF (@ErrorCode <> 0 )
BEGIN
set @Result = ( -@ErrorCode )
ROLLBACK TRANSACTION
-- In case of ERROR RollBack and return the Error
Return @Result
END
set @Result=1
COMMIT TRANSACTION
RETURN @Result
END
ELSE
BEGIN
INSERT INTO @Table(Code,Descri)
VALUES (@Cod,@Descr)
SET @ErrorCode = @@ERROR
IF (@ErrorCode <> 0 )
BEGIN
set @Result = ( -@ErrorCode )
ROLLBACK TRANSACTION
-- In case of ERROR RollBack and return the Error
Return @Result
END
set @Result=2
COMMIT TRANSACTION
RETURN @Result
END
GO
thanks for your help and please correct my sp if i'm thinking in a bad way
i'm trying to do a sp that does the same thing in 4 diferent tables...
the best was to create a sp, where the table name is a parameter...
what is the syntax to do that
i've done something like this
create sp_insertX (@table, @Cod, @descr)
AS
DECLARE @ErrorCode int
DECLARE @Result int
BEGIN TRANSACTION
IF @Cod IN (SELECT Code FROM @Table)
BEGIN
UPDATE @Table SET Descri = @Descr.
SET @ErrorCode = @@ERROR
IF (@ErrorCode <> 0 )
BEGIN
set @Result = ( -@ErrorCode )
ROLLBACK TRANSACTION
-- In case of ERROR RollBack and return the Error
Return @Result
END
set @Result=1
COMMIT TRANSACTION
RETURN @Result
END
ELSE
BEGIN
INSERT INTO @Table(Code,Descri)
VALUES (@Cod,@Descr)
SET @ErrorCode = @@ERROR
IF (@ErrorCode <> 0 )
BEGIN
set @Result = ( -@ErrorCode )
ROLLBACK TRANSACTION
-- In case of ERROR RollBack and return the Error
Return @Result
END
set @Result=2
COMMIT TRANSACTION
RETURN @Result
END
GO
thanks for your help and please correct my sp if i'm thinking in a bad way