频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

如何给SQL SERVER存储过程传递数组参数

来源: 作者: 出处:巧巧读书 2007-12-16 进入讨论组

  Q. How can I pass an array of values to a SQL Server stored-procedure.
  (v1.1 1999.05.14)
  
  A. Basically you can't - SQL Server has no array type - ANSI SQL 92 does not specify array support. But there are various ways around it.
  
  确切的说不行-SQL SERVER没有数组类型,ANSI SQL 92标准也不支持数组。但可用其它的方法来实现。
  
  1. You could simulate an array by passing one or more varchar(255) fields with comma-separated values and then use a WHILE loop with PATINDEX and SUBSTR to extract the values.
  
  1、你可以使用几个VARCHAR(255)字段来模拟数组,字段中用逗号分开各个数据,然后使用循环和PATINDEX和SUBSTR分开这些数据。
  
  2. The more usual way to do this would be to populate a temporary table with the values you need and then use the contents of that table from within the stored-procedure. Example of this below
  
  2、通常这种方法需要为这些数据创建一个临时表,然后在存储过程使用表中的内容。如下例
  
  create procedure mytest @MyParmTempTable varchar(30)
  as
  begin
  -- @MyParmTempTable contains my parameter list...  这个变量是包含参数的表名
  
  -- For simplicity use dynamic sql to copy into a normal temp table...
  
  create table #MyInternalList (
  list_item varchar( 2 ) not null
  )
  
  set nocount on
  
  insert #MyInternalList
  exec ( "select * from " + @MyParmTempTable )
  
  set nocount off
  
  -- It is now easier to join..
  select *
  from sysobjects
  where type in ( select list_item from #MyInternalList )
  
  end
  go
  
  To call..
  
  create table #MyList (
  list_item varchar( 2 ) not null
  )
  insert #MyList values ( 'S' )
  insert #MyList values ( 'U' )
  insert #MyList values ( 'P' )
  
  exec mytest "#MyList"
  
  3. If all you wanted to do was use the array/list as input to an IN clause in a WHERE statement you could use :-
  
  3、如果你想在IN子句里使用输入的数组参数可以这样做:
  
  CREATE PROCEDURE sp_MyProcedure (@MyCommaDelimitedString
  Varchar(255))
  AS
  BEGIN
  EXEC ('SELECT * FROM MYTABLE WHERE MYFIELD IN (' + @MyCommaDelimitedString + ')')
  END
  GO保留:: http://www.qqread.com/network/server/s376178181.html 更多文章 更多内容请看SQL Server 数据处理专题SQL Server 索引和查询专题SQL Server专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章