以下是用于搜索列的查询结果
background-color:red;
这非常适合查找/搜索,所使用的查询位于
之下DECLARE @Temp TABLE (
[CoreTable] VARCHAR(250),
[CoreTableDecription] VARCHAR(250),
[FieldName] VARCHAR(250),
[cnt] VARCHAR(250)
)
declare @Keyword1 VARCHAR(100) = '%Prob%'
declare @Keyword2 VARCHAR(100) = '%Prob%'
--------------------------------------------------------------
INSERT INTO @Temp (CoreTable, CoreTableDecription, FieldName, cnt)
VALUES
('PRO','PROTOS_PersonalInformation','AnyCommunicationProblem','1'),
('PRO','PROTOS_PersonalInformation','SightProblems','1'),
('PRO','PROTOS_PersonalInformation','SpeechProblems','1'),
('PRO','PROTOSMODEL_Antenatal_Status','Other_Antenatal_Problems','1'),
('PRO','PROTOSMODEL_Antenatal_Status','Other_Antenatal_Problems_Text','1'),
('PRO','PROTOSMODEL_Baby_Details','Neonatal_medical_problems','1'),
('PRO','PROTOSMODEL_Baby_Details','Neonatal_medical_problems_Text','1'),
('PRO','PROTOSMODEL_Baby_Postnatal','Any_skin_problems_detected','1'),
('PRO','PROTOSMODEL_Baby_Postnatal','Any_skin_problems_detected_Bruising','1'),
('PRO','PROTOSMODEL_Baby_Postnatal','Any_skin_problems_detected_Naevus','1'),
('PRO','PROTOSMODEL_Baby_Postnatal','Any_skin_problems_detected_Rash','1'),
('PRO','PROTOSMODEL_Baby_Postnatal','Any_skin_problems_detected_Text','1')
Select * from @Temp
然而,我需要做的是运行手动查询来确定列/表中的值(见下文):
declare @Keyword1 VARCHAR(100) = '%Prob%'
declare @Keyword2 VARCHAR(100) = '%Prob%'
select
LEFT(o.name,3) CoreTable,
o.name CoreTableDesc,
--o.name AS TableName,
c.name AS FieldName
,COUNT(c.name) cnt
from sys.columns c
inner join sys.objects o on c.object_id=o.object_id
where c.name LIKE @Keyword1
and LEFT(o.name,3) in ('PRO')
and c.name LIKE @Keyword2
GROUP BY c.name, LEFT(o.name,3), o.name
-- order by LEFT(o.name,3), c.name
这只返回行NULL - 2682,No - 87687,Yes - 135478
我想知道是否有办法从显示表名和列名的第一个查询的结果中自动执行此操作
答案 0 :(得分:0)
谢谢以下所有答案,但我不确定如何计算
中的数据项DECLARE
@TABLENAME VARCHAR(255),
@FIELDNAME VARCHAR(255),
@SQL VARCHAR(MAX),
@FieldNamePart1 as varchar(50),
@FieldNamePart2 as varchar(50)
SET @SQL = ''
SET @FieldNamePart1 = 'Type'
SET @FieldNamePart2 = 'Method'
IF @FieldNamepart2 = ''
DECLARE CRS CURSOR FOR
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE CHARINDEX('PROTOSMODEL',TABLE_NAME) <> 0
AND CHARINDEX(@FieldNamePart1, COLUMN_NAME) <> 0
-- AND CHARINDEX('DATE', COLUMN_NAME) = 0
-- AND CHARINDEX('TIME', COLUMN_NAME) = 0
ELSE
DECLARE CRS CURSOR FOR
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE CHARINDEX('PROTOSMODEL',TABLE_NAME) <> 0
AND CHARINDEX(@FieldNamePart1, COLUMN_NAME) <> 0
AND CHARINDEX(@FieldNamePart2, COLUMN_NAME) <> 0
-- AND CHARINDEX('DATE', COLUMN_NAME) = 0
-- AND CHARINDEX('TIME', COLUMN_NAME) = 0
OPEN CRS
FETCH NEXT FROM CRS INTO @TABLENAME, @FIELDNAME
WHILE @@FETCH_STATUS = 0
BEGIN
-- SET @SQL = @SQL + CAST('SELECT DISTINCT ''' AS VARCHAR(MAX)) + CAST(@TABLENAME AS VARCHAR(MAX)) + CAST(''' AS PROTOSMODEL_TABLE, ''' AS VARCHAR(MAX)) + CAST(@FIELDNAME AS VARCHAR(MAX)) + CAST(''' AS PROTOSMODEL_FIELD, ' AS VARCHAR(MAX)) + CAST(@FIELDNAME AS VARCHAR(MAX)) + CAST(' AS FIELD_VALUE FROM ' AS VARCHAR(MAX)) + CAST(@TABLENAME AS VARCHAR(MAX)) + CAST(' UNION ALL ' AS VARCHAR(MAX))
SET @SQL = @SQL + 'SELECT DISTINCT ''' + @TABLENAME + ''' AS PROTOSMODEL_TABLE, ''' + @FIELDNAME + ''' AS PROTOSMODEL_FIELD, [' + @FIELDNAME + '] AS FIELD_VALUE FROM ' + @TABLENAME + ' UNION ALL '
FETCH NEXT FROM CRS INTO @TABLENAME, @FIELDNAME
END
CLOSE CRS
DEALLOCATE CRS
SET @SQL = SUBSTRING(@SQL, 1, LEN(@SQL) - 10)
EXEC (@SQL)
GO