0

壁に頭をぶつけて2日になります。この更新ステートメントは、ssmsで実行すると機能します

update dbo.DEMOGRAPHICS 
set ETHNICITY = (select distinct(ethnicity) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = 'P000084716' and ACADEMIC_SESSION != '')
, GENDER = (select distinct(GENDER) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = 'P000084716' and ACADEMIC_SESSION != '')
, MARITAL_STATUS = (select distinct(MARITAL_STATUS) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = 'P000084716' and ACADEMIC_SESSION != '')
, RELIGION = (select distinct(RELIGION) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = 'P000084716' and ACADEMIC_SESSION != '')
, VETERAN = (select distinct(VETERAN) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID =  'P000084716' and ACADEMIC_SESSION != '')
, CITIZENSHIP = (select distinct(CITIZENSHIP) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = 'P000084716'and ACADEMIC_SESSION != '')
,RETIRED = (select distinct(RETIRED) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = '000084716' and ACADEMIC_SESSION != '')
, LEGAL_RESIDENCE = (select distinct(LEGAL_RESIDENCE) from dbo.DEMOGRAPHICS where   PEOPLE_CODE_ID = 'P000084716' and ACADEMIC_SESSION != '')
where PEOPLE_CODE_ID = 'P000084716'  and ACADEMIC_SESSION = ''

しかし、コードで更新を行っても、

string updDemoRecords = @"update dbo.DEMOGRAPHICS 
set ETHNICITY = (select distinct(ethnicity) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = '{0}' and ACADEMIC_SESSION != '')
, GENDER = (select distinct(GENDER) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = '{0}' and ACADEMIC_SESSION != '')
, MARITAL_STATUS = (select distinct(MARITAL_STATUS) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = '{0}' and ACADEMIC_SESSION != '')
, RELIGION = (select distinct(RELIGION) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = '{0}' and ACADEMIC_SESSION != '')
, VETERAN = (select distinct(VETERAN) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = '{0}' and ACADEMIC_SESSION != '')
, CITIZENSHIP = (select distinct(CITIZENSHIP) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = '{0}'and ACADEMIC_SESSION != '')
,RETIRED = (select distinct(RETIRED) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = '{0}' and ACADEMIC_SESSION != '')
, LEGAL_RESIDENCE = (select distinct(LEGAL_RESIDENCE) from dbo.DEMOGRAPHICS where PEOPLE_CODE_ID = '{0}' and ACADEMIC_SESSION != '')
where PEOPLE_CODE_ID = '{0}'  and ACADEMIC_SESSION = ''";
updDemoRecords = string.Format(updDemoRecords, peopleOrgCodeID);
pcCon = new SqlConnection(pcConnString);
SqlCommand doUpdDemoRecords = new SqlCommand(updDemoRecords, pcCon);
pcCon.Open();
doUpdDemoRecords.ExecuteNonQuery();
pcCon.Close();

ストアドプロシージャからも試してみましたが、関連する行も更新されません。SQL Server 2008とC#.netです

4

1 に答える 1

1

問題は、実際の番号( )を使用しているように見えます。これには、SSMSバージョン(文字列を使用)で使用しているように必要と思われるpeopleOrgCodeID先頭のesがありません。0

したがって、peopleOrgCodeIdが値を持つ整数(または長整数)の場合84716WHERE句は次のようになります。

where PEOPLE_CODE_ID = '84716'

そしてそうではありません:

where PEOPLE_CODE_ID = '000084716'

これは、複合フォーマット文字列を使用するか、先行ゼロを使用して文字列値を渡すことで解決できます。


I would mention SQL Injection as a possible issue, but if peopleOrgCodeID is indeed an integer, you are safe in this instance. I would still use a parameterized query instead of string.Format, however.

于 2012-11-08T19:38:45.730 に答える