10 个解决方案
#1
SELECT *
FROM information_schema.columns
WHERE TABLE_CATALOG='数据库名'
AND TABLE_NAME like 'tab%'
AND COLUMN_NAME='列名'
#2
没办法,一个一个去写吧。
或者用动态语句去写,如果你那些字段名有规律的话
或者用动态语句去写,如果你那些字段名有规律的话
#3
所有的varchar型字段和nvarchar型字段全部清空
这个不会
1楼的是查询出那些表
这个不会
1楼的是查询出那些表
#4
第一步找出以tab开头的表
select *
from sysobjects
where xtype='u'and name like 'tab%'
第二步用循环写语句
while @i<xxx
begin
declare @s varchar(8000)
set @s=''
select @s=@s+name+',' from syscolumns
where id=object_id('dept') and name<>'id'
order by colid
end
循环里面的代码你自己加工,加以判断为varchar,nvarchar
select *
from sysobjects
where xtype='u'and name like 'tab%'
第二步用循环写语句
while @i<xxx
begin
declare @s varchar(8000)
set @s=''
select @s=@s+name+',' from syscolumns
where id=object_id('dept') and name<>'id'
order by colid
end
循环里面的代码你自己加工,加以判断为varchar,nvarchar
#5
--先备份一下数据库。
--用游标
declare @str varchar(100) ,@tb varchar(100),@col varchar(20)
--定义游标
declare DZCursor CURSOR for
SELECT b.name,a.name FROM syscolumns a,sysobjects b where a.id=b.id and b.xtype='u' and b.name like 'tab%' and a.xtype in (167,231)
--打开游标
open DZCursor
--从游标取记录
fetch next from DZCursor into @tb, @col
--当有记录
while @@fetch_status=0
begin
exec ('update '+@tb+' set '+@col+'=null')
--取下一条记录
fetch next from DZCursor into @tb, @col
end
--关闭游标
close DZCursor
--删除游标引用
deallocate DZCursor
#6
--先备份一下数据库。
--用游标
declare @str varchar(100) ,@tb varchar(100),@col varchar(20)
select @tb='',@col=''
--定义游标
declare DZCursor CURSOR for
SELECT b.name,a.name FROM syscolumns a,sysobjects b where a.id=b.id and b.xtype='u' and b.name like 'tab%' and a.xtype in (167,231)
--打开游标
open DZCursor
--从游标取记录
fetch next from DZCursor into @tb, @col
--当有记录
while @@fetch_status=0
begin
exec ('update '+@tb+' set '+@col+'=null')
--取下一条记录
fetch next from DZCursor into @tb, @col
end
--关闭游标
close DZCursor
--删除游标引用
deallocate DZCursor
#7
CREATE TABLE tab_1(
col1 varchar(100),
col2 nvarchar(100)
)
insert into tab_1
select 'a','b'
CREATE TABLE tab_2(
col3 varchar(100),
col4 nvarchar(100)
)
insert into tab_2
select 'c','d'
select * from tab_1 union all
select * from tab_2
select objs.name 'tabname',cols.name 'colname' into #cols from syscolumns cols
inner join sysobjects objs on cols.id=objs.id and objs.type='U' and patindex('tab%',objs.name)>0
WHILE (SELECT COUNT(DISTINCT TABNAME) FROM #COLS)>0
BEGIN
DECLARE @STR VARCHAR(8000)
DECLARE @TABNAME VARCHAR(100)
DECLARE @COLNAME VARCHAR(100)
SELECT TOP 1 @TABNAME=TABNAME FROM #COLS
SELECT @STR='UPDATE '+@TABNAME+' SET'
SELECT @STR=@STR+' '+COLNAME+'=NULL,' FROM #COLS WHERE TABNAME=@TABNAME
SELECT @STR=LEFT(@STR,LEN(@STR)-1)
EXEC (@STR)
DELETE FROM #COLS WHERE TABNAME=@TABNAME
END
select * from tab_1 union all
select * from tab_2
drop table tab_1
drop table tab_2
DROP TABLE #COLS
/*
a b
c d
NULL NULL
NULL NULL
写的太慢了。。。
*/
#8
--使用游标
DECLARE @COL VARCHAR(100),@TN VARCHAR(100)
--定义游标
DECLARE UP_Col_Cur CURSOR FOR
SELECT a.name,b.name
FROM SYSCOLUMNS a inner join SYSOBJECTS b
on b.XTYPE='u' and CHARINDEX('t',b.name)=1
and a.XTYPE in(231,167)
--打开游标
OPEN UP_Col_Cur
--循环游标
fetch next from UP_Col_Cur into @COL,@TN
--如果没到结尾
while @@fetch_status=0
BEGIN
EXEC('UPDATE '+@TN+' SET '+@COL+'=''''')
FETCH NEXT FROM UP_Col_Cur INTO @COL, @TN
END
--关闭游标
CLOSE UP_Col_Cur
--删除游标引用
DEALLOCATE UP_Col_Cur
#9
妈呀,把我自己的数据库全清空了。。。
#10
强人。。。
#1
SELECT *
FROM information_schema.columns
WHERE TABLE_CATALOG='数据库名'
AND TABLE_NAME like 'tab%'
AND COLUMN_NAME='列名'
#2
没办法,一个一个去写吧。
或者用动态语句去写,如果你那些字段名有规律的话
或者用动态语句去写,如果你那些字段名有规律的话
#3
所有的varchar型字段和nvarchar型字段全部清空
这个不会
1楼的是查询出那些表
这个不会
1楼的是查询出那些表
#4
第一步找出以tab开头的表
select *
from sysobjects
where xtype='u'and name like 'tab%'
第二步用循环写语句
while @i<xxx
begin
declare @s varchar(8000)
set @s=''
select @s=@s+name+',' from syscolumns
where id=object_id('dept') and name<>'id'
order by colid
end
循环里面的代码你自己加工,加以判断为varchar,nvarchar
select *
from sysobjects
where xtype='u'and name like 'tab%'
第二步用循环写语句
while @i<xxx
begin
declare @s varchar(8000)
set @s=''
select @s=@s+name+',' from syscolumns
where id=object_id('dept') and name<>'id'
order by colid
end
循环里面的代码你自己加工,加以判断为varchar,nvarchar
#5
--先备份一下数据库。
--用游标
declare @str varchar(100) ,@tb varchar(100),@col varchar(20)
--定义游标
declare DZCursor CURSOR for
SELECT b.name,a.name FROM syscolumns a,sysobjects b where a.id=b.id and b.xtype='u' and b.name like 'tab%' and a.xtype in (167,231)
--打开游标
open DZCursor
--从游标取记录
fetch next from DZCursor into @tb, @col
--当有记录
while @@fetch_status=0
begin
exec ('update '+@tb+' set '+@col+'=null')
--取下一条记录
fetch next from DZCursor into @tb, @col
end
--关闭游标
close DZCursor
--删除游标引用
deallocate DZCursor
#6
--先备份一下数据库。
--用游标
declare @str varchar(100) ,@tb varchar(100),@col varchar(20)
select @tb='',@col=''
--定义游标
declare DZCursor CURSOR for
SELECT b.name,a.name FROM syscolumns a,sysobjects b where a.id=b.id and b.xtype='u' and b.name like 'tab%' and a.xtype in (167,231)
--打开游标
open DZCursor
--从游标取记录
fetch next from DZCursor into @tb, @col
--当有记录
while @@fetch_status=0
begin
exec ('update '+@tb+' set '+@col+'=null')
--取下一条记录
fetch next from DZCursor into @tb, @col
end
--关闭游标
close DZCursor
--删除游标引用
deallocate DZCursor
#7
CREATE TABLE tab_1(
col1 varchar(100),
col2 nvarchar(100)
)
insert into tab_1
select 'a','b'
CREATE TABLE tab_2(
col3 varchar(100),
col4 nvarchar(100)
)
insert into tab_2
select 'c','d'
select * from tab_1 union all
select * from tab_2
select objs.name 'tabname',cols.name 'colname' into #cols from syscolumns cols
inner join sysobjects objs on cols.id=objs.id and objs.type='U' and patindex('tab%',objs.name)>0
WHILE (SELECT COUNT(DISTINCT TABNAME) FROM #COLS)>0
BEGIN
DECLARE @STR VARCHAR(8000)
DECLARE @TABNAME VARCHAR(100)
DECLARE @COLNAME VARCHAR(100)
SELECT TOP 1 @TABNAME=TABNAME FROM #COLS
SELECT @STR='UPDATE '+@TABNAME+' SET'
SELECT @STR=@STR+' '+COLNAME+'=NULL,' FROM #COLS WHERE TABNAME=@TABNAME
SELECT @STR=LEFT(@STR,LEN(@STR)-1)
EXEC (@STR)
DELETE FROM #COLS WHERE TABNAME=@TABNAME
END
select * from tab_1 union all
select * from tab_2
drop table tab_1
drop table tab_2
DROP TABLE #COLS
/*
a b
c d
NULL NULL
NULL NULL
写的太慢了。。。
*/
#8
--使用游标
DECLARE @COL VARCHAR(100),@TN VARCHAR(100)
--定义游标
DECLARE UP_Col_Cur CURSOR FOR
SELECT a.name,b.name
FROM SYSCOLUMNS a inner join SYSOBJECTS b
on b.XTYPE='u' and CHARINDEX('t',b.name)=1
and a.XTYPE in(231,167)
--打开游标
OPEN UP_Col_Cur
--循环游标
fetch next from UP_Col_Cur into @COL,@TN
--如果没到结尾
while @@fetch_status=0
BEGIN
EXEC('UPDATE '+@TN+' SET '+@COL+'=''''')
FETCH NEXT FROM UP_Col_Cur INTO @COL, @TN
END
--关闭游标
CLOSE UP_Col_Cur
--删除游标引用
DEALLOCATE UP_Col_Cur
#9
妈呀,把我自己的数据库全清空了。。。
#10
强人。。。