X
تبلیغات
رایتل

  جستجوی یک فیلد خاص درون جداول و یا View های موجود در DataBase


  شنبه 30 دی‌ماه سال 1391


DECLARE @I INT = 1;

DECLARE @TABLENAME NVARCHAR(MAX);

DECLARE @COUNT INT;

DECLARE @Return Table(TableName nvarchar(MAX));


SELECT ROW_NUMBER() OVER (ORDER BY NAME) AS ID,NAME AS NAME INTO #TABLES FROM SYS.TABLES --Or SYS.VIEWS

SET @COUNT = (SELECT COUNT(*) FROM #TABLES);


WHILE @I <= @COUNT

BEGIN

SELECT @TABLENAME = NAME FROM #TABLES WHERE ID = @I;


IF EXISTS (SELECT * FROM SYS.COLUMNS WHERE (NAME LIKE N'%Field_Name%') AND OBJECT_ID = OBJECT_ID(@TABLENAME))

INSERT INTO @Return(TableName) Values (@TABLENAME)


SET @I = @I + 1;

END


SELECT * FROM @Return;

DROP TABLE #TABLES;