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

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

来源:网络摘录 日期:2008-09-10 17:51 点击:0


  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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值