在 SQL Server 中的一个参数中传递多个值 [英] Pass multiple values in one parameter in SQL Server

查看:86
本文介绍了在 SQL Server 中的一个参数中传递多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我执行这个程序时什么都不显示

When I execute this procedure displays nothing

如果有什么不对的地方给我建议

Suggest me if anything wrong

谢谢

推荐答案

你没有执行你的动态查询,试试这个:

You are not executing your dynamic query, try this :

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 语句将打印查询字符串,取消注释并检查您的查询.

Note : PRINT statement will print the query string, uncomment it and check your query.

这篇关于在 SQL Server 中的一个参数中传递多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆