I want to verify a row is deleted. I am passing both the table name and the value in question to my stored procedure. It deletes said items and I want something returned back. I created @ItemFound and want to let it equal to the count. But it is not getting set since its inside of the SQL string I'm building. How do I create a SQL string, Execute it, set that result to a variable, then check the variable later?
TIA
DougP
Code:
Create Procedure MyProcedure
(@UPCode varchar(20),
@Tablename varchar(200))
AS
Declare @SQLString varchar(1000)
Select @SQLString = ('Delete from ' + @Tablename + ' Where [UPCode] = ''' + @UPCode + '''')
print @SQLString
--[My Table name]
EXEC(@SQLString)
-- area of issue where
Declare @ItemFound as int
Declare @SQLString2 varchar(500)
-- this gave error @ItemFound not declared so I added it to the string I'm building. but its not available outside.
Select @SQLString2 = ('Declare @ItemFound as int; set @ItemFound = (Select count(*) from ' + @Tablename + ' Where UPCode = ''' + @UPCode + ''')')
print @SQLString2
EXEC(@SQLString2)
-- @ItemFound is not equal to zero so it returns 'Item NOT Deleted'
If @ItemFound = 0
Begin
Select 'Item Deleted'
End
else
Begin
Select 'Item NOT Deleted'
End
TIA
DougP