I had a requirement where I needed to find all the tables / columns which contain an email address value.
I assumed the field which stores the email address to have length of atleast 30 chars.
This is a stored procedure to achieve that.
--DROP PROCEDURE dbo.SS_SearchInAllPSTables
CREATE PROCEDURE dbo.SS_SearchInAllPSTables (@in_param1 as varchar(50))
AS
DECLARE @a varchar(50);
DECLARE @c varchar(50);
DECLARE @MyCursor CURSOR ;
declare @SqlTabName Nvarchar(500)
declare @ParamDefinition Nvarchar(500)
declare @CntrValue as int
declare @in_param2 as varchar(50)
SET @MyCursor = CURSOR
FOR
SELECT CASE WHEN SUBSTRING (A.RECNAME,1,2)<>'PS' THEN 'PS_'+A.RECNAME ELSE A.RECNAME END,B.FIELDNAME
FROM PSRECDEFN A
, PSRECFIELDDB B
, PSDBFIELD C
WHERE A.RECNAME=B.RECNAME
AND A.RECTYPE=0
AND B.FIELDNAME=C.FIELDNAME
AND ((C.LENGTH>30
AND C.FIELDTYPE=0)
OR (C.FIELDTYPE=1))
AND A.RECNAME NOT LIKE '%AET'
AND A.RECNAME NOT LIKE '%TMP'
AND EXISTS(
SELECT '1'
FROM SYSOBJECTS
WHERE NAME=CASE WHEN SUBSTRING (A.RECNAME,1,2)<>'PS' THEN 'PS_'+A.RECNAME ELSE A.RECNAME END);
SET @ParamDefinition = N'@b varchar(10) output'
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @a ,@c
WHILE @@FETCH_STATUS = 0
BEGIN
select @SqlTabName = 'SELECT @b=COUNT(1) FROM ' + @a + ' where ' + @c +' like '+ '''%' + @in_param1 + '%''';
exec sp_executesql @statement = @SqlTabName, @parameters = @ParamDefinition ,@b = @CntrValue OUTPUT
IF @CntrValue>0
BEGIN
INSERT INTO PS_TEST_SEARCH_REC VALUES('SELECT * FROM '+@a +' where ' + @c +' like '+ '''%' + @in_param1 + '%'''+'-- COUNT ='+ cast(@CntrValue AS char(5)) );
END;
FETCH NEXT FROM @MyCursor
INTO @a ,@c
END
CLOSE @MyCursor
DEALLOCATE @MyCursor
No comments:
Post a Comment