Subhash Sharma

Subhash Sharma
Subhash Sharma

This is Subhash Sharma(Software Engineer) Blog

Welcome to this blog and find every solution.............

Search This Blog

Software Engineer(Subhash Sharma)

Software Engineer(Subhash Sharma)
Software Engineer

Wednesday, February 23, 2011

sql server funcation Return Table (User Defined Function)

create function fun(@id int)
returns table
as
return (select name from students where id=@id)


//How to use in sql server
select * from master.dbo.fun(1);

////how to use in asp.net

con.open();
SqlCommand cmd = new SqlCommand("select name from schools.dbo.fun(3)", con);
SqlDataReader dr2 = cmd.ExecuteReader();

if (dr2.HasRows)
{
while (dr2.Read())
{
string s= dr2.GetString(0);
}
}
con.close();



///another Function return string

CREATE FUNCTION GetFullName()
RETURNS varchar(100)
AS
BEGIN
RETURN 'Doe, John'
END

//how to use function in sql server

PRINT Schools.dbo.GetFullName();

No comments:

Post a Comment