473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Urgent problem - Stored Procedures using Created. Tables

I have a rather odd problem.

I have a SP which uses temp. tables along the way, and then returns a
table of results:

CREATE PROCEDURE dbo.usp_myproc(
@pNameList VARCHAR(6000)
)
AS

....
CREATE TABLE #MyTable (
Id INT
, Nm VARCHAR(255)
)
....
SELECT * FROM #MyTable;

GO

I am calling this using a "text" style SqlCommand, as follows:

SqlCommand pCommand = new SqlCommand("usp_myproc '" + namelist.Text +
"'");
pCommand.CommandType = CommandType.Text;
SqlConnection oConn = new SqlConnection(myConnectionString);
oConn.Open();
pCommand.Connection = oConn;
SqlDataAdapter oDtAd = new SqlDataAdapter(pCommand);
oDtAd.SelectCommand = pCommand; //new SqlDataAdapter(pCommand);
MyDataSet.Tables.Clear();
oDtAd.Fill(MyDataSet,"Results");
oConn.Close();

The oddity is that I'm getting back an Invalid Object exception -
"Invalid object name '#MyTable'"

If I take out the SELECT * FROM #MyTable and replace it with a select
from another table, it works fine.

If I change the table to a permanent table, the problem returns.

So it seems that the issue is one of using tables which have been
created in the same procedure.

It seems that the run of the stored proc is ignoring any DDL commands.
I have a large number of SPs which use temporary tables et al, and
these need to be accessed via ASP.NET

HELP!

From a security POV I am using a named user with db_owner access onto
both the target database and tempdb.

Any suggestions, anyone?

Mike.
Nov 18 '05 #1
2 1694
add parameters to command object. Don't pass them like that.

SqlCommand pCommand = new SqlCommand("usp_myproc", oConn);
pCommand.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("@pNameList ", SqlDbType.Varchar, 6000).Value =
namelist.Text ;

Hope that help's.

"Mike Hutton" <hu***@yahoo.co.uk> wrote in message
news:e1**************************@posting.google.c om...
I have a rather odd problem.

I have a SP which uses temp. tables along the way, and then returns a
table of results:

CREATE PROCEDURE dbo.usp_myproc(
@pNameList VARCHAR(6000)
)
AS

...
CREATE TABLE #MyTable (
Id INT
, Nm VARCHAR(255)
)
...
SELECT * FROM #MyTable;

GO

I am calling this using a "text" style SqlCommand, as follows:

SqlCommand pCommand = new SqlCommand("usp_myproc '" + namelist.Text +
"'");
pCommand.CommandType = CommandType.Text;
SqlConnection oConn = new SqlConnection(myConnectionString);
oConn.Open();
pCommand.Connection = oConn;
SqlDataAdapter oDtAd = new SqlDataAdapter(pCommand);
oDtAd.SelectCommand = pCommand; //new SqlDataAdapter(pCommand);
MyDataSet.Tables.Clear();
oDtAd.Fill(MyDataSet,"Results");
oConn.Close();

The oddity is that I'm getting back an Invalid Object exception -
"Invalid object name '#MyTable'"

If I take out the SELECT * FROM #MyTable and replace it with a select
from another table, it works fine.

If I change the table to a permanent table, the problem returns.

So it seems that the issue is one of using tables which have been
created in the same procedure.

It seems that the run of the stored proc is ignoring any DDL commands.
I have a large number of SPs which use temporary tables et al, and
these need to be accessed via ASP.NET

HELP!

From a security POV I am using a named user with db_owner access onto
both the target database and tempdb.

Any suggestions, anyone?

Mike.

Nov 18 '05 #2
"Sebastian" <po*****@mail.com> wrote in message news:<Oq**************@TK2MSFTNGP11.phx.gbl>...
add parameters to command object. Don't pass them like that.
I know it's not "best practice", but it was easier for me to do. I
still fail to see why this method shouldn't work. .NET only interfaces
to SQL - the operation of a stored proc should be independent of how I
pass parameters to it.

Has anyone else out there managed to successfully run stored procs
from .NET which create and drop tables?

As it is I've changed the proc so it parses the string and adds the
parameters dynamically (there are reasons for me doing it this way).
But I still get the same error. Any object created or altered in the
SP is not behaving - I still get the "Invalid object name
'#temptablename'." message.

I am running .NET 1.1, and using this to populate a custom datagrid by
adding a custom method to pass in the stored proc.

The proc call is MyCustomDataGrid.ExecuteStoredProc(storedprocstrin g,connectionstring);

In my custom control the code works along these lines:

public void ExecuteStoredProc(string pSPStr,string pCnStr)
{
string sSP;
// parse SP name
sSP = ...

SqlConnection oConn;
oConn = new SqlConnection(pCnStr);
oConn.Open();

SqlCommand oSqlCmd;
oSqlCmd = new SqlCommand(sSP,oConn);
oSqlCmd.CommmandType = CommandType.StoredProcedure;
AddParameters(ref oSqlCmd,pSPStr);

SqlDataAdapter oDA;
oDA = new SqlDataAdapter(oSqlCmd);

oDA.Fill(this.DataSet,"Results");
oConn.Close();
}

AddParameters(ref SqlCommand pSql,string pParams)
{
while(more params to process)
{
string ParamName=<parse from pParams>
string ParamValue=<parse from pParams>
SqlDbType ParamType=<parse from pParams/ParamValue>

pSql.Parameters.Add(ParamName,ParamType);
pSql.Parameters[ParamName].Direction = ParameterDirection.Input;
switch (ParamType) {
case SqlDbType.VarChar:
pSql.Parameters[ParamName].Value = sParamValue;
break;
case SqlDbType.Float:
pSql.Parameters[ParamName].Value =
Convert.ToDouble(sParamValue);
break;
...
etc
...
}
}
}

Irrespective of the clunkiness of this approach, the SP still works
fine provided I avoid creating/dropping tables, and fails when I
include them.

ARRGGHHH!!

Is this a .NET Bug, or am I doing something blatantly wrong?

Mike.
SqlCommand pCommand = new SqlCommand("usp_myproc", oConn);
pCommand.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add("@pNameList ", SqlDbType.Varchar, 6000).Value =
namelist.Text ;

Hope that help's.

"Mike Hutton" <hu***@yahoo.co.uk> wrote in message
news:e1**************************@posting.google.c om...
I have a rather odd problem.

I have a SP which uses temp. tables along the way, and then returns a
table of results:

CREATE PROCEDURE dbo.usp_myproc(
@pNameList VARCHAR(6000)
)
AS

...
CREATE TABLE #MyTable (
Id INT
, Nm VARCHAR(255)
)
...
SELECT * FROM #MyTable;

GO

I am calling this using a "text" style SqlCommand, as follows:

SqlCommand pCommand = new SqlCommand("usp_myproc '" + namelist.Text +
"'");
pCommand.CommandType = CommandType.Text;
SqlConnection oConn = new SqlConnection(myConnectionString);
oConn.Open();
pCommand.Connection = oConn;
SqlDataAdapter oDtAd = new SqlDataAdapter(pCommand);
oDtAd.SelectCommand = pCommand; //new SqlDataAdapter(pCommand);
MyDataSet.Tables.Clear();
oDtAd.Fill(MyDataSet,"Results");
oConn.Close();

The oddity is that I'm getting back an Invalid Object exception -
"Invalid object name '#MyTable'"

If I take out the SELECT * FROM #MyTable and replace it with a select
from another table, it works fine.

If I change the table to a permanent table, the problem returns.

So it seems that the issue is one of using tables which have been
created in the same procedure.

It seems that the run of the stored proc is ignoring any DDL commands.
I have a large number of SPs which use temporary tables et al, and
these need to be accessed via ASP.NET

HELP!

From a security POV I am using a named user with db_owner access onto
both the target database and tempdb.

Any suggestions, anyone?

Mike.

Nov 18 '05 #3

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

Similar topics

17
by: serge | last post by:
How can i delete all user stored procedures and all table triggers very fast in a single database? Thank you
1
by: Amit D.Shinde | last post by:
Hi Experts, i am writting a stored procedure in sql server 7. Its a simple stored procedure It is my first stored procedure. I want insert a record in table if the primary key field user id...
11
by: Bã§TãRÐ | last post by:
I have been working on this particular project for a little over 2 weeks now. This product contains between 700-900 stored procedures to handle just about all you can imagine within the product. I...
3
by: Eugene | last post by:
Hi, Is there any way to find all stored procedures that contain a given field Example: I want to find all stored procedures that work with the field ShipDate in tblOrder table Thanks, Eugene
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
0
by: Ryan | last post by:
PLEASE HELP!! I am trying to do what I think should be the simplest thing. In Visual Studio 2005, I set up a datagrid and then went ot configure a datasource for it. I made my connection to...
2
by: acw | last post by:
On a SQL Server 2000 db I would like to setup a stored procedure that accesses couple tables and runs the extended stored procedure xp..cmdshell. The goal is to grant users with limited privileges...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.