declare @i intset @i = 0 while @i < 100 begin .........
set @i = @i + 1 end
在调用存储过程的程序里用循环处理(如果是vb、vc什么的调用存储过程就依次把数组里的值传过去) ; for i = 1 to 10 ... next (vb) 等价于 set @i = 1 while(@i<11) begin ... set @i = @i+1 end
如果是sqlserver2000的话,数组可以使用表变量来实现,而且功能更多。
不支持表变量的就只好用临时表了,while循环可以代替FOR循环。
存储过程相当于写的程序代码 数组要用SELECT 来赋值这样来写程序,不好 循环也可
數據庫有表,可以不用數組。用while代替FOR循环對表更好處理:如 select c1,c2 into #tab ... dealare @c1 varchar(50) while exists(select * from #tab) begin select top 1 @c1=c1 from #tab ... delete #tab where c1=@c1 end
SQL SERVER存储过程循环--游标 数组同理,楼上的很多贴子在误人子弟 ------------------------------------------- declare @BH as smallint begin transaction declare tbl_space cursor KEYSET for select BH from Tablename open tbl_space FETCH NEXT FROM tbl_space INTO @BH while (@@FETCH_STATUS=0) begin ....... ...... FETCH NEXT FROM tbl_space INTO @BH end close tbl_space DEALLOCATE tbl_space commit transaction
while @i < 100
begin .........
set @i = @i + 1
end
;
for i = 1 to 10
...
next (vb)
等价于
set @i = 1
while(@i<11)
begin
...
set @i = @i+1
end
数组要用SELECT 来赋值这样来写程序,不好
循环也可
select c1,c2 into #tab ...
dealare @c1 varchar(50)
while exists(select * from #tab)
begin
select top 1 @c1=c1 from #tab
...
delete #tab where c1=@c1
end
数组同理,楼上的很多贴子在误人子弟
-------------------------------------------
declare @BH as smallint
begin transaction
declare tbl_space cursor KEYSET
for
select BH from Tablename
open tbl_space
FETCH NEXT FROM tbl_space INTO @BH
while (@@FETCH_STATUS=0)
begin
.......
......
FETCH NEXT FROM tbl_space INTO @BH
end
close tbl_space
DEALLOCATE tbl_space
commit transaction
一种特殊的数据类型,用于存储结果集以供后续处理。该数据类型主要用于临时存储一组行,这些行将作为表值函数的结果集返回。语法
说明 使用 DECLARE @local_variable 声明 table 类型的变量。
table_type_definition ::=
TABLE ( { column_definition | table_constraint } [ ,...n ] ) column_definition ::=
column_name scalar_data_type
[ COLLATE collation_definition ]
[ [ DEFAULT constant_expression ] | IDENTITY [ ( seed , increment ) ] ]
[ ROWGUIDCOL ]
[ column_constraint ] [ ...n ] column_constraint ::=
{ [ NULL | NOT NULL ]
| [ PRIMARY KEY | UNIQUE ]
| CHECK ( logical_expression )
} table_constraint ::=
{ { PRIMARY KEY | UNIQUE } ( column_name [ ,...n ] )
| CHECK ( search_condition )
} 参数
table_type_definition与 CREATE TABLE 中定义表所用的信息子集相同的信息子集。表声明包括列定义、名称、数据类型和约束。允许的约束类型仅为 PRIMARY KEY、UNIQUE KEY 和 NULL。有关语法的更多信息,请参见 CREATE TABLE、CREATE FUNCTION 和 DECLARE @local_variable。collation_definition是由 Microsoft® Windows™ 区域设置和比较风格、Windows 区域设置和二进制表示法或 Microsoft SQL Server™ 排序规则组成的列的排序规则。注释
可将函数和变量声明为 table 类型。table 变量可用于函数、存储过程和批处理中。尽可能使用表变量而不使用临时表。table 变量有以下优点: table 变量的行为类似于局部变量,有明确定义的作用域。该作用域为声明该变量的函数、存储过程或批处理。
在其作用域内,table 变量可像常规表那样使用。该变量可应用于 SELECT、INSERT、UPDATE 和 DELETE 语句中用到表或表的表达式的地方。但是,table 不能用在下列语句中:INSERT INTO table_variable EXEC 存储过程。SELECT select_list INTO table_variable 语句。在定义 table 变量的函数、存储过程或批处理结束时,自动清除 table 变量。在存储过程中使用表变量与使用临时表相比,减少了存储过程的重新编译量。
涉及表变量的事务只在表变量更新期间存在。这样就减少了表变量对锁定和记录资源的需求。
不支持在表变量之间进行赋值操作。另外,由于表变量作用域有限,并且不是持久数据库的一部分,因而不受事务回滚的影响。