首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

数据库内容修改!解决办法

2012-02-28 
数据库内容修改!条件:现有数据库:A以及知道数据库中存在一条信息记录:123456。问题:但不知道存放在数据库:A

数据库内容修改!
条件:
        现有数据库:A   以及知道数据库中存在一条信息记录:123456     。
问题:
          但不知道存放在数据库:A   中的哪些表?以及表中的哪个字段?

想得到的结果:

    1:如何才能知道记录:123456,存在数据库哪些表中以及存在表中哪个字段?

    2:如何对存在记录:123456中的所有信息进行修改。修改后的信息变为:654321   ?

[解决办法]
create table test(id int,code varchar(10))
insert into test select 1, '123456 '
go

select * from test
go

declare @tname varchar(40),@cname varchar(40)
declare @sql varchar(8000)

create table #t(tname varchar(40),cname varchar(40))

declare t_cursor cursor for
select a.name as tname,b.name as cname
from sysobjects a,syscolumns b
where a.id=b.id and a.type= 'U ' and a.name not like '#% '

open t_cursor

fetch next from t_cursor into @tname,@cname

while @@fetch_status=0
begin
set @sql= 'if exists(select 1 from '+@tname+ ' where '+@cname+ '=123456) '+char(13)
+ ' insert into #t values( ' ' '+@tname+ ' ' ', ' ' '+@cname+ ' ' ') '
print @sql
exec(@sql)

fetch next from t_cursor into @tname,@cname
end
close t_cursor
deallocate t_cursor

select * from #t

select @sql= 'update '+tname+ ' set '+cname+ '=654321 where '+cname+ '=123456 ' from #t
exec(@sql)
go

select * from test

drop table test,#t
go
[解决办法]
试验用表
create table temp
(A varchar(50),
B varchar(50),
C varchar(50),
D varchar(50),
E varchar(50),
F varchar(50),
G varchar(50)
)
insert into temp
select 'ab ', 'abc ', 'abc ', 'abcd ', '1abcb ', 'ls ', 'dh '
select * from temp

存储过程:根据表名和字符名查出相应字段
create procedure pro_col (@tbl_name varchar(30),@str varchar(10))as
declare @cur_col varchar(30)

declare @sql varchar(1000)
set @sql= ' '
declare cur_col cursor for

select t2.name from sysobjects t1,syscolumns t2 where t1.id=t2.id and t1.xtype= 'u ' and t1.name= ' '+@tbl_name+ ' ' order by t2.name

open cur_col

fetch next from cur_col into @cur_col
while @@fetch_status = 0

begin

declare @flag varchar(50)
select @flag= ' '+@cur_col+ ' '
set @sql=@sql+ ' select ' ' '+@flag+ ' ' ' 列, '+@flag+ ' 值 from '+@tbl_name+ ' where '+@flag+ ' like ' '% '+ ' '+@str+ ' '+ '% ' ' '+ ' union all '--这里用的是like也可以改成=号

fetch next from cur_col into @cur_col

end
set @sql=substring(@sql,1,len(@sql)-10)
print @sql
exec (@sql)

close cur_col
deallocate cur_col

go

查询表中包含 'abc '字符的列名
exec pro_col temp,abc

------------
列 值
B abc
C abc

[解决办法]

declare @tbname nvarchar(200),@colname nvarchar(200),@sql nvarchar(2000)
DECLARE temp_cursor CURSOR
FOR select so.name as tbname,sc.name colname from sysobjects so inner join syscolumns sc on so.id=sc.id where so.xtype= 'u ' and sc.xtype=231
OPEN temp_cursor
FETCH NEXT FROM temp_cursor


into @tbname,@colname

WHILE @@FETCH_STATUS = 0
BEGIN
set @sql= 'select ' ' '+@tbname+ ' ' ', ' ' '+@colname+ ' ' 'from '+@tbname+ ' where '+@colname+ '= ' '8888 ' ' '
execute sp_executesql @sql
set @sql= 'update '+@tbname+ ' set '+@colname+ '=8888 where '+@colname+ '= ' '8888 ' ' '
execute sp_executesql @sql
FETCH NEXT FROM temp_cursor
into @tbname,@colname
END

CLOSE temp_cursor
DEALLOCATE temp_cursor


bigint
127binary
173bit
104char
175datetime
61decimal
106float
62image
34int
56money
60nchar
239ntext
99numeric
108nvarchar
231real
59smalldatetime
58smallint
52smallmoney
122sql_variant
98sysname
231text
35timestamp
189tinyint
48uniqueidentifier
36varbinary
165varchar
167
[解决办法]
--建立测试数据表,并insert一条测试数据
create table test(id int,code varchar(10))
insert into test select 1, '123456 '
go

--检查测试数据
select * from test
go

--定义用于存放表名及列名的变量
declare @tname varchar(40),@cname varchar(40)

--定义用于存放动态SQL语句的字符串变量
declare @sql varchar(8000)

--建立用于保存动态SQL语句执行结果的临时表
create table #t(tname varchar(40),cname varchar(40))

--定义游标,用于获得当前数据库所有的用户表及所有用户表字段的集合
declare t_cursor cursor for
select a.name as tname,b.name as cname
from sysobjects a,syscolumns b
where a.id=b.id and a.type= 'U ' and a.name not like '#% '

--打开游标
open t_cursor

--取第一条记录的数据
fetch next from t_cursor into @tname,@cname

--游标遍历处理
while @@fetch_status=0
begin
--组织动态SQL语句,目的:当前表的当前字段如果满足查询条件——值为123456,
-- 则将该表表名及当前字段名insert到临时表
set @sql= 'if exists(select 1 from '+@tname+ ' where '+@cname+ '=123456) '+char(13)
+ ' insert into #t values( ' ' '+@tname+ ' ' ', ' ' '+@cname+ ' ' ') '

--执行动态SQL
exec(@sql)

--取下一条记录
fetch next from t_cursor into @tname,@cname
end

--关闭并释放游标
close t_cursor
deallocate t_cursor

--查看满足条件的数据记录
select * from #t

--假定只有一条满足条件的记录,则借助动态SQL更新数据
select @sql= 'update '+tname+ ' set '+cname+ '=654321 where '+cname+ '=123456 ' from #t

--执行更新操作的动态SQL
exec(@sql)
go

--查看更新操作的执行结果
select * from test
go

--删除测试数据及临时表
drop table test,#t
go
[解决办法]
從系統表里面取字段哈.
Select @iObjectID=id from sysobjects where [name]=[Table A]
Select name from syscolumns where id=@iObjectID order by colorder
下面的簡單羅,不說了....

热点排行