我有一个包含100多个表的SQL Server数据库。许多但并非所有都有一个名为ins_date
的列,用于保存每条记录在数据库中插入的日期。
我想获取数据库中每个表的最后(最大)插入日期。我已经对sp_MSforeachtable
进行了一些修补,这似乎足以遍历所有表格。
我遇到的问题:我的查询在遇到没有ins_date
列的表时会返回错误。我可以和sp_MSforeachtable
一起使用什么来仅从具有ins_date
的表中获取信息,并忽略或返回其他表的NULL?
答案 0 :(得分:0)
U可以尝试使用此代码来检查表
的空状态 string kyt = "SELECT * from sp_MSforeachtable where yourcondition=@yourcondition";
SqlCommand comm = new SqlCommand(kyt, con);
con.Open();
comm.Parameters.AddWithValue(@yourcondition,"valueofyourcondition");
SqlDataAdapter da = new SqlDataAdapter(comm);
SqlDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
dr["ins_date"].ToString();
if (dr["ins_date"] != DBNull.Value)
{
答案 1 :(得分:0)
您可能希望使用if语句将查询包装在@command1
中。像这样:
if exists(select 1 from INFORMATION_SCHEMA.columns where COLUMN_NAME = 'ins_date' and TABLE_NAME='?')