2014-12-08 80 views
0
Create procedure [dbo].[sp_Sample] 
    @param1 varchar(100) 
as 
    DECLARE @Sql VARCHAR(MAX) 
    SET @param1 = REPLACE(@param1,',',''',''') 
    SET @Sql = 'select * from tblSample where col1 IN (''' + @param1 + ''')' 
Go 

DECLARE @return_value int 

EXEC @return_value = [dbo].[sp_Sample] 
    @Escalation = N'SIM4' 

SELECT 'Return Value' = @return_value 
GO 

當我執行這個程序不顯示任何傳遞多個值

建議我如果有什麼錯

感謝

回答

0

您還沒有執行你的動態查詢,試試這個:

CREATE PROCEDURE [dbo].[sp_Sample] @param1 VARCHAR(100) 
AS 
    DECLARE @Sql NVARCHAR(MAX) 

    SET @param1 = Replace(@param1, ',', ''',''') 
    SET @Sql = 'select * from tblSample where col1 IN (''' 
       + @param1 + ''')' 
    --PRINT @Sql 
    exec sp_executesql @Sql 
GO 

Exec sp_Sample '1001' 
GO 

注:PRINT聲明將打印查詢字符串,取消註釋並檢查您的查詢。

+0

感謝響應迪帕克。 – user3209075 2014-12-08 09:52:01

+0

我在我的sql中被冠以相同的代碼。 – user3209075 2014-12-08 09:52:17

+0

當我執行[sp_Sample] 'value1'其給出錯誤:名稱'select * from tblSample其中col1 IN('value1')'不是一個有效的標識符。 – user3209075 2014-12-08 09:53:30

0

有哪些需要很多東西在這裏更新:

CREATE PROCEDURE [dbo].[sp_Sample] 
@param1 nvarchar(100),-- change 1 as you have added N for @Escalation 
@return int OUTPUT -- change 2: define output variable for value the sp will return 
AS 

    SET NOCOUNT ON; 
    DECLARE @SQLString nvarchar(500); 
    DECLARE @ParmDefinition nvarchar(500); 

    /* Build the SQL string one time.*/ 
    SET @SQLString = 
     N'SELECT @return_valueOUT = Id from tblSample where col1 
     IN (''' + @param1 + ''')' ; 

    SET @ParmDefinition = N'@return_valueOUT int OUTPUT'; 

    -- dynamically execute the query 
    EXECUTE sp_executesql @SQLString, @ParmDefinition, 
      @[email protected] OUTPUT; 

RETURN 
GO 

-- Declare the variable to receive the output value of the procedure. 
DECLARE @return_value int; 
-- Execute the procedure specifying a input parameter 
-- and saving the output value in the variable @return_value 
EXECUTE [dbo].[sp_Sample] N'Test', @return = @return_value OUTPUT; 
-- Display the value returned by the procedure. 
-- as @return_value is an int cast it to varchar 
Select 'Return Value = ' + convert(varchar(10),@return_value); 
GO 

DEMO