2014-02-10 82 views
0

我正在編寫一個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 
+0

不要緊,你做載於'@ replaced'字符串的內容。只要你將一個* single *字符串參數傳遞給IN()運算符,它將繼續被視爲一個* single *字符串。這並不奇怪。你可以命名*任何*編程語言,系統會突然決定檢查一個字符串的內容,查看逗號和引號字符,並決定神奇地將單個值轉換爲多個值? –

+0

當我們將字符串傳遞給IN時,它應該用單引號括起來,否則會給出一個錯誤,所以將','替換爲「',」。在置換set(@replaced =''''+ REPLACE(@trimmed,',',',''')+'''')後,輸入的內容是在020,N20, ('020','N20','G19')像這樣,只有我們必須在字符串中輸入正確的 –

+1

不,您還在某種程度上不理解我上面所說的。包含'abc','def','ghi''的* single *字符串與三個* separate *字符串完全不同,它們作爲三個獨立的參數傳遞給IN(),它們之間用逗號隔開。 –

回答

1

的問題是,你不能一個變量傳遞給IN操作符,你必須使用動態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,',',''',''')+''''; 
declare @qry nvarchar(max); 
set @qry = 'select * from BillingSystem where BillingCode IN (' + @replaced + ')'; 
exec sp_executesql @qry 
end 
+0

轉到sql查詢的參數是字符串「020,N19,A04」。當我執行你的SQL它提供錯誤的語法附近','。 –

+0

@Sushma Gowda 嘗試更換行 set @ replaced = REPLACE(@trimmed,',',''','''); 與此 set @replaced =''''+ REPLACE(@trimmed,',',''',''')+''''; –

+0

我也以這種方式嘗試了我的代碼,我寫的代碼與你提到的相同,我希望你看到了sql代碼.its給語法錯誤。 –

0

我給你,作爲替代品的工作功能,但這樣你就按照你的數據庫語言修改它在PostgreSQL的..

CREATE OR REPLACE FUNCTION public.fn_replacer(to_rep character varying, by_rep character varying, str character varying) 
RETURNS character varying 
LANGUAGE plpgsql 
AS $function$ 
declare 
res varchar2(100); 

BEGIN 
    for i in 1.. length(str) 
    loop 
    res:= res||(case when substr(str,i,1)=TO_rep then by_rep else substr(str,i,1) end); 
    end loop; 

return res; 
end $function$; 
0

您不需要更換任何東西。如果您使用「IN」作爲動態值,則需要動態地自行準備查詢。

通過

CREATE PROCEDURE [dbo].[usp_GetBillingSystem] 
(
    @BillingCode VARCHAR(300) 
) 
AS 

BEGIN 

    exec('select * from BillingSystem where BillingCode IN ('[email protected]+')') 

END 

更換你的代碼必將工作

+0

當我通過'020,G19'它給出錯誤的無效列名'020'無效的列名'G19' –

+0

請給我看你使用的代碼「usp_GetBillingSystem」Strored Procedure –

+0

我添加了一個vb.net的代碼,在這裏使用「usp_GetBillingSystem」 –