Click here to Skip to main content
16,012,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Tejas Vaishnav 3-Oct-13 5:05am    
can you provide your desired out put of store procedure?
ocean99 3-Oct-13 5:05am    
notice : n = numberOfMonths
Tejas Vaishnav 3-Oct-13 5:06am    
means? you need to get number of month from your table..

like

1
2
3
4
ocean99 3-Oct-13 5:08am    
yes that is right
Tejas Vaishnav 3-Oct-13 5:10am    
i think there is a 12 month in your table and you need to pass a value to your storeprocedure like 5 then it will return 1,2,3,4,5 am i right?

1 solution

SQL
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



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
 
Share this answer
 
Comments
ocean99 3-Oct-13 5:24am    
Thank you so much ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900