我正在編寫一個SQL查詢,因此它必須將,
替換爲','
以傳遞字符串。正在傳遞的字符串就像jack,john,tony
,但輸出只是帶有列標題的空表。如何在sql中用「','」替換符號「,」?
你能爲此提供答案嗎?
CREATE PROCEDURE [dbo].[usp_GetBillingSystem]
(
@BillingCode VARCHAR(300)
)
AS
BEGIN
declare @ListOfPlanID varchar(30)
declare @trimmed varchar(30)
declare @replaced varchar(30)
set @ListOfPlanID [email protected]
If IsNull(@ListOfPlanID,'')='' return
If LEN(@ListOfPlanID)=0 return
SET @trimmed=LTRIM(RTRIM(@ListOfPlanID))
set @replaced=''''+REPLACE(@trimmed,',',''',''')+''''
select * from BillingSystem where BillingCode IN (@replaced)
END
在vb.net我寫了獲取數據表中的代碼從上面的SQL查詢這樣
Public Function getBillingSystemAttributesInfo(ByVal ConnectionString As String, ByVal BillingCode As String) As DataTable
Dim conn As SqlConnection
Dim sqlcmd As SqlCommand
Dim dtBillingSystem As DataTable
Try
conn = New SqlConnection(ConnectionString)
conn.Open()
sqlcmd = New SqlCommand("usp_GetBillingSystem", conn)
With sqlcmd
.CommandType = CommandType.StoredProcedure
.Parameters.Add("@BillingSystemCode", SqlDbType.VarChar, 300)
.Parameters("@BillingSystemCode").Value = BillingSystemCode
dataadapter = New SqlDataAdapter()
dataadapter.SelectCommand = sqlcmd
dtBillingSystem = New DataTable("BillingSystem")
dataadapter.Fill(dtBillingSystem)
End With
Catch ex As Exception
Throw ex
Finally
conn.Close()
sqlcmd = Nothing
End Try
Return dtBillingSystem
End Function
不要緊,你做載於'@ replaced'字符串的內容。只要你將一個* single *字符串參數傳遞給IN()運算符,它將繼續被視爲一個* single *字符串。這並不奇怪。你可以命名*任何*編程語言,系統會突然決定檢查一個字符串的內容,查看逗號和引號字符,並決定神奇地將單個值轉換爲多個值? –
當我們將字符串傳遞給IN時,它應該用單引號括起來,否則會給出一個錯誤,所以將','替換爲「',」。在置換set(@replaced =''''+ REPLACE(@trimmed,',',',''')+'''')後,輸入的內容是在020,N20, ('020','N20','G19')像這樣,只有我們必須在字符串中輸入正確的 –
不,您還在某種程度上不理解我上面所說的。包含'abc','def','ghi''的* single *字符串與三個* separate *字符串完全不同,它們作爲三個獨立的參數傳遞給IN(),它們之間用逗號隔開。 –