如指定PostID为4的值查询后面的三个字段值为1的字段,后面三个字段不指定.可能以后不至这三个字段.
AwardsID PostID 质量奖 安全奖 考勤奖
----------- ----------- ----- ----- -----
1 4 NULL 1 NULL
2 2 NULL 1 NULL
3 2 NULL NULL 1
4 3 NULL NULL 1
AwardsID PostID 质量奖 安全奖 考勤奖
----------- ----------- ----- ----- -----
1 4 NULL 1 NULL
2 2 NULL 1 NULL
3 2 NULL NULL 1
4 3 NULL NULL 1
PostID 安全奖
------ -----
4 1其它的没有就不查询出来
declare @PostID int
set @PostID=4
select * from table1 where PostID=@PostID and (质量奖+安全奖+考勤奖)=3--其它條件為固定條件就行了,或用動態判斷列名
操作数数据类型 bit 对于 add 运算符无效。报错,我不明白为什么这样.但感觉不是我要的结果
操作数数据类型 bit 对于 add 运算符无效。
----------------
把轉換一下declare @PostID int
set @PostID=4
select * from table1 where PostID=@PostID and (cast(质量奖 as smallint)+安全奖+考勤奖)=3
不是我要的结果
如查询了PostID=4 结果要是这样
PostID 安全奖
------ -----
4 1
create table tb(AwardsID int,PostID int,质量奖 int,安全奖 int,考勤奖 int)
insert into tb select 1,4,NULL,1,NULL
insert into tb select 2,2,NULL,1,NULL
insert into tb select 3,2,NULL,NULL,1
insert into tb select 4,3,NULL,NULL,1
go
declare @id int,@s as nvarchar(30)
set @id=3 --改变这个可以改变所引用的列,但众多列中只选第一个不为NULL的.
declare @str nvarchar(4000)
select @str=isnull(@str+'+','')+'ltrim(isnull('+a.name+',0))' from syscolumns a inner join sysobjects b on a.id=b.id where b.type='U' and b.name='tb' and a.colid>2 order by a.colid
set @str='select @s='+@str+' from tb where AwardsID='+ltrim(@id)
exec sp_executesql @str,N'@s nvarchar(30) output ',@s output
select @str='select AwardsID,PostID,'+a.name+' from tb where AwardsID='+ltrim(@id) from syscolumns a inner join sysobjects b on a.id=b.id where b.type='U' and b.name='tb' and a.colid=2+charindex('1',@s)
exec (@str)
/*
AwardsID PostID 考勤奖
----------- ----------- -----------
3 2 1(1 行受影响)*/
go
drop table tb
from syscolumns a inner join sysobjects b on a.id=b.id where b.type='U' and b.name='tb' and a.colid=2+charindex('1',@s)
有点不是很明白这个意思系统列表系统对象表还有type=U这是什么意思还得麻烦你告诉我一下你的思路.你说的是我要的结果.但没看明白你的思路
我觉得这么搞没什么意思,毕竟这样的限制对于现有系统来讲只能是拼人品。
可通过xml配置或数据库表配置,将每个表需要统计的字段写到xml或配置表中,查询的时候再根据表名来取出这些字段然后拼成SQL用EXEC来执行
AwardsID PostID.后面的N个字段由用户自已添加.
create table tb(AwardsID int,PostID int,质量奖 int,安全奖 int,考勤奖 int)
insert into tb select 1,4,1,1,NULL
insert into tb select 2,2,NULL,1,NULL
insert into tb select 3,2,1,NULL,1
insert into tb select 4,3,NULL,1,1这样的话,只显示第一列质量.后面任意列不会.只会检查第一个列是否为null了.有没有办法任意呢?
create table tb(AwardsID int,PostID int,质量奖 int,安全奖 int,考勤奖 int)
insert into tb select 1,4,1,1,NULL
insert into tb select 2,2,NULL,1,NULL
insert into tb select 3,2,1,NULL,1
insert into tb select 4,3,NULL,1,1
declare @sql nvarchar(4000)
declare @PostID int
set @PostID=2
declare @var nvarchar(4000)
select @sql=isnull(@sql+'+','')+'case when sum('+name+')>=0 then '+quotename(','+name,'''')+' else '''' end ' from syscolumns where id=object_id('tb') and name not in ('AwardsID','PostID')
set @sql='select @a='+@sql+' from tb where PostID='+ltrim(@PostID)
print (@sql)
exec sp_executesql @sql,N'@a varchar(4000) output',@var outputset @sql='select AwardsID,PostID'+@var+' from tb where PostID='+ltrim(@PostID)
exec(@sql)
谢谢
那我把任意列的值改成这样该怎么改.
select case (select a.name from tb where PostID = @id) when 1 then (select Amount from Awards where AwardsName=a.name) end也就是值为1的时候查询AwardsName表的Amount值
select case (select a.name from tb where PostID = @id) when 1 then (select Amount from Awards where AwardsName=a.name) end也就是值为1的时候查询AwardsName表的Amount值
你上面是select a.name from tb where PostID = @id when 1
就是name=1吗?没看懂得你语句的意思,
另外这句话“也就是值为1的时候查询AwardsName表的Amount值”
是指什么值为1的时候候查询AwardsName表的Amount值?
蓝色部分的a.name是什么意思?
注意:红色部分的a.name查询的值应该是一个值,不能是多个
同样:select Amount from Awards where AwardsName=a.name,
查询后的Amount也应该是一个值,不能是多个值,
否则都会报错
也就是@var值为1时查询AwardsName表相应的Amount值如@var值为质量奖
就查询Awards表里面AwardsName为质量奖的Amount值