如何查询一条记录中所有为空的字段名:
ID name num test1 test2
1 null 22 null null查询后显示为
name test1 test2
反正要得到所有为空的字段名称
ID name num test1 test2
1 null 22 null null查询后显示为
name test1 test2
反正要得到所有为空的字段名称
解决方案 »
- 【求助】请问怎样将一张表中的一个字段所有行的值给另一张表的一个字段?详细里面说。。。
- 请高手帮忙看看一个存储过程的错误:服务器: 消息 170,级别 15,状态 1,过程 Job_ResumeList,行 23
- 这句sql怎么写?(table (a char(10),id int) 我现在想替换字段a的第二个字符,怎么写?)
- 增加一个int字段竟然让整个SQL语句的执行时间翻了几倍
- 怎么在数据库管理器工具界面中直接输入表中的大量含特殊字符的数据,否则怎么写SOL来insert特殊字符呢?
- 一条SQL帮忙看看
- BULK INSERT 权限问题?
- 小问题~
- ][DBNETLIB]SQL Server 不存在或访问被拒绝 在线等待!!!
- 数据库发生置疑是怎么回事?
- 倒出表????
- 如何修改sql中的每服务器,每客户端
drop table tbTest
GO
----创建测试数据
create table tbTest(ID int,name varchar(10),num int,test1 varchar(10),test2 varchar(10))
insert tbTest
select null,null,null,null,null union all
select 1, null, 22, null, null----创建SQL字符串
declare @str varchar(8000)
set @str = ''
select @str = @str + ' and ' + name + ' is null' from syscolumns where id = object_id('tbTest')
set @str = 'select * from tbTest where ' + + stuff(@str,1,4,'')----查看SQL字符串
print @str----执行SQL语句
EXEC(@str)/*结果
ID name num test1 test2
------------------------------------------
NULL NULL NULL NULL NULL
*/
select * from tb where id is null or name is null or num is null or test1 is null or test2 is null整行记录为空?
select * from tb where id is null and name is null and num is null and test1 is null and test2 is null
我的意思是得到一条记录的,
而且得到的是字段名称,比如返回一个varchar(100) Columes
Columes='name,num ,test1'
最后要看到的效果是 'name,num ,test1'
if object_id('tbTest') is not null
drop table tbTest
GO
----创建测试数据
create table tbTest(ID int,name varchar(10),num int,test1 varchar(10),test2 varchar(10))
insert tbTest
select 1, null, 22, null, null----创建SQL字符串
declare @str varchar(8000)
set @str = ''
select @str = @str + ' union all select ''' + name + ''' from tbTest where ' + name + ' is null'
from syscolumns where id = object_id('tbTest')
set @str = stuff(@str,1,10,'')----查看SQL字符串
print @str----执行SQL语句
EXEC(@str)/*结果
name
test1
test2
*/
非常感谢
如果换成其他表有条件的怎么换?
比如where id=1
'where ' + 列名 + 'is null AND 其它条件'.例如:
if object_id('tbTest') is not null
drop table tbTest
GO
----创建测试数据
create table tbTest(ID int,name varchar(10),num int,test1 varchar(10),test2 varchar(10))
insert tbTest
select 1, null, 22, null, null----创建SQL字符串
declare @str varchar(8000)
set @str = ''
select @str = @str + ' union all select ''' + name + ''' from tbTest where ' + name + ' is null and id = 1' /*在此添加其它条件*/
from syscolumns where id = object_id('tbTest')
set @str = stuff(@str,1,10,'')----查看SQL字符串
print @str----执行SQL语句
EXEC(@str)/*结果
name
test1
test2
*/
那么应该怎么改呢?
特别感谢hellowork(一两清风)