How to pass variable to LIKE wildcard
I'm trying to delete temp table names from the APPDB by getting the list
from the DOMDB. I can't find or figure out how the syntax in SQL should
be.
This is some sort of a combination of two codes that works separately but
I thought of combining the two to iterate on the list.
USE DOMDB
Declare @OPR_CODE char( 11 )
SET rowcount 0
SELECT OPR_CODE into #TEMPOPR from DOMN_OPR
SET rowcount 1
SELECT @OPR_CODE = OPR_CODE from #TEMPOPR
USE APPDB
DECLARE @OPRCODE NVARCHAR(MAX) = N'';
SELECT @OPRCODE += '
DROP TABLE '
+ QUOTENAME(OBJECT_SCHEMA_NAME([object_id]))
+ '.' + QUOTENAME(name) + ';'
FROM sys.tables
WHERE name LIKE '% +&@OPR_CODE&+001%';
--something like if OPR_CODE='USER' it will be (WHERE name LIKE '%USER001%';
EXEC sp_executesql @OPRCODE;
while @@rowcount <> 0
begin
set rowcount 0
select * from #TEMPOPR where OPR_CODE = @OPR_CODE
delete #TEMPOPR where OPR_CODE = @OPR_CODE
set rowcount 1
select @OPR_CODE = OPR_CODE from #TEMPOPR
end
set rowcount 0
DROP table #TEMPOPR
No comments:
Post a Comment