Retrive临时表取决于来自其他表的vlaue [英] Retrive temp table depends on vlaue from other table

查看:65
本文介绍了Retrive临时表取决于来自其他表的vlaue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在SQL Server中,(和ASP.NET)



i需要创建存储过程,

优先:从table1中选择numberOfMonths(int value)

秒:返回临时表包含的行数等于数值numberOfMonths

此临时表仅包含on列(int类型)每行有一个值

第1行有:1

第2行有:2

......

row n有:n,n是返回值



但我不想在数据库中创建新表



我该怎么做? PLZ帮我

Hi all,
IN SQL Server, ( and ASP.NET )

i need to create stored procedure ,
first : select numberOfMonths (int value) from table1
second: return temp table contains number of rows equal to value numberOfMonths
this temp table consist from just on column (int type) each row have one value
row 1 have : 1
row 2 have : 2
......
row n have : n , n is the returned value

But I don't want to create new table in database

How can I do that ? plz help me

推荐答案

DECLARE @tablofmonth AS TABLE (MONINDEX int)
INSERT INTO @tablofmonth VALUES(1)
INSERT INTO @tablofmonth VALUES(2)
INSERT INTO @tablofmonth VALUES(3)
INSERT INTO @tablofmonth VALUES(4)
INSERT INTO @tablofmonth VALUES(5)
INSERT INTO @tablofmonth VALUES(6)
INSERT INTO @tablofmonth VALUES(7)
INSERT INTO @tablofmonth VALUES(8)
INSERT INTO @tablofmonth VALUES(9)
INSERT INTO @tablofmonth VALUES(10)
INSERT INTO @tablofmonth VALUES(11)
INSERT INTO @tablofmonth VALUES(12)

DECLARE @NumOfMonth INT = 6
SELECT TOP(@NumOfMonth)* FROM @tablofmonth ORDER BY MONINDEX


--for example
CREATE PROCEDURE SELECT_MONTH(
    @NumOfMonth INT
)
AS
BEGIN
    SELECT TOP(@NumOfMonth)* FROM yourtable
END
GO







在上面的查询中我使用了临时表我从中选择了一个数字。

你只需要点一个简单的选择在您的商店过程中查询作为示例演示

您需要在表名中替换您的表名。上面的sql部分仅用于演示查询的工作原理




in the above query i have used temporary table from which i have select a number.
you just need to fire a simple select query in your store procedure as demonstrating in example
you need to replace your table name in your table name place. above sql part is just for demonstration how query works


这篇关于Retrive临时表取决于来自其他表的vlaue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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