数据库中名字
select * from sysobjects where xtype='u'
表中字段
SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='table_name'
更改字段名
EXEC sp_rename '[dbo].[table_name].[old_column_name]','new_column_name'
生成该数据库所有表所有字段空值检查脚本
逻辑:
1.获取该数据所有表对应的所有字段名
2.根据字段生成空值检查脚本(字段名 is null or 字段名 = '')
SELECT TABLE_CATALOG,TABLE_NAME,COLUMN_NAME,DATA_TYPE,
'Select count(*) from ['+TABLE_CATALOG+'].[dbo].['+ TABLE_NAME+'] where ['+COLUMN_NAME+'] is null or ['+COLUMN_NAME+'] ='''' 'as sql
FROM INFORMATION_SCHEMA.columns
-- WHERE TABLE_NAME='table_name'
order by TABLE_CATALOG,TABLE_NAME,COLUMN_NAME
升级:
SELECT TABLE_CATALOG,TABLE_NAME,COLUMN_NAME,DATA_TYPE,
'Select @count=count(*) from ['+TABLE_CATALOG+'].[dbo].['+ TABLE_NAME+'] where ['+COLUMN_NAME+'] is null or ['+COLUMN_NAME+'] ='''' 'as sql,
CAST('' as varchar(50)) as [count]
into #ColumnIsNull
FROM INFORMATION_SCHEMA.columns
order by TABLE_CATALOG,TABLE_NAME,COLUMN_NAME
---------------------------------------------
DECLARE @sql NVARCHAR(4000)
DECLARE @cou NVARCHAR(4000)
DECLARE cur CURSOR STATIC LOCAL FOR --声明游标变量 创建游标
SELECT sql FROM #ColumnIsNull --提取#files中的sql语句
OPEN cur --打开游标
WHILE 1 = 1 --开始循环
BEGIN
FETCH cur INTO @sql --将游标值写入sql
IF @@fetch_status <> 0 --若语句失败
BREAK --循环结束
exec sp_executesql @sql, N'@count varchar(20) out', @cou out --执行sql
update #ColumnIsNull set [count] = @cou where sql = @sql --更新插入行数
print(@sql) --打印sql
END
--DEALLOCATE cur ----摧毁游标所占资源
--查看临时表
select * from #ColumnIsNull
--删除临时表
drop table #ColumnIsNull
网友评论