我想在SQL服务器中创建一个存储过程,我将传递一个参数“N type int”。然后过程将创建乘法表 [英] I want to create a stored procedure in SQL server where I will pass a parameter "N type int" and then procedure will create multiplication tables
本文介绍了我想在SQL服务器中创建一个存储过程,我将传递一个参数“N type int”。然后过程将创建乘法表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说N = 2
预期产量
lets say N=2
expected output
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
10 20
然后在下一栏中我想要两列或n列的总和。
什么我试过了:
and then in next column i want sum of both columns or n number of columns.
What I have tried:
ALTER procedure [dbo].[project]
@N int
AS
BEGIN
declare @tbl1 table(ID int identity(1,1), MT int )
declare @tbl2 table(ID int identity(1,1), MT int)
declare @T int
declare @i int, @j int
set @i=1
while @i<=@N
begin
set @j=1
while @j<=10
begin
set @T=@i*@j
insert into @tbl1 values(@T)
set @j=@j+1
end
select *from @tbl1
set @i=@i+1
end
END
以上代码创建乘法输出,但在不同的表中,请建议一个带查询的解决方案。
above code creates multiplication output but in different tables, please suggest a solution with query.
推荐答案
试试这个:
Try this:
DECLARE @n INT = 2;
;WITH cte AS
(
-- create a common table expression with ten rows with the "Value" column
-- being set to the row number
SELECT TOP(10) Value = ROW_NUMBER()
OVER (ORDER BY[object_id]) FROM SYS.ALL_OBJECTS
)
-- and then select the rows, identify the mulitplier (@n), and set the result
SELECT Value,
@n AS Multiplier,
Value * @n AS Result
FROM cte;
你最终应该有10行显示值(1-10) ,乘数(在这种情况下,它是2),以及乘以乘数的结果。
如果你想要超过10行,只需更改 TOP(10)
子句反映所需的值。
这篇关于我想在SQL服务器中创建一个存储过程,我将传递一个参数“N type int”。然后过程将创建乘法表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文