检查行集合是否属于SQL中的特定值?
我想检查给定的表是否有特定的列名,如果它有所有列名,我想让它返回true,如果一个列名不存在,我想让它返回false,这是我的查询:检查行集合是否属于SQL中的特定值?,sql,sql-server,Sql,Sql Server,我想检查给定的表是否有特定的列名,如果它有所有列名,我想让它返回true,如果一个列名不存在,我想让它返回false,这是我的查询: SELECT COLUMN_NAME FROM db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTable' 我想检查查询结果中是否有这些名称: ('UpdateDate','CreatedDate','UpdatedBy')如果您知道此查询的列表计数,则可以帮助您 SELECT COUNT(COLUMN_
SELECT COLUMN_NAME
FROM db.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTable'
我想检查查询结果中是否有这些名称:
('UpdateDate','CreatedDate','UpdatedBy')如果您知道此查询的列表计数,则可以帮助您
SELECT COUNT(COLUMN_NAME)
FROM db.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTable' AND COLUMN_NAME IN ('UpdatedDate', 'CreatedDate', 'UpdatedBy')
GROUP BY COLUMN_NAME HAVING COUNT(COLUMN_NAME) = 3;
如果您知道列表的计数,此查询将帮助您
SELECT COUNT(COLUMN_NAME)
FROM db.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTable' AND COLUMN_NAME IN ('UpdatedDate', 'CreatedDate', 'UpdatedBy')
GROUP BY COLUMN_NAME HAVING COUNT(COLUMN_NAME) = 3;
创建一个带有列名的表变量,我们需要使用
信息\u schema.columns
检查这些列名。然后在表变量和信息\u schema.columns
之间进行检查。不知道这有多高效
查询
declare @cols as varchar(max) = 'UpdatedDate,CreatedDate,UpdatedBy';
declare @cols2 as varchar(max) = '(' + char(39);
set @cols2 += replace(@cols, ',', '''),(''') + ''');';
declare @sql as varchar(max) = 'declare @tbl as table([col] varchar(1000));';
set @sql += 'insert into @tbl values' + @cols2;
set @sql += 'declare @tot as int;set @tot = (select count(*) from @tbl);';
set @sql += 'select case when count(*) = @tot
then ''true'' else ''false'' end as [status]
from @tbl t1 where exists(
select 1 from information_schema.columns t2
where t1.[col] = t2.[column_name]
and t2.[table_name] = ''MyTable'');';
exec(@sql);
创建一个带有列名的表变量,我们需要使用
信息\u schema.columns
检查这些列名。然后在表变量和信息\u schema.columns
之间进行检查。不知道这有多高效
查询
declare @cols as varchar(max) = 'UpdatedDate,CreatedDate,UpdatedBy';
declare @cols2 as varchar(max) = '(' + char(39);
set @cols2 += replace(@cols, ',', '''),(''') + ''');';
declare @sql as varchar(max) = 'declare @tbl as table([col] varchar(1000));';
set @sql += 'insert into @tbl values' + @cols2;
set @sql += 'declare @tot as int;set @tot = (select count(*) from @tbl);';
set @sql += 'select case when count(*) = @tot
then ''true'' else ''false'' end as [status]
from @tbl t1 where exists(
select 1 from information_schema.columns t2
where t1.[col] = t2.[column_name]
and t2.[table_name] = ''MyTable'');';
exec(@sql);