How to print a table row wise in SQL Server?

Jason Clark

I have created a function that outputs the table of a number, Here is my UDF scalar function:

create function fntable(@a int)
returns varchar(250)
as begin
declare @b int, @c varchar(250),@e varchar(50)=''
set @b=1
while (@b<=10)
begin
set @c=@a*@b
set @e=@e+@c
set @b=@b+1
end
return @e
end

It displays what i want, but it displays the result in a single row, as per my knowledge. I want to display the rows in multiple rows. How can i do this ? i want to print a table of a number.

lc.

You need a table-valued function. Using your code as is, this roughly translates to:

create function fntable(@a int)
returns @e table(c int)
as begin
    declare @b int = 1
    while (@b <= 10)
    begin
        insert into @e values (@a * @b)
        set @b += 1
    end

    return
end

Note, however, in this particular case the number of iterations is a constant 10 and the whole thing can be done inline in a single query, without a loop simply by manually expanding the loop. This can be then saved as an inline table-valued function, which can in turn be referenced from other queries.

create function fntable(@a int)
returns table as
return (
    select @a * 1 as c
    union all
    select @a * 2 as c
    union all
    select @a * 3 as c
    union all
    select @a * 4 as c
    union all
    select @a * 5 as c
    union all
    select @a * 6 as c
    union all
    select @a * 7 as c
    union all
    select @a * 8 as c
    union all
    select @a * 9 as c
    union all
    select @a * 10 as c
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to sum column wise and row-wise in SQL Server?

From Dev

How to perform row wise math operations in SQL Server?

From Dev

Row-wise encryption SQL Server 2016

From Dev

How to update table by applying row wise condition

From Dev

How to check if a row is present in SQL Server table or not?

From Dev

How to get column wise data in SQL Server?

From Dev

How To Arrange Table Data in Parent_Id And Child_Id Wise Via Sql-Server Query

From Dev

How to get elements from table by row number in sql server

From Dev

How to sum a specific row from one table to another in SQL Server

From Dev

How to add row to a SQL Server table with uniqueidentifier type column?

From Dev

SQL Query ,How to show result in row wise format?

From Dev

How to do row-wise comparison between SQL (Oracle) fields?

From Dev

How do I format the output of SQL query row-wise

From Dev

how to get sql select query result as column vs value wise instead of row wise without using PIVOT

From Dev

How to get the top record group wise in MS SQL Server?

From Dev

How to get Product wise stock using sql server query

From Dev

How to do Grouping Total by names wise in SQL SERVER

From Dev

how to get day wise dynamically get data using sql server?

From Dev

How to print all values from sql table instead of just first row

From Dev

How can i select row from table according to column values of row in sql server

From Dev

Compare two text columns from two different tables row wise in SQL server

From Dev

PIVOT table only in one row in SQL Server?

From Dev

Sql Server row size limit and table design

From Dev

SQL Server import row string to table columns

From Dev

SUM grouped table row value in SQL Server

From Dev

Sql Server row size limit and table design

From Dev

SQL SERVER PIVOT TABLE One Row

From Dev

SQL Server import row string to table columns

From Dev

Row wise operation on data.table

Related Related

  1. 1

    How to sum column wise and row-wise in SQL Server?

  2. 2

    How to perform row wise math operations in SQL Server?

  3. 3

    Row-wise encryption SQL Server 2016

  4. 4

    How to update table by applying row wise condition

  5. 5

    How to check if a row is present in SQL Server table or not?

  6. 6

    How to get column wise data in SQL Server?

  7. 7

    How To Arrange Table Data in Parent_Id And Child_Id Wise Via Sql-Server Query

  8. 8

    How to get elements from table by row number in sql server

  9. 9

    How to sum a specific row from one table to another in SQL Server

  10. 10

    How to add row to a SQL Server table with uniqueidentifier type column?

  11. 11

    SQL Query ,How to show result in row wise format?

  12. 12

    How to do row-wise comparison between SQL (Oracle) fields?

  13. 13

    How do I format the output of SQL query row-wise

  14. 14

    how to get sql select query result as column vs value wise instead of row wise without using PIVOT

  15. 15

    How to get the top record group wise in MS SQL Server?

  16. 16

    How to get Product wise stock using sql server query

  17. 17

    How to do Grouping Total by names wise in SQL SERVER

  18. 18

    how to get day wise dynamically get data using sql server?

  19. 19

    How to print all values from sql table instead of just first row

  20. 20

    How can i select row from table according to column values of row in sql server

  21. 21

    Compare two text columns from two different tables row wise in SQL server

  22. 22

    PIVOT table only in one row in SQL Server?

  23. 23

    Sql Server row size limit and table design

  24. 24

    SQL Server import row string to table columns

  25. 25

    SUM grouped table row value in SQL Server

  26. 26

    Sql Server row size limit and table design

  27. 27

    SQL SERVER PIVOT TABLE One Row

  28. 28

    SQL Server import row string to table columns

  29. 29

    Row wise operation on data.table

HotTag

Archive