2016-07-05 166 views
1

我在VB6應用程序有一個Select語句....SQL SERVER 2008 SELECT語句的情況下

這裏是什麼樣子.....

Dim str As string 

str = "SELECT CompID, Department from tblCompanies " & _ 
     "Where CompID in (123, 234, 345, 456) " & _ 
     "Order by CompID " 

那麼我tryign這裏要做的是添加一個CASE WHEN語句的WHERE子句 - 基本上我正在尋找添加一個字符串到每個部門名稱,具體取決於COMPID。所以,我需要指定這些都是COMPID的,我要選擇的話,我想這樣做soemthing像

Case when CompID = 123 Then ----ADD "GC" to that Department Name 

我想我需要之前,我打開我的記錄與要做到這一點

rs.open str, g_CN, adOpenStatic 

因爲一旦打開它,當我嘗試編輯它時,似乎給我帶來了錯誤。

所有的一切,如果我的記錄是這樣的......

Accounting 
Finance 
IT 
R&D 

我想使它看起來像

"GC" - Accounting 
"GC" - Finance 
"BP" - IT 
"DC" - R&D 
+1

那麼你真的需要過濾嗎?我正在閱讀的方式是,你只是想在選擇。 –

回答

1

更改爲,

Dim str As string 

str = "SELECT CompID, CASE WHEN CompID IN (123,234) THEN 'GC' " & _ 
       " WHEN CompID = 345 THEN 'IT' " & _ 
       " WHEN CompID = 456 THEN 'DC' " & _ 
       " ELSE '' END + ' - ' + Department AS Department from tblCompanies " & _ 
     "Where CompID in (123, 234, 345, 456) " & _ 
     "Order by CompID " 
0

試試這個:

str = "SELECT CompID, CASE WHEN CompID = 123 THEN '""GC"" - ' ELSE '' END + Department As Department from tblCompanies " & _ 
     "Where CompID in (123, 234, 345, 456) " & _ 
     "Order by CompID " 
+0

pelase請參閱編輯,謝謝!這似乎比我想象的要難一些 – BubbaSparxx