我有一个attandence表,每天通过在当天添加新列来更改 现在我希望看到月份的关注我不知道是否所有这些列都存在,如果通过添加完整的月份来选择所有列的查询
BM_query = @"[" + st.m + "/1" + "/" + st.y + "] ," + "[" + st.m + "/2" + "/" + st.y + "] ," +
"[" + st.m + "/3" + "/" + st.y + "] ," + "[" + st.m + "/4" + "/" + st.y + "] ," +
"[" + st.m + "/5" + "/" + st.y + "] ," + "[" + st.m + "/6" + "/" + st.y + "] ," +
"[" + st.m + "/7" + "/" + st.y + "] ," + "[" + st.m + "/8" + "/" + st.y + "] ," +
"[" + st.m + "/9" + "/" + st.y + "] ," + "[" + st.m + "/10" + "/" + st.y + "] ," +
"[" + st.m + "/11" + "/" + st.y + "] ," + "[" + st.m + "/12" + "/" + st.y + "] ," +
"[" + st.m + "/13" + "/" + st.y + "] ," + "[" + st.m + "/14" + "/" + st.y + "] ," +
"[" + st.m + "/15" + "/" + st.y + "] ," + "[" + st.m + "/16" + "/" + st.y + "] ," +
"[" + st.m + "/17" + "/" + st.y + "] ," + "[" + st.m + "/18" + "/" + st.y + "] ," +
"[" + st.m + "/19" + "/" + st.y + "] ," + "[" + st.m + "/20" + "/" + st.y + "] ," +
"[" + st.m + "/21" + "/" + st.y + "] ," + "[" + st.m + "/22" + "/" + st.y + "] ," +
"[" + st.m + "/23" + "/" + st.y + "] ," + "[" + st.m + "/24" + "/" + st.y + "] ," +
"[" + st.m + "/25" + "/" + st.y + "] ," + "[" + st.m + "/26" + "/" + st.y + "] ," +
"[" + st.m + "/27" + "/" + st.y + "] ," + "[" + st.m + "/28" + "/" + st.y + "] ," +
"[" + st.m + "/29" + "/" + st.y + "] ," + "[" + st.m + "/30" + "/" + st.y + "] " ;
query = "select Roll_No , Student_Name, " + BM_query + " from [" + textBox2.Text + "_attandence] ";
如果列不存在,则显示无效的列名,如2/7/2017 ....等等 我可以忽略这些不存在的列并继续下一个和下一个..
答案 0 :(得分:1)
您可以使用如下查询检索表列的名称:
select COLUMN_NAME
from information_schema.columns
where table_name = 'attendance'
然后遍历结果以构建您的查询。
但是说真的,你应该回到这个的绘图板上。该表违背了RDBMS所代表的一切。当你达到表所允许的最大列数时你打算做什么?
当你只想看一个月的唱片时,你打算做什么?或者当您想要将两个日期之间的出勤加起来?您无法利用SQL中内置的任何功能。您需要通过构建字符串来构建所有查询,而您尝试执行的所有操作都将变得缓慢而脆弱。
答案 1 :(得分:0)
如果您的数据表中填充了EVER扩展表中的数据,那么您可以检查列是否存在。
table.Columns.Contains("ColumnName");
答案 2 :(得分:0)
这项工作很好,给出了所有存在的列名,忽略了其他
DECLARE @dd INT;
SET @dd = 1;
DECLARE @tmp Varchar(5)
SET @tmp = '';
DECLARE @have Varchar(max)
SET @have = '';
WHILE @dd <= 31
BEGIN
IF COL_LENGTH('test','2/' + CONVERT(varchar, @dd) + '/2017') IS NOT NULL
set @have=@have+'2/' + CONVERT(varchar, @dd) + '/2017 ,';
SET @dd = @dd + 1;
END;
PRINT @have;
GO
答案 3 :(得分:0)
使用以下查询
select COLUMN_NAME
from information_schema.columns
where table_name = 'your table name'