Showing posts with label proc. Show all posts
Showing posts with label proc. Show all posts

Friday, March 30, 2012

Problem running stored Procedure

Hi Guys & Gals

I'm having problems running a stored procedure, I'm getting an error that I don't understand. My procedure is this:


ALTER PROC sp_get_allowed_growers
@.GrowerList varchar(500)
AS
BEGIN
SET NOCOUNT ON

DECLARE @.SQL varchar(600)

SET @.SQL =
'SELECT nu_code, nu_description, nu_master
FROM nursery WHERE nu_master IN (' + @.GrowerList + ') ORDER BY nu_code ASC'

EXEC(@.SQL)
END
GO

and the code I'm using to execute the procedure is this:


public DataSet GetGrowers(string Username)
{
System.Text.StringBuilder UserRoles = new System.Text.StringBuilder();
UsersDB ps = new UsersDB();
SqlDataReader dr = ps.GetRolesByUser(Username);
while(dr.Read())
{
UserRoles.Append(dr["RoleName"]+",");
}
UserRoles.Remove(UserRoles.Length-1,1);
//Create instance of Connection and Command objects
SqlConnection transloadConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionStringTARPS"]);
SqlDataAdapter transloadCommand = new SqlDataAdapter("sp_get_allowed_growers",transloadConnection);
//Create and fill the DataSet
SqlParameter paramList = new SqlParameter("@.GrowerList",SqlDbType.VarChar);
paramList.Value = UserRoles.ToString();
transloadCommand.SelectCommand.Parameters.Add(paramList);
DataSet dsGrowers = new DataSet();
transloadCommand.Fill(dsGrowers);
return dsGrowers;

}

The UserRoles stringbuilder has an appropriate value when it is passed to the stored procedure. When I run the stored procedure in query analyser it runs just fine. However, when I step through the code above, I get the following error:


Line 1: Incorrect syntax near 'sp_get_allowed_growers'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'sp_get_allowed_growers'.

Anyone with any ideas would be very helpful...Try surrounding your parameter list (just the one parameter) in your stored procedure in parentheses. Also, I've never used Alter proc, I use Create Procedure (not to say that Alter proc doesn't work, I don't know - just an observation)|||CREATE PROCEDURE dbo.sp_get_allowed_growers
(
@.GrowerList varchar(500)
)
AS

I always use dbo.sp"NAME" because when you push to production your account may or may not be the dbo.|||Although, technically, you should never preface your stored procedure names with sp_. sp_ is used by sql server to designate system stored procedures. When you preface your own sprocs with sp_, you cause sql server to first search through all system sprocs, then through the local sprocs to find your procedure. In other words, you're adding in a bit of inefficiency to each and every procedure call.|||Thanks for the suggestions guys, I tried that and I still get the same error. It's a bit of an odd one because the stored procedure seems fine. I compiled it in query analyser and I can run it in query analyser by right-clicking and selecting "Open", I'm then prompted for the paramter and it returns the expected result so, as far as I know, it's functioning correctly. When I step through the code calling the procedure, the Autos window shows the value of the parameter as "'Admins','NH'" which is exactly what I'd expect.

I'm pretty sure it's something to do with the way that the value of the parameter is being assigned. In query analyser, when I run the stored proc and I am prompted for the value of the @.GrowerList parameter, I have to type the values in exactly like this 'Admins','NH' and it works.

The odd thing is that the database is raising the error and saying that there's a syntax error in the stored proc when there definitely isn't. It would be different if it was raising a type conversion error but it isn't.

Keep thinking folks, I'll send a small prize to whoever can help me crack it (don't get too excited, it will probably be a company pen or something!)

Cheers,|||Add

transloadCommand.SelectCommand.CommandType=CommandType.StoredProcedure;

before you call .Fill()

And I would also concur that naming an sp with an sp_ prefix will slow things down...|||Thanks doug, that cracked it straight away, I'm very grateful. If you email me your address to imacleverbloke@.mcowan.info I'll pop the freebies in the post. Don't get too excited but they're worth having!

As for the sp_ naming convention, I hear about that performance issue too late and I'm kinda stuck with it until the next comprehensive overhaul.

Cheers guys.

Problem running Proc from Job

I have a stored procedure that runs fine using the Query Analyzer:

exec sp_ProcessRecords

However, when I create a job to run the stored proc once an hour, the job fails, with the following message:

Executed as user: sa. String or binary data would be truncated. [SQLSTATE 22001] (Error 8152) The statement has been terminated. [SQLSTATE 01000] (Error 3621). The step failed.

I don't think it's a permission problem, since the job runs as sa.

I don't understand why it would work if I run it manually, but not when it runs as a job.

Any help would be greatly appreciated.Check your parameters that are being passed to the stored procedure. This means that some parameter that is being passed it too long for the datatype and will be truncated.|||Originally posted by rnealejr
Check your parameters that are being passed to the stored procedure. This means that some parameter that is being passed it too long for the datatype and will be truncated.

Unfortunately, the stored proc called from the job does not take any parameters.

The odd thing is that the stored proc will work fine if it is run manually from the Query Analyzer. The error only occurs when the proc is run from the job, using the exact syntax!

I'm at a loss...|||Can you post the stored proc code - or describe what it is doing ? If you are doing inserts/updates then the same problem can occur.sql

Wednesday, March 21, 2012

Problem populating table with a cursor from VB GUI

Hi all
I have a store proc that that does various bits of data manipulation, but
the last part I have used a cursor to update 2 fields in a table. This sp i
s
called by a VB front end and then the contents of the table in question
displayed.
The problem is, if I run the sp directly (within VB's Server Explorer) these
2 fields are always fully populated. If however I call the sp from within
code in a GUI, after each call (which should fully populate the table based
on certain criteria) the 2 fields become less populated. What I mean by thi
s
is on the 1st call the 2 fields are 100% populated, with each subsequent cal
l
only populating maybe 50% of records, then 20%, until no records are
populated.
Would anyone have any ideas where I may look.
thanks"Chubbly Geezer" <ChubblyGeezer@.discussions.microsoft.com> wrote in message
news:1BE8A023-7631-49AA-85BC-E5FD5808EF00@.microsoft.com...
> Hi all
> I have a store proc that that does various bits of data manipulation, but
> the last part I have used a cursor to update 2 fields in a table. This sp
is
> called by a VB front end and then the contents of the table in question
> displayed.
> The problem is, if I run the sp directly (within VB's Server Explorer)
these
> 2 fields are always fully populated. If however I call the sp from within
> code in a GUI, after each call (which should fully populate the table
based
> on certain criteria) the 2 fields become less populated. What I mean by
this
> is on the 1st call the 2 fields are 100% populated, with each subsequent
call
> only populating maybe 50% of records, then 20%, until no records are
> populated.
> Would anyone have any ideas where I may look.
> thanks
If you post the code and any necessary DDL, we can give you a lot better
answer.
From the limited information you have provided, I would guess that your VB6
application may be timing out. Then again, without any idea of what is
really going on in the sproc, it's hard to say.
Rick Sawtell
MCT, MCSD, MCDBA

Problem passing parameter into remote stored proc

I'm having a problem passing a parameter value into a stored procedure
that I am running on a remote (linked) server, and am receiving a DTC
error because of it.
I have a stored procedure that brings in a variable (@.CustID int). I
later pass that parameter to another stored procedure. The code looks
like this...
EXEC LinkedServer.dbname.dbo.spname @.CustID
When I run that, I get this error...
Server: Msg 7391, Level 16, State 1, Procedure spname, Line 394
The operation could not be performed because the OLE DB provider
'SQLOLEDB' was unable to begin a distributed transaction.
OLE DB error trace [OLE/DB Provider 'SQLOLEDB'
ITransactionJoin::JoinTransaction returned 0x8004d00a].
[OLE/DB provider returned message: New transaction cannot enlist in the
specified transaction coordinator. ]
However, if I hard-code the parameter, it works:
EXEC LinkedServer.dbname.dbo.spname 1234 -- this works.
I can even do this:
DECLARE @.var int
SET @.var = 1234
EXEC LinkedServer.dbname.dbo.spname @.var -- this works too.
But if I accept the variable as an input parameter to my stored
procedure, I get the error listed above.
Any ideas?
Thanks in advance for your help...
Zev Steinhardtzev_steinhardt
what happen if you assign the parameter to a variable?
DECLARE @.var int
SET @.var = @.CustID
EXEC LinkedServer.dbname.dbo.spname @.var
...
AMB
"zev_steinhardt" wrote:

> I'm having a problem passing a parameter value into a stored procedure
> that I am running on a remote (linked) server, and am receiving a DTC
> error because of it.
> I have a stored procedure that brings in a variable (@.CustID int). I
> later pass that parameter to another stored procedure. The code looks
> like this...
> EXEC LinkedServer.dbname.dbo.spname @.CustID
> When I run that, I get this error...
> Server: Msg 7391, Level 16, State 1, Procedure spname, Line 394
> The operation could not be performed because the OLE DB provider
> 'SQLOLEDB' was unable to begin a distributed transaction.
> OLE DB error trace [OLE/DB Provider 'SQLOLEDB'
> ITransactionJoin::JoinTransaction returned 0x8004d00a].
> [OLE/DB provider returned message: New transaction cannot enlist in the
> specified transaction coordinator. ]
> However, if I hard-code the parameter, it works:
> EXEC LinkedServer.dbname.dbo.spname 1234 -- this works.
> I can even do this:
> DECLARE @.var int
> SET @.var = 1234
> EXEC LinkedServer.dbname.dbo.spname @.var -- this works too.
> But if I accept the variable as an input parameter to my stored
> procedure, I get the error listed above.
> Any ideas?
> Thanks in advance for your help...
> Zev Steinhardt
>|||Thanks for the reply, Alejandro.
I tried that. It didn't work.
I even tried to trick it into thinking that it's another variable
altogether. I put the variable into a temp table, declared a new
variable, populated it with the value from the temp table and passed it
in. That didn't work either.
Zev Steinhardt|||zev_steinhardt,
Are you executing the remote sp inside a transaction?
AMB
"zev_steinhardt" wrote:

> Thanks for the reply, Alejandro.
> I tried that. It didn't work.
> I even tried to trick it into thinking that it's another variable
> altogether. I put the variable into a temp table, declared a new
> variable, populated it with the value from the temp table and passed it
> in. That didn't work either.
> Zev Steinhardt
>|||Yes. The remote sp is within a transaction.
Zev|||zev_steinhardt,
you are using a distributed one, correct?
begin distributed transaction
exec ...
AMB
"zev_steinhardt" wrote:

> Yes. The remote sp is within a transaction.
> Zev
>|||Alejandro...
Yes, it is a distributed transaction... and I have XACT_ABORT on
Zev|||zev_steinhardt,
When you execute the remote sp using:
DECLARE @.var int
SET @.var = 1234
EXEC LinkedServer.dbname.dbo.spname @.var -- this works too.
then you are not executing it using a distributed transaction, that is why
you do not get the error.
See if this helps.
You receive error 7391 when you run a distributed transaction against a
linked server
http://support.microsoft.com/kb/329332/en-us
AMB
"zev_steinhardt" wrote:

> Alejandro...
> Yes, it is a distributed transaction... and I have XACT_ABORT on
> Zev
>

Monday, February 20, 2012

Problem inserting into a Text field in a temp table after SP3

I have a stored proc that is inserting data into a Text field. This was
working fine on Friday, but today throws the following error:
ODBC: Msg 0, Level 19, State 1
SqlDumpExceptionHandler: Process 55 generated fatal exception c0000005
EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
Yesterday we upgraded our SQL server from SP2 to SP3, and today we upgraded
3 to 3a after finding this problem. We found the KB article 817359 which
seems to be related, but if we change their example from nText & nVarchar to
Text and Varchar we still get the error (the same one as above). Can anyone
tell me how to fix this problem?
TIA
Ron LounsburyRon,
You should contact PSS to report this. An AV normally means a bug in
sqlserver. You should not be charged for this incident.
--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net
"Ron Lounsbury" <rlounsbury AT Progeny DOT net> wrote in message
news:uwvRfPrsDHA.1788@.tk2msftngp13.phx.gbl...
> I have a stored proc that is inserting data into a Text field. This was
> working fine on Friday, but today throws the following error:
> ODBC: Msg 0, Level 19, State 1
> SqlDumpExceptionHandler: Process 55 generated fatal exception c0000005
> EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
> Yesterday we upgraded our SQL server from SP2 to SP3, and today we
upgraded
> 3 to 3a after finding this problem. We found the KB article 817359 which
> seems to be related, but if we change their example from nText & nVarchar
to
> Text and Varchar we still get the error (the same one as above). Can
anyone
> tell me how to fix this problem?
> TIA
> Ron Lounsbury
>|||OJ
Thanks for the response. It was a bug that they have a fix for, but you
have to call them to get it.
Ron Lounsbury
"oj" <nospam_ojngo@.home.com> wrote in message
news:eJjF$jrsDHA.4056@.TK2MSFTNGP11.phx.gbl...
> Ron,
> You should contact PSS to report this. An AV normally means a bug in
> sqlserver. You should not be charged for this incident.
> --
> -oj
> RAC v2.2 & QALite!
> http://www.rac4sql.net
>
> "Ron Lounsbury" <rlounsbury AT Progeny DOT net> wrote in message
> news:uwvRfPrsDHA.1788@.tk2msftngp13.phx.gbl...
> > I have a stored proc that is inserting data into a Text field. This was
> > working fine on Friday, but today throws the following error:
> >
> > ODBC: Msg 0, Level 19, State 1
> > SqlDumpExceptionHandler: Process 55 generated fatal exception c0000005
> > EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.
> >
> > Yesterday we upgraded our SQL server from SP2 to SP3, and today we
> upgraded
> > 3 to 3a after finding this problem. We found the KB article 817359
which
> > seems to be related, but if we change their example from nText &
nVarchar
> to
> > Text and Varchar we still get the error (the same one as above). Can
> anyone
> > tell me how to fix this problem?
> >
> > TIA
> > Ron Lounsbury
> >
> >
>

Problem inserting decimal value into SQL Server 2000

In my VS 2005 windows control I am inserting a record into a table using a proc.

One of the fields "Accuracy" should look like this 66.4, but when I isnert it from the proc itlooks like 66.0. If I bypass my proc and use an inser statement from SQL quey analyzer it look like it should 66.4.

What am I doing wrong in my proc...

CREATE PROCEDURE [dbo].[insMyLameProc]

@.PlayerName nvarchar(255),
@.Score int,
@.Rounds int,
@.Accuracy decimal,
@.CorrectPicks int,
@.IncorrectPicks int

AS

--Insert the new game score
--===============================================================================================
insert into wmTurnTileScores
(PlayerName, Score, Rounds, Accuracy, CorrectPicks, IncorrectPicks)

values (@.PlayerName, @.Score, @.Rounds, @.Accuracy, @.CorrectPicks, @.IncorrectPicks)
--===============================================================================================
GO

@.Accuracy decimal(18,2)

--The 2 gives it 2 decimal places.

|||

rpack79:

@.Accuracy decimal(18,2)

--The 2 gives it 2 decimal places.

This is right, but as a tip: don't use the 18 if you are sure you will not need it. (faster query + save space).

Good luck.

|||

Yup, that worked, thanks

Problem inserting data into SQL Server 2005 DB from WebForm controls

Hello all,

I am having problems running a stored proc from an aspx.cs file. Basically I want to extract data from webForm controls and create a new record in a DB table. I have watched the "Getting Started" videos on thi site, and the only difference between my code and the code of the demonstrator is the connection string.

My conn string:-dataSrc.ConnectionString =ConfigurationManager.ConnectionStrings["ProjectTblConnString"].ConnectionString;
Demonstrator conn string:-dataSrc.ConnectionString =ConfigurationManager.ConnectionStrings("ProjectTblConnString");
When I debug with the string as shown by the demonstrator the error list reports that ConnectionStrings is a property and I am trying to use it as a mothod,
which is fair enough, but I am just confused as how it worked for the demonstrator and not myself. When I debug with the code I used above I get no error, but nothing is inserted in the DB.
Also, it looks as if the only class in my aspx.cs file that is actually being executed is PageLoad() - the remainder of the code seems to be ignored. Below is the entired aspx.cs file:-
 
1using System;2using System.Data;3using System.Configuration;4using System.Collections;5using System.Web;6using System.Web.Security;7using System.Web.UI;8using System.Web.UI.WebControls;9using System.Web.UI.WebControls.WebParts;10using System.Web.UI.HtmlControls;1112public partialclass CreateProject : System.Web.UI.Page13{14protected void Page_Load(object sender, EventArgs e)15 {1617if (User.Identity.IsAuthenticated ==false)18 {19 Server.Transfer("Default.aspx");20 }2122if (chkbox_startDate.Checked ==true)23 {24 txtbox_startDate.ReadOnly =true;25 txtbox_startDate.Text = (System.DateTime.Now.ToString());26 }2728 }2930protected void btn_create_Click(object sender, EventArgs e)31 {32//connection string for connecting to SQL Server DB33 SqlDataSource dataSrc =new SqlDataSource();34 dataSrc.ConnectionString = ConfigurationManager.ConnectionStrings["ProjectTblConnString"].ConnectionString;353637//insert data in table using the CreateNewProject stored proc in the DB38 dataSrc.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;39 dataSrc.InsertCommand ="CreateNewProject";40 dataSrc.InsertParameters.Add("Project_Title", txtbox_title.ToString() );41 dataSrc.InsertParameters.Add("Description", txtbox_desc.ToString() );42 dataSrc.InsertParameters.Add("Start_Date", txtbox_startDate.ToString() );43 dataSrc.InsertParameters.Add("Primary_Dev_Lang", list_devLang.ToString() );44 dataSrc.InsertParameters.Add("Dev_Environment", list_devEnv.ToString() );45 dataSrc.InsertParameters.Add("No_Junior_Devs", txtbox_juniorDevs.ToString() );46 dataSrc.InsertParameters.Add("No_Senior_Devs", txtbox_seniorDevs.ToString() );4748int rowsAffected = 0;49try50 {51 rowsAffected = dataSrc.Insert();52 }53catch (Exception ex)54 {55 Server.Transfer(Error.aspx);56 }57finally58 {59 dataSrc =null;60 }6162if (rowsAffected != 1)63 {64 label_confirm.Text ="Error, Contact system admin";65 }66else67 {68 label_confirm.Text ="Success";69 }7071 }//end btn_create7273 //if cancel is clicked set all values back to default74protected void btn_cancel_Click(object sender, EventArgs e)75 {76 txtbox_title.Text ="";77 txtbox_desc.Text ="";78 txtbox_startDate.Text ="";79 txtbox_seniorDevs.Text ="";80 txtbox_juniorDevs.Text ="";81 list_devEnv.SelectedIndex = 0;82 list_devLang.SelectedIndex = 0;83 chkbox_startDate.Checked =true;84 label_confirm.Text ="";85 }8687//if checked the current dateTime is inserted into txtbox88protected void chkbox_startDate_CheckedChanged(object sender, EventArgs e)89 {90if (chkbox_startDate.Checked ==true)91 {92 txtbox_startDate.ReadOnly =true;93 txtbox_startDate.Text = (System.DateTime.Now.ToString());94 }95else96 {97 txtbox_startDate.ReadOnly =false;98 txtbox_startDate.Text ="";99 }100 }//end chkox101102103}//end class

Any help anyome can give is greatly appreciated. This is part of my Final Year College Dissertation which is due in 3wks !!!!

Sláinte á chaire,

Seán

Hi,

ConnectionStrings is a property name that returns a collection of connection strings. to get the items in the collection, you will need to use [] instead of () in C#. In VB, () is OK. The is the syntax.

Please check the EnableViewState property for each of the textboxes on your page, to see if they have been set to True. If false, the data in it will get lost when postback.

Also, since you're using a SqlDataSource, if CreateNewProject is a stored procedure, the parameter names should contain a @. prefix. Please check in your database to see if the CreateNewProject stored procedure has been created properly. Also use, such as @.Project_Title as parameter name.

HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!

|||

Thanks for the reply Kevin.

As you can probably tell I am a newbie.

I have corrected all the problems you have mentioned, but it looks as if my "Submit" button is not executing any of the code at runtime. Any ideas on this?

problem inserting data in table - all rows are not returned

The procedure is inserting data into this table :
create table temp_XML
(xml_data varchar(8000))
I ran the trace and proc is running fine. But all the data is not
returned when I do select * from temp_xml.
I did some research and limited data by using a where clause.
The proc ran fine in some cases and returns all data and does not runs
fine in some where clauses. What do I need to do to return all the
rows.
But the proc runs fine in production without any limiting where clause
I have different procs like this and some run fine in development and
not in production and vice versa. The procs have successfully ran in
the development before also.
any sugesstions on how to fix this.VJ wrote:
> The procedure is inserting data into this table :
> create table temp_XML
> (xml_data varchar(8000))
> I ran the trace and proc is running fine. But all the data is not
> returned when I do select * from temp_xml.
> I did some research and limited data by using a where clause.
> The proc ran fine in some cases and returns all data and does not runs
> fine in some where clauses. What do I need to do to return all the
> rows.
> But the proc runs fine in production without any limiting where clause
> I have different procs like this and some run fine in development and
> not in production and vice versa. The procs have successfully ran in
> the development before also.
> any sugesstions on how to fix this.
Check if there is a faulty trigger on the table, especially one that
can't handle multiple row inserts. Also, are you sure you are capturing
any error message that the proc might be generating?
If you need more help, please post your code if you can.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||There are no triggers in the proc.
The proc is running fine in the other database. Something is limiting
the rows inserted in the development database now, but it was fine a
few minutes ago.
Please let me know your email address and I will send you the proc.
David Portas wrote:
> VJ wrote:
> Check if there is a faulty trigger on the table, especially one that
> can't handle multiple row inserts. Also, are you sure you are capturing
> any error message that the proc might be generating?
> If you need more help, please post your code if you can.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --