sql 如何查询包含某一字段的值
答案:6 悬赏:80 手机版
解决时间 2021-03-30 15:34
- 提问者网友:世勋超人
- 2021-03-30 02:46
sql 如何查询包含某一字段的值
最佳答案
- 五星知识达人网友:逐風
- 2021-03-30 03:59
这种情况需要写存储过程,进行全库搜索。代码如下:
declare @cloumns varchar(40)
declare @tablename varchar(40)
declare @str varchar(40)
declare @counts int
declare @sql nvarchar(2000)
declare MyCursor Cursor For
Select a.name as Columns, b.name as TableName from syscolumns a,sysobjects b,systypes c
where a.id = b.id
and b.type = 'U'
and a.xtype=c.xtype
and c.name like '%varchar%'
set @str='张三'
Open MyCursor
Fetch next From MyCursor Into @cloumns,@tablename
While(@@Fetch_Status = 0)
Begin
set @sql='select @tmp_counts=count(*) from ' +@tablename+ ' where ' +@cloumns+' = ''' +@str+ ''''
execute sp_executesql @sql,N'@tmp_counts int out',@counts out
if @counts>0
begin
print '表名为:'+@tablename+',字段名为'+@cloumns
end
Fetch next From MyCursor Into @cloumns,@tablename
End
Close MyCursor
Deallocate MyCursor注意:其中“张三”为要查找的字符串,可以替换成其他的,如果查询的字符串超长,需要在定义变量时适当扩大长度。
declare @cloumns varchar(40)
declare @tablename varchar(40)
declare @str varchar(40)
declare @counts int
declare @sql nvarchar(2000)
declare MyCursor Cursor For
Select a.name as Columns, b.name as TableName from syscolumns a,sysobjects b,systypes c
where a.id = b.id
and b.type = 'U'
and a.xtype=c.xtype
and c.name like '%varchar%'
set @str='张三'
Open MyCursor
Fetch next From MyCursor Into @cloumns,@tablename
While(@@Fetch_Status = 0)
Begin
set @sql='select @tmp_counts=count(*) from ' +@tablename+ ' where ' +@cloumns+' = ''' +@str+ ''''
execute sp_executesql @sql,N'@tmp_counts int out',@counts out
if @counts>0
begin
print '表名为:'+@tablename+',字段名为'+@cloumns
end
Fetch next From MyCursor Into @cloumns,@tablename
End
Close MyCursor
Deallocate MyCursor注意:其中“张三”为要查找的字符串,可以替换成其他的,如果查询的字符串超长,需要在定义变量时适当扩大长度。
全部回答
- 1楼网友:慢性怪人
- 2021-03-30 07:39
如果查询的是2011开始的就是:
select * from tableName where a like '2011%'
如果在中间的就是
select * from tableName where a like '%2011%'
不包含就是 not like
select * from tableName where a like '2011%'
如果在中间的就是
select * from tableName where a like '%2011%'
不包含就是 not like
- 2楼网友:北城痞子
- 2021-03-30 06:35
select * from tableName where a like '2011%'
select * from tableName where a not like '2011%'
select * from tableName where a not like '2011%'
- 3楼网友:话散在刀尖上
- 2021-03-30 06:29
-- 语句表示 内容字段里面包含了葡萄酒
select 内容 from 表名 where 内容 like'%葡萄酒%'
select 内容 from 表名 where 内容 like'%葡萄酒%'
- 4楼网友:何以畏孤独
- 2021-03-30 05:02
不同的数据库,SQL语句略有区别:
1.如果是Access数据库,SQL语句应为:
SELECt * FROM TableName WHERe A LIKE '*2011*'
2.如果是Oracle数据库,SQL语句应为:
SELECt * FROM TableName WHERe A LIKE '%2011%'
3.如果是SQL SERVER数据库,SQL语句应为:
SELECt * FROM TableName WHERe A LIKE '%2011%'
1.如果是Access数据库,SQL语句应为:
SELECt * FROM TableName WHERe A LIKE '*2011*'
2.如果是Oracle数据库,SQL语句应为:
SELECt * FROM TableName WHERe A LIKE '%2011%'
3.如果是SQL SERVER数据库,SQL语句应为:
SELECt * FROM TableName WHERe A LIKE '%2011%'
- 5楼网友:人间朝暮
- 2021-03-30 04:32
select * from tableName where a like '2011%'
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯