存储过程中动态条件的添加 where条件 in条件中字符串参数

http://topic.csdn.net/u/20070531/11/2777896d-27c0-4ddc-9c9c-5983dfff5a30.html?110579472 http://www.cnblogs.com/macroxu-1982/archive/2007/03/05/664500.html
一、使用   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  @ParamIDListVarchar(500) sql 字符串参数 - qingtiansunsky - The world in my eyes     Set @ ParamIDList=’张三,李四,王五’     sql 字符串参数 - qingtiansunsky - The world in my eyesSelect 姓名,性别,年龄 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 eyesDeclare  @ParamIDListVarchar(500) sql 字符串参数 - qingtiansunsky - The world in my eyesSet @ ParamIDList=’张三,李四,王五’ sql 字符串参数 - qingtiansunsky - The world in my eyesSet   @Command  =Select 姓名,性别,年龄 From  Persons where Name In(’+@ ParamIDList+’)’ sql 字符串参数 - qingtiansunsky - The world in my eyesExec(@Command) sql 字符串参数 - qingtiansunsky - The world in my eyes

 

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

 

sql 字符串参数 - qingtiansunsky - The world in my eyes/* 根据输入的@ParamIDList列表来生Name列表*/ sql 字符串参数 - qingtiansunsky - The world in my eyes Declare  @ParamIDListVarchar(500) sql 字符串参数 - qingtiansunsky - The world in my eyes Declare@Table_NameListtable ( Name Varchar(20))  -- 建立表变量    Declare@Index_Paramint   /*参数 记录分隔符的位置*/     Declare@NeedParsevarchar(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 eyesbegin        /*一个名字组成*/ sql 字符串参数 - qingtiansunsky - The world in my eyes          insertinto@Table_NameList (Name) values(@ParamIDList) sql 字符串参数 - qingtiansunsky - The world in my eyes  end sql 字符串参数 - qingtiansunsky - The world in my eyeselse     /*存在多个名字*/ 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                          insertinto@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        insertinto@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 eyesSelect 姓名,性别,年龄 From  Persons where Name Inselect Name from@Table_NameList
原文地址:https://www.cnblogs.com/lvfeilong/p/hsfgfdgdfgfdg.html