比较数据库中的值 [英] Comparing values in database
问题描述
从代码中,您可以将2个变量作为参数传递给SP并获取DB中的行数.如果记录存在,则返回1,否则从SP中插入return 0.
或使用合并语句.
MERGE < target_table > [AS TARGET] 使用< table_source > [AS资源] 开启< search_condition > [匹配时 然后< merge_matched > ] [未按目标匹配时] 然后< merge_not_matched > ] [未按来源匹配时 然后< merge _ 匹配 > ];
请参考下面的链接,例如
SQL Server 2008中的合并语句 [ cmd = 新 SqlCommand(" + CLASS_ID.Text + " + TIME.Text + " ,con); dr = cmd.ExecuteReader(); 如果(!dr.HasRows) { 字符串 str = " + SUBJECT_CODE.Text + " " + CLASS_ID.Text + " ','" + DropDownList1.SelectedItem + ',' " + TIME.Text + ',' + FACULTY_ID.Text + " + DropDownList2.SelectedItem + " ',' + DURATION.Text + " ; cmd2 = 新 SqlCommand(str,con); cmd2.ExecuteNonQuery(); }
i m devloping timetable as my project. i wanted to compare two field timeslots and class. if they are already available in database then print approprite message otherwise it will store in database. pls help...
Hi,
From the code, you can pass the 2 variables as parameter to SP and get the count of rows in the DB.If the records exists return 1 else insert return 0 from the SP.
OR use a merge statement.
MERGE <target_table> [AS TARGET] USING <table_source> [AS SOURCE] ON <search_condition> [WHEN MATCHED THEN <merge_matched> ] [WHEN NOT MATCHED [BY TARGET] THEN <merge_not_matched> ] [WHEN NOT MATCHED BY SOURCE THEN <merge_ matched> ];
Refer the below link for example
Merge Statement in SQL Server 2008[^]
Hope this helps.
this may not be 100% currect but it will give some idea
cmd = new SqlCommand("select * from timetable where clo1='"+CLASS_ID.Text+"' and col2='"+TIME.Text+"'", con); dr = cmd.ExecuteReader(); if(!dr.HasRows) { String str = "insert into TIMETABLE values ('" + SUBJECT_CODE.Text + "','" + OFFER_ID.Text + "','" + CLASS_ID.Text + "','" + DropDownList1.SelectedItem + "','" + TIME.Text + "','" + FACULTY_ID.Text + "','" + DropDownList2.SelectedItem + "','" + DURATION.Text + "') "; cmd2 = new SqlCommand(str, con); cmd2.ExecuteNonQuery(); }
这篇关于比较数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!