iT邦幫忙

0

ASP.NET GridView checkbox 與 SQL 問題 _已解決

各位大大好,想請問Gridview的基本問題
資料庫Table A 中有一個清單如下

 List      Check
1    A        ok
2    B        ok
3    C       unknow
4
5

當所有的List都被checked以後有辦法INSERT一筆"All Check OK"到Table B的欄位中嗎?
另外如果只要有一筆資料沒有被Check ok就INSERT尚有資料沒被check
請問要怎麼實現呢??

tx50xyz iT邦新手 4 級 ‧ 2018-04-10 12:25:18 檢舉
一般做法是,取出資料要寫判別資料的值,來顯示,但你的問題是出在判別值判別錯,如A=A,但你的判別因該是A<>A而產生的,建議你自己列印出來,你就知道判別是那裡錯了
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
暐翰
iT邦大師 1 級 ‧ 2018-04-09 17:32:29
最佳解答

效果圖:

Script:


--建立測試資料
drop table #table_A;
drop table #table_B;
create table #table_A (List nvarchar(2), [Check] nvarchar(10) );
insert into #table_A (List,[Check])
values ('A','ok')
,('B','ok')
,('C','unknow')
create table #table_B (updatetime datetime, [Check] nvarchar(100) );


--測試還有一筆資料還沒CHECK
insert into #table_B (updatetime,[Check])
select sysdatetime()
	,case 
		when EXISTS(select 1 from #table_A where [Check]<>'ok') then  N'尚有資料沒被check'
	else
		'All Check OK'
	end
;

select * from #table_B;

--測試全部資料都CHECK
UPDATE #table_A
SET [Check] = 'ok';

insert into #table_B (updatetime,[Check])
select sysdatetime()
	,case 
		when EXISTS(select 1 from #table_A where [Check]<>'ok') then  N'尚有資料沒被check'
	else
		'All Check OK'
	end
;
select * from #table_B;

邏輯:


使用exists判斷,是不是表格A的check欄位都是'ok'狀態
假如是返回字串'All Check OK'
假如不是返回'尚有資料沒被check'

PS.使用exists而非count原因是效率好


有問題再跟我說 :)

看更多先前的回應...收起先前的回應...
小火車 iT邦新手 4 級 ‧ 2018-04-09 23:12:35 檢舉

謝謝大大的詳解

小火車 iT邦新手 4 級 ‧ 2018-04-09 23:29:50 檢舉
select sysdatetime()
	,case 
		when EXISTS(select 1 from #table_A where [Check]<>'ok') then  N'尚有資料沒被check'

大大請問"sysdatetime()"跟"select 1"什麼意思 我不太懂@@

暐翰 iT邦大師 1 級 ‧ 2018-04-10 10:00:14 檢舉

sysdatetime()

跟C# DateTime.Now 意思一樣
獲取現在的時間,類型datetime

在這邊用意是insert log 時間

select 1

這邊select 1 是跟 EXISTS 做配合
1可以換成任何一個數100、99、'xxoo' 都可以
它只是給exists做判斷有無資料用

小火車 iT邦新手 4 級 ‧ 2018-04-10 10:49:46 檢舉

非常感謝大大 我弄懂了
另外想請問如果#table_A 裡的 [Check] 欄位
如果有DBNULL值會影響判斷嗎?
另外如果要改成update #tableB語法要怎麼修改
不好意思 問題很多

暐翰 iT邦大師 1 級 ‧ 2018-04-10 14:23:56 檢舉

另外想請問如果#table_A 裡的 [Check] 欄位
如果有DBNULL值會影響判斷嗎?

不會,因為判斷標準是'ok',除了此字串之外都是沒check狀態

另外如果要改成update #tableB語法要怎麼修改

不知道你要的邏輯,可以提供嗎

小火車 iT邦新手 4 級 ‧ 2018-04-10 15:32:43 檢舉

大大這是我修改後的結果,但是好像判斷錯誤了 @@
依Pa_infor_ID 9 , 10 筆為例哪邊有錯嗎??
https://ithelp.ithome.com.tw/upload/images/20180410/20107444qpVk4eKNIp.jpg
https://ithelp.ithome.com.tw/upload/images/20180410/20107444vIUgntqKCM.jpg

UPDATE [Table B] SET [Status] = case when EXISTS(select 1 from Table A where [Certification]<>'True') then N'尚有資料未被審核' else '已審核' end, RJ_Updatetime = sysdatetime() where Pa_infor_ID=FK_Pa_infor_ID

[Certification] 資料型態 bit
不好意思 我原先說的不清楚
好像會造成邏輯上的錯誤

暐翰 iT邦大師 1 級 ‧ 2018-04-10 17:46:39 檢舉

mssql bit概念是只有 0,1,null的差別
如圖:

所以要改成

UPDATE [Table B] SET [Status] = case when EXISTS(select 1 from Table A where [Certification]<>1 or [Certification] is null ) then N'尚有資料未被審核' else '已審核' end, RJ_Updatetime = sysdatetime() where Pa_infor_ID=FK_Pa_infor_ID
小火車 iT邦新手 4 級 ‧ 2018-04-11 09:44:57 檢舉

謝謝大大 耐心的教導 非常感謝你

我要發表回答

立即登入回答