sql 存储过程 where条件 in 字符串参数

 一、使用   sp_executesql   系统存储过程执行   Unicode   字符串
  1、直接组合   SQL   语句执行
  CREATE   PROCEDURE   p_Test1
  @TableName   varchar(20)
  AS
  declare   @SQLString   nvarchar(200)
  set   @SQLString   =   N 'select   *   from   '   +   @TableName
  EXECUTE   sp_executesql   @SQLString
  2、SQL   语句里包含嵌入参数
  CREATE   PROCEDURE   p_Test2
  @TableName   varchar(20),
  @UserID   int,
  @UserName   varchar(50)
  AS
  declare   @SQLString   nvarchar(200)
  
  set   @SQLString   =   N 'select   *   from   '   +
  @TableName   +
  N '   where   UserID=@UserID   or   UserName=@UserName '
  
  EXECUTE   sp_executesql   @SQLString,
  N '@UserID   int,   @UserName   varchar(50) ',
  @UserID,   @UserName
  
  这也是   Microsoft   SQL   Server   的推荐做法。
  
  二、使用EXECUTE语句执行字符串
  CREATE   PROCEDURE   p_Test3
  @TableName   varchar(20)
  AS
  declare   @SQLString   nvarchar(200)
  set   @SQLString   =   N 'select   *   from   '   +   @TableName
  EXEC(@SQLString)

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

 
Declare  @ParamIDList Varchar(500)
sql 字符串参数 - qingtiansunsky - The world in my eyes     
Set @ ParamIDList=’张三,李四,王五’
    sql 字符串参数 - qingtiansunsky - The world in my eyes
Select 姓名,性别,年龄 From  Persons where Name In(@ ParamIDList)
sql 字符串参数 - qingtiansunsky - The world in my eyes


 

    这样写 ,数据库分析成为了

         Select 姓名,性别,年龄 From  Persons where Name =@ ParamIDList

(和上面的语句是等价的)

    这样情况的原因是因为数据库不会去分析变量中的, 号,并将变量拆分开来重新组合成新的SQL语句。

 

目录:

1.0       Table 变量和临时表,数据库中的表的区别和相同点

2.0       解决上面问题的两种途径

2.1 使用 ExecSQL命令字符串)的方式 动态的返回数据

2.2 使用Table数据类型方式解决

  

1.0   Table 变量和临时表,数据库中的表的区别和相同点

       Table变量的概念

         Table变量是一种特殊的数据类型,在SQL程序中可以将多维的信息零时的保存到Table变量中,供后  续处理,该数据类型保存数据的形式为一组行,这些行将可作为表值函数的结果集返回。

Table 变量不能作为 存储过程,和自定义函数的变量

 

   临时表

   由会话创建于tempdb 数据库的 sysobjects 表中的临时表

 

 Table和临时表,数据库中表的区别:

SQL语句的支持

Table 变量

对于表的列的约束类型仅仅为PRIMARY KEYUNIQUE KEY NULL

INSERT INTO table_variable EXEC 存储过程。

支持基本的SELECTINSERTUPDATE DELETE基本的表操作语句

 

临时表和数据库中实际的表 对表的语句的支持是一致(全部支持)。

 

实际的存储位置和生存的时间

Table 对象 和其他的类型的变量生成的周期是一样的,有明确的作用域,在超过作用域时系统自动的删除Table对象。

Table 对象实际上和其他类型的变量一样保存在内存中

 

临时表和数据库中的表都存储在实际的数据库中

临时表 #TableName的作用域是当前的会话中

              ##TableName 的作用域为全局量

 

 

2.0解决上面问题的两种途径

          2.1 使用 Exec(SQL命令字符串)的方式 动态的返回数据

sql 字符串参数 - qingtiansunsky - The world in my eyesDeclare @Command  Varchar(1000)   
sql 字符串参数 - qingtiansunsky - The world in my eyes
Declare  @ParamIDList Varchar(500)
sql 字符串参数 - qingtiansunsky - The world in my eyes
Set @ ParamIDList=’张三,李四,王五’
sql 字符串参数 - qingtiansunsky - The world in my eyes
Set   @Command  =Select 姓名,性别,年龄 From  Persons where Name In(’+@ ParamIDList+’)’
sql 字符串参数 - qingtiansunsky - The world in my eyes
Exec(@Command
)
sql 字符串参数 - qingtiansunsky - The world in my eyes



         2.2 使用Table数据类型方式解决

 

sql 字符串参数 - qingtiansunsky - The world in my eyes sql 字符串参数 - qingtiansunsky - The world in my eyes /**/ /* 根据输入的@ParamIDList列表来生Name列表*/
sql 字符串参数 - qingtiansunsky - The world in my eyes 
Declare    @ParamIDList   Varchar ( 500 )
sql 字符串参数 - qingtiansunsky - The world in my eyes D
eclare   @Table_NameList   table  ( Name  Varchar ( 20 ))   --  建立表变量
    Declare   @Index_Param   int     /**/ /*参数 记录分隔符的位置*/
   
Declare   @NeedParse   varchar ( 500 /**/ /*参数 没有处理的字符串*/
sql 字符串参数 - qingtiansunsky - The world in my eyes
sql 字符串参数 - qingtiansunsky - The world in my eyes 
Select    @Index_Param = CharIndex ( ' , ' @ParamIDList )
sql 字符串参数 - qingtiansunsky - The world in my eyes 
if  ( @Index_Param = 0 )
sql 字符串参数 - qingtiansunsky - The world in my eyessql 字符串参数 - qingtiansunsky - The world in my eyes
begin          /**/ /*一个名字组成*/
sql 字符串参数 - qingtiansunsky - The world in my eyes          
insert   into   @Table_NameList  (Name)  values ( @ParamIDList )
sql 字符串参数 - qingtiansunsky - The world in my eyes 
end
sql 字符串参数 - qingtiansunsky - The world in my eyessql 字符串参数 - qingtiansunsky - The world in my eyes
else       /**/ /*存在多个名字*/
sql 字符串参数 - qingtiansunsky - The world in my eyes        
begin
sql 字符串参数 - qingtiansunsky - The world in my eyes             
set   @NeedParse   = @ParamIDList
sql 字符串参数 - qingtiansunsky - The world in my eyes             
while  ( CharIndex ( ' , ' @NeedParse ) > 0 )
sql 字符串参数 - qingtiansunsky - The world in my eyes                 
begin
sql 字符串参数 - qingtiansunsky - The world in my eyes                         
insert   into   @Table_NameList  (Name)  values ( SubString ( @BeginString , 1 , CharIndex ( ' , ' , @BeginString ) - 1 ))
sql 字符串参数 - qingtiansunsky - The world in my eyes                        
set   @NeedParse   = SubString ( @NeedParse , CharIndex ( ' , ' @NeedParse ) + 1 , len ( @NeedParse ) - CharIndex ( ' , ' @NeedParse ))
sql 字符串参数 - qingtiansunsky - The world in my eyes            
end
sql 字符串参数 - qingtiansunsky - The world in my eyes        
insert   into   @Table_NameList  (Name)  values ( @NeedParse )
sql 字符串参数 - qingtiansunsky - The world in my eyes    
end
sql 字符串参数 - qingtiansunsky - The world in my eyes
sql 字符串参数 - qingtiansunsky - The world in my eyes
sql 字符串参数 - qingtiansunsky - The world in my eyes
Select  姓名,性别,年龄  From   Persons  where  Name  In select  Name  from   @Table_NameList
sql 字符串参数 - qingtiansunsky - The world in my eyes

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值