如何在MSSQL存储过程中使用单引号传入多个值 [英] How to pass in multiple values with single quote in MSSQL store proc

查看:270
本文介绍了如何在MSSQL存储过程中使用单引号传入多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我知道这是一个愚蠢的问题,但事实是我在过去1小时内一直试图弄清楚如何做到这一点。

好​​的,这是我的情况,



例如,我正在尝试使用如下的select语句将参数传递给store proc:



  DECLARE   @ strA   VARCHAR  20 )= ' '''''',''2''' 

选择 * from tableA
其中​​ ColA in @ strA





执行时没有错误,没有结果......这是错误的因为应该有结果返回,我测试了查询..



 选择 * 来自 tableA 
其中​​ ColA in ' 1'' 2'





请告知我的商店proc有什么问题。提前谢谢。

解决方案

如果要通过这样的数据传递数据,你必须使用动态sql

例如

  DECLARE   @ strA   VARCHAR  20 )= ' '''1',''2''' 

DECLARE @sql NVARCHAR (MAX)= ' select * from tableA其中ColA in(' + @ strA + ' )'
EXEC sp_sqlexec @sql


Hi all, i know this is stupid question, but the fact is i'm stuck with it over the past 1 hour trying to figure out how to do it.
ok, here is my situation,

for example, i'm trying to pass in a parameter to a store proc with the select statement like below:

DECLARE @strA VARCHAR(20) = '''1'',''2'''
 
select * from tableA
where ColA in (   @strA )



it executed with no error, no result...which is wrong because there should be result return, i tested the query..

select * from tableA
where ColA in ('1','2')



please advise what is wrong with my store proc. thanks in advance.

解决方案

You have to use dynamic sql if you are going to pass the data through like that
e.g.

DECLARE @strA VARCHAR(20) = '''1'',''2'''
 
DECLARE @sql NVARCHAR(MAX) = 'select * from tableA where ColA in (' + @strA + ')'
EXEC sp_sqlexec @sql


这篇关于如何在MSSQL存储过程中使用单引号传入多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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