如何使用SQL Server 2000中的while循环存储查询返回的记录 [英] How to store the records returned by a query using while loop in SQL server 2000

查看:71
本文介绍了如何使用SQL Server 2000中的while循环存储查询返回的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从sql server中的表生成随机问题

i想要使用存储过程,其输入参数将是来自网页(.aspx)的整数。



以下是生成10个随机问题的查询。

可以将记录存储在sql server中的临时表或数组中(我怀疑它是否可用)我可以在ASP.NET 2.0(VS 2005)中使用sqldata适配器在.aspx页面上显示它们。还有更好的办法吗?



任何指导对我都有很大帮助

thanx



I want to generate random questions from a table in sql server
i would like to use stored procedure whose input parameter will be integer that comes from web page(.aspx).

Below is the query to generate 10 random questions.
can i store the records in a temp table or array in sql server (i doubt if it is available) from where i can display them on a .aspx page using sqldata adapter in ASP.NET 2.0(VS 2005). Is there a better way to do?

Any guidance will be of great help to me
thanx

# DECLARE @intFlag INT
#  
# DECLARE @random INT
# DECLARE @upper INT
# DECLARE @lower INT
#  
# SET @intFlag = 1
#  
#  
# set @lower=1
# set @upper=100
#  
# WHILE (@intFlag <=10)
#  
# BEGIN
# select @random=Round(((@upper-@lower-1))*RAND()+@lower,0)
#  
#  
# PRINT @random
#  
# select * from science where ques_no=@random
#  
# SET @intFlag = @intFlag + 1
#  
# END
#  
# GO

推荐答案

是 - 只需创建一个临时表并将行插入其中。然后,您可以在查询结束时选择临时表内容。

例如:

Yes - just create a temporary table and insert the rows into that. You can then select the temporary table content at the end of the query.
For example:
DECLARE @M int
SET @M = 1
DECLARE @TAB TABLE ([Month No] INT, [Month Name] VARCHAR(20))
WHILE (@M <=12)
BEGIN
INSERT INTO @TAB VALUES(@M, DATENAME(Month, CAST('2000-' + CAST(@M AS VARCHAR(2)) + '-1' AS DATE)))
SET @M = @M + 1
END
SELECT * FROM @TAB

创建一个包含月份编号和名称的表格,并填写它,最后返回整个表格。

Creates a table containing month number and name, and fills it, returning the whole table at the end.


这篇关于如何使用SQL Server 2000中的while循环存储查询返回的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆