我動態選擇使用另一個字符串構建的字符串。因此,如果字符串2 =「大衛旗幟」,然後MyDynamicString
應該是「DBanne」在Where子句中引用時,選擇動態字符串具有不同的值
Select
...
, Left(
left((select top 1 strval from dbo.SPLIT(string1,' ')) //first word
,1) //first character
+ (select top 1 strval from dbo.SPLIT(string1,' ')
//second word
where strval not in (select top 1 strval from dbo.SPLIT(string1,' ')))
,6) //1st character of 1st word, followed by up to 5 characters of second word
[MyDynamicString]
,...
From table1 Join table2 on table1pkey=table2fkey
Where MyDynamicString <> table2.someotherfield
我知道table2.someotherfield 不等於的動態字符串。然而,當我用Where的子句中的MyDynamicString替換爲完全左側(左側(等..函數,它按預期的方式工作)。
我可以在查詢中稍後引用此字符串嗎?是否必須使用左(左(等等。如果你這樣做,你有它上面的函數在where子句?
+1光滑,現在測試 – 2010-06-17 14:46:39
這是未經測試的,所以你可能不得不稍微調整語法,但我認爲這個想法應該可行。另請參閱我的最新編輯。確保在內聯視圖中添加列別名(即「theString」部分)。 – dcp 2010-06-17 14:48:10
如何在此內聯視圖中引用table1.string1?內聯視圖的FROM子句對我來說還不清楚。我可以使用'FROM table1 JOIN table2 ON table1pkey = table2fkey',但是我必須重複Where子句嗎?在我的實際代碼中,Where子句有另外5或6個條件 – 2010-06-17 15:06:30