473,698 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a Table Valued MSSQL Function from C#

Hello All,

I am trying to call a Function in SQL Server which returns a TABLE type.

I get an exception

Unhandled Exception: System.Data.Sql Client.SqlExcep tion: The request for
procedu
re 'testFunc' failed because 'testFunc' is a function object.
at System.Data.Sql Client.SqlComma nd.ExecuteReade r(CommandBehavi or
cmdBehavior
, RunBehavior runBehavior, Boolean returnStream)
at System.Data.Sql Client.SqlComma nd.ExecuteReade r()
at CallSqlFunc.Mai n(String[] args)
The C# code which I have written is

using System;
using System.Data;
using System.Data.Sql Client;

public class CallSqlFunc
{
public static void Main(string[] args)
{
SqlConnection con = new
SqlConnection(@ "server=nt22914 1;trusted_conne ction=yes;datab ase=junk");
SqlCommand cmd = new SqlCommand("tes tFunc", con);
cmd.CommandType = CommandType.Sto redProcedure;
con.Open();
SqlDataReader reader = cmd.ExecuteRead er();
while(reader.Re ad())
{
Console.WriteLi ne(reader.GetIn t32(0));
}
con.Close();
}
}
The SQL Server Function code is

create Function testFunc ()
returns @junktable table (num int)
as
begin
insert @junktable values (1)
insert @junktable values (2)
return
end

Why doesn't this code work? I am able to call Scalar SQL Server
functions. The problem comes only when trying to call TVFs.

Thanks in advance for your help.

regards,
Abhishek.
Nov 15 '05 #1
1 14375
Abhishek,

This is just a guess, but you might have to issue a select against the
function, something like:

select * from dbo.testFunc()

This should return to you the function itself. When I run this in query
analyzer, it gives me the result set as I would expect.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldin o=at=exisconsul ting<dot>com

"Abhishek Srivastava" <ab******@nospa m.net> wrote in message
news:uo******** ******@TK2MSFTN GP11.phx.gbl...
Hello All,

I am trying to call a Function in SQL Server which returns a TABLE type.

I get an exception

Unhandled Exception: System.Data.Sql Client.SqlExcep tion: The request for
procedu
re 'testFunc' failed because 'testFunc' is a function object.
at System.Data.Sql Client.SqlComma nd.ExecuteReade r(CommandBehavi or
cmdBehavior
, RunBehavior runBehavior, Boolean returnStream)
at System.Data.Sql Client.SqlComma nd.ExecuteReade r()
at CallSqlFunc.Mai n(String[] args)
The C# code which I have written is

using System;
using System.Data;
using System.Data.Sql Client;

public class CallSqlFunc
{
public static void Main(string[] args)
{
SqlConnection con = new
SqlConnection(@ "server=nt22914 1;trusted_conne ction=yes;datab ase=junk");
SqlCommand cmd = new SqlCommand("tes tFunc", con);
cmd.CommandType = CommandType.Sto redProcedure;
con.Open();
SqlDataReader reader = cmd.ExecuteRead er();
while(reader.Re ad())
{
Console.WriteLi ne(reader.GetIn t32(0));
}
con.Close();
}
}
The SQL Server Function code is

create Function testFunc ()
returns @junktable table (num int)
as
begin
insert @junktable values (1)
insert @junktable values (2)
return
end

Why doesn't this code work? I am able to call Scalar SQL Server
functions. The problem comes only when trying to call TVFs.

Thanks in advance for your help.

regards,
Abhishek.

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3426
by: Bruce Lester | last post by:
Is there a way to create a trigger directly on an inline or multi-line table value function? I am trying to create a quick-and-dirty application using an Access Data Project front-end with SQL 2000 SP3 EE. Thanks.
9
5910
by: Phil Sowden | last post by:
I'd really appreciate your help creating a parameterised view in SQL Server, which I can then invoke from VB5. I'm using DAO and ODBC to connect to SQL Server 2000. I can open Tables and Views, but can't find how to create or use a "query" parameter. If I use the "?" (in the SQL Enterprise manager View design mode), the syntax checker verifies the query ok, but I then get the message "Parameters cannot be used in this query type". It...
7
1086
by: roger | last post by:
I'm having difficulties invoking a user defined table function, when passing to it a parameter that is the result of another user defined function. My functions are defined like so: drop function dbo.scalar_func go create function dbo.scalar_func() returns int
8
3599
by: Bill Ehrreich | last post by:
I'm faced with a situation where I will need to calculate a column for a resultset by calling a component written as a VB6 DLL, passing parameters from the resultset to the component and setting (or updating) a column with the result. I thought that perhaps the best way out would be to create a UDF that passes the parameters to the VB component using the sp_oa* OLE stored procs. For a test, I created an ActiveX DLL in VB6 (TestDLL) with...
5
2635
by: Zlatko Matiĉ | last post by:
Hello. How can I call some functions on MSDE when working in .mdb ? Especially in-line functions which are similar to stored procedures. How can I use MSDE in-line functions as recordsource for .mdb forms ? Can I call in-line functions using ADO ? I tried, but it seems that only stored procedures are allowed (adCmdStoredProc).... Thanks.
18
4348
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
4
1925
by: James Fraser | last post by:
I have run into a challenge that I'm not sure how to best solve. I'd appreciate any opinions or input. I am working with a third party database. They are storing some data that I need to use in a binary field. I've got the code to parse the binary and reconstruct what I need. Unfortunately, there might be multiple "entries" stored in a single binary field. a certain byte, let's just say the first, will always be the count of "entries" in...
10
3252
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a programming system places on how a procedure is called and how data is passed between a calling program and procedures are called calling conventions"
3
4974
by: simple simon | last post by:
How would I insert into multiple related tables using a table valued parameter? Is there any way to do this without using a WHILE loop? I know how to insert from a table valued parameter into one table: INSERT INTO dbo.Table SELECT * FROM @TVP But what do I do when I want to insert all of someone's information into multiple tables, and have those records related to each other by foreign keys? For example, I want to insert their address...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8898
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6524
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3051
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.