怎样能筛选出数据库中值为负的字段呢?

bringyourheart8 2008-02-26 03:20:08
公司的数据库出了问题。现在确认是数据库中有些字段的值为负的最大值。需要把这些值改了。但问题是怎么能一次把所有值为负的字段选出来呢?谢谢大家了。
...全文
563 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
JiangHongTao 2008-02-26
  • 打赏
  • 举报
回复
如果你的表的记录不是很多的话可以试试下面的语句。
下面的语句只给出了表,至于字段应该很容易了。
特别提醒,当你存在某个表的记录过百万且存在数值字段并且没有小于0的记录,那你就等着吧。
如果你实现知道哪个表的记录数很大的话可以用@whereand 过滤这些表。
sp_MSforeachtable 
@command1='declare @s varchar(8000)
select @s = isnull(@s+'' or '','''')+a.name +''<0'' from dbo.syscolumns a,sysobjects b where ''[dbo].[''+b.name+'']'' = ''?'' and a.id = b.id and a.xtype in(127,56,52,48,104,108,60,122,62,59)
if @s is not null
begin
select @s = ''if exists(select 1 from ? where ''+@s+'') insert ## select ''''?'''' ''
exec(@s)
end',
@precommand='create table ##(tbn varchar(200))',
@postcommand ='select * from ## drop table ##'
/*
tbn
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[dbo].[os]
[dbo].[Table1]
[dbo].[Table2]
[dbo].[Table3]
[dbo].[A]
[dbo].[B]
[dbo].[T_Line]
[dbo].[RdRecord2]
[dbo].[tt]
[dbo].[thw]
[dbo].[y]
[dbo].[sort2]
*/
bringyourheart8 2008-02-26
  • 打赏
  • 举报
回复
库里有一千过张表,出问题的就是其中的某些字段。但字段太多。需要有个脚本能遍历整个数据库,找出其中有负值的字段来。
michaelgy 2008-02-26
  • 打赏
  • 举报
回复
估计是字段很多 不知道哪个可能出现负数的意思。

那也可以罗列所有的字段把。
liangCK 2008-02-26
  • 打赏
  • 举报
回复
不知道列的情况下..使用动态构造列.

得到表中除Col1、Col2的所有列

例如:userno_fm、userno_to
create table test(
num int identity(1,1),
userno_fm varchar(10),
userno_to varchar(10),
username varchar(10))
select * from test

declare @sql varchar(8000)
select @sql=''
select @sql=@sql+','+[name] from
(select [name] from syscolumns where object_id(N'[test]')=[id] and [name] not in ('userno_fm','userno_to')) A

set @sql='select '+stuff(@sql,1,1,'')+' from [test]'
--print @sql
exec (@sql)

drop table test
csshan 2008-02-26
  • 打赏
  • 举报
回复
select * from table where 列1<0 or 列2<0 or 列3<0 ....or 列N<0

如果列多很慢,也很麻烦,慢慢写吧。

liangCK 2008-02-26
  • 打赏
  • 举报
回复
select *
from sysobject
where xtype='U'

就可以知道表名.
areswang 2008-02-26
  • 打赏
  • 举报
回复
强,顶!
liangCK 2008-02-26
  • 打赏
  • 举报
回复
有数据库还不知道表名字段名?
bringyourheart8 2008-02-26
  • 打赏
  • 举报
回复
先谢谢楼上了。
刚十分抱歉,没说清楚。现在是表名和字段名都不知道。就有一个数据库放在那里,这样能选出吗?
liangCK 2008-02-26
  • 打赏
  • 举报
回复
select * from tb
where col1 <0 or col2<0 or col3<0

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧