"could not find stored procedure" - sql-server

I am maintaining a classic ASP website that has a SQL Server 2005 backend. For a small piece of new functionality I wrote a stored procedure to do an insert. This is the only user stored procedure in the database.
When I attempt to call the stored procedure from code I get the following error:
Microsoft OLE DB Provider for SQL Server error '80040e14'
Could not find stored procedure 'InsertGroup'.
/newGroup.asp, line 84
The DB uses SQL Server authentication. When I connect to the DB server in Visual Studio using the same user/pw as in the connection string the stored procedure is not visible but all tables are.
The user has datareader and datawriter roles and explicit execute permission on the stored procedure.
What am I missing?
UPDATE: My apologies, the server admin misinformed me that it was a 2000 server when it is actually a 2005 server (running on Windows Server 2003 x64).

Walk of shame:
The connection string was pointing at the live database. The error message was completely accurate - the stored procedure was only present in the dev DB. Thanks to all who provided excellent answers, and my apologies for wasting your time.

You may need to check who the actual owner of the stored procedure is. If it is a specific different user then that could be why you can't access it.

Sometimes this can also happen when you have a stored procedure being called with parameters. For example, if you type something like:
set #runProc = 'dbo.StoredProcedure'
exec #runProc
This will work, However:
set #runProc = 'dbo.StoredProcedure ''foods'''
exec #runProc
This will throw the error "could not find stored procedure dbo.StoredProcedure 'foods'", however this can easily be overcome with parantheses like so:
set #runProc = 'exec dbo.StoredProcedure ''foods'''
exec (#runProc)

make sure that your schema name is in the connection string?

There are 2 causes:
1- store procedure name
When you declare store procedure in code make sure you do not exec or execute keyword
for example:
C#
string sqlstr="sp_getAllcustomers";// right way to declare it.
string sqlstr="execute sp_getAllCustomers";//wrong way and you will get that error message.
From this code:
MSDBHelp.ExecuteNonQuery(sqlconexec, CommandType.StoredProcedure, sqlexec);
CommandType.StoreProcedure will look for only store procedure name and ExecuteNonQuery will execute the store procedure behind the scene.
2- connection string:
Another cause is the wrong connection string. Look inside the connection string and make sure you have the connection especially the database name and so on.

I had:
USE [wrong_place]
GO
before
DECLARE..

Could not find stored procedure?---- means when you get this.. our code like this
String sp="{call GetUnitReferenceMap}";
stmt=conn.prepareCall(sp);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
currencyMap.put(rs.getString(1).trim(), rs.getString(2).trim());
I have 4 DBs(sample1, sample2, sample3) But stmt will search location is master Default DB then we will get Exception.
we should provide DB name then problem resolves::
String sp="{call sample1..GetUnitReferenceMap}";

One more possibility to check. Listing here because it just happened to me and wasn't mentioned;-)
I had accidentally added a space character on the end of the name.
Many hours of trying things before I finally noticed it. It's always something simple after you figure it out.

I had the same problem. Eventually I found why. I used a code from web to test output of my procedure. At the end it had a call to Drop(procedure) so I deleted it myself.

If the error message only occurs locally, try opening the sql file and press the play button.

Related

Error in SP while translating from Oracle to Snowflake

I am translating procedure from Oracle to Snowflake and getting an error:
The procedure is:
https://github.com/santas-little-helper-13/sql/blob/main/sp.sql
I see that ":" is missed when using variables in a SQL statement or assigning the result of a SQL statement to a variable. Please check this part:
https://docs.snowflake.com/en/developer-guide/snowflake-scripting/variables.html#using-a-variable-in-a-sql-statement-binding
https://docs.snowflake.com/en/developer-guide/snowflake-scripting/variables.html#setting-variables-to-the-results-of-a-select-statement
This is a long SP so you may probably hit other errors. Good luck with the rest of the code.

SQL Server SQLCLR function receives parameter error after copying from one DB to another

I am trying to copy the send email assembly from one database to another. I clicked on script assembly as create to and created it in the new db.
When I try to send an email with the function in the newer db I get the following error:
The parameter 'fileName' cannot be an empty string. Parameter name: fileName
How can I copy the assembly across databases?
Some details:
Both DBs are on the same instance
Both DBs are owned by the same login
Using SQL Server 2016
Assembly is marked as UNSAFE in both DBs
Both DBs have TRUSTWORTHY enabled
T-SQL wrapper object is a scalar function / UDF
Function is being called the same way in both DBs
How can I copy the assembly across databases?
So far I am not seeing how this is a SQLCLR issue. You clearly copied the Assembly and the T-SQL wrapper object else you would be getting T-SQL errors instead of a .NET error.
I clicked on script assembly as create to and created it in the new db.
Given that you scripted out the T-SQL wrapper object and you are getting an error related to an input parameter, you might be running into a bug that causes defaults for NVARCHAR parameters to not script out correctly:
SSMS scripting CLR stored procedure NVARCHAR parameter NULL default as N'' (empty string)
Execute the following in both old and new DBs to make sure that all parameter definitions are the same, including any potential default values (paying close attention to rows that have a 1 for [has_default_value]):
SELECT [name], [user_type_id], [max_length], [is_output],
[has_default_value], [default_value]
FROM sys.parameters prm
WHERE prm.[object_id] = OBJECT_ID(N'dbo.ObjectName')
ORDER BY prm.[parameter_id];
If you find any differences, you will need to update your CREATE statement to include the correct default value(s). For example, if you have:
#SomeParam [nvarchar](1 - 4000) = N``
Then you will need to update that part of your T-SQL script to instead be:
#SomeParam [nvarchar](1 - 4000) = NULL
And then re-run the CREATE (you might need to either first DROP the existing T-SQL wrapper object, or change the CREATE to be ALTER).
Please vote for that Feedback bug report that I linked above. Thanks!
For more info on working with SQLCLR in general, please visit: SQLCLR Info

How to use print statement in sybase stored procedure when calling the stored procedure from application

I am using powerbuilder with Sybase ASE database. I am calling a stored procedure while doing an update from my application but I am getting an error in the stored procedure. I want to put some print statements in the stored procedure to check what the error is. How can I see the output of this print statement? How can I log the stored procedure?
Thanks in advance
Sharmistha
There is no log of these print statements and they are not considered part of the result set. They are in the same channel as the Sybase ASE error messages.
They should be returned to PowerBuilder, in the SQLCA, sqlerrortext is the field.
That's not how I used to debug stored procedures though. I found it easier to capture the parameters being passed to the stored procedure and use Sybase's ISQL command line tool, not the Java GUI, to see the error messages.
You can use function which will return the status code. Add "ON EXCEPTION RESUME" clause inside function and return global variable ##error to your application.
Use a custom error logging procedure to store data in a log table and read errors from it during or after execution.
Alternatively, you can try the PRINT statement inside the procedure to see if you can get the stdout output into PB.
Thank you all, I added some logs in powerbuilder instead to check for my problem.

SQL Server Profiler suddenly says Encrypted Text

Recently I noticed that a stored proc we are trying to profile failed to appear in the profiling output.
After adding in SP:StmtStarting and SP:StmtCompleted events, I noticed the TextData reported as
-- Encrypted text
.. but the stored procedure is not encrypted.
This has only recently started happening - we used to profile this SP perfectly fine, and I can't figure what has changed.
Any suggestions would be gratefully received.
UPDATE: The SP is definitely not encrypted. I've created new SP's on the box, and I see SP:BatchStarting event with the new SP's name. With the old SP, I don't see the BatchStarting event, but I do see the statements within the SP executing.
However I need to see the values of the parameters the SP is being called with, as they are table types. Originally I could see the table types being instantiated and populated before the SP is called.
So I figured this out in case anyone finds it useful.
I have table type parameters to this stored procedure. One of the parameters is passed a lot of data (i.e. a C# DataTable with >5000 rows). Without this quantity of data the stored proc profiled fine.
I guess there must be some cut-off at which point Profiler does not show all of the data being passed in.
Someone has altered the stored procedure and added the 'WITH ENCRYPTION' hint, which will cause this behavior. Alter the stored procedure and remove that hint and you'll start seeing the text of the proc again.
Also to note, if you don't have the original code, you will not be able to decrypt the text of the proc to issue the ALTER statement, so hopefully you have that handy.
Here's a decent run down of this option: Options for hiding SQL Server code
Moving the Trace Properties from the default of OnlySP(<your database here>)(user,default) to TSQL or TSQL_Replay unveiled the SQL being used for me, ... Go to File|Properties... and change the [Use the template:] drop-down combobox.

SQLServer cannot find my user defined function function in stored procedure

I must have some permissions wrong, but I can't figure out how. The following code is simplified but I can't even get this to work
CREATE FUNCTION ufTest
(
#myParm int
)
RETURNS int
AS
BEGIN
DECLARE #Result int
SELECT #Result = #myParm + 1
RETURN #Result
END
GO
Then I just want to be able to call the function from a stored procedure:
CREATE PROCEDURE dbo.[uspGetGroupProfileService]
#id int
AS
BEGIN
SET NOCOUNT ON;
DECLARE #otherId int;
SET #otherId = dbo.ufTest(#id);
END
SQLServer keeps telling me that it can't find dbo.ufTest. It shows up under [DB]\Programmability\Functions\Scalar-valued Functions but I can't figure out how to use it.
Anybody have any idea what I'm doing wrong?
EDIT
As indicated by the selected answer below, you can't always trust the SSMS Intellisense. One thing that you can try, other than just trying to execute the script, is forcing an Intellisense refresh with CTRL + SHIFT + R
https://blog.sqlauthority.com/2013/07/04/sql-server-how-to-refresh-ssms-intellisense-cache-to-update-schema-changes/
Works for me.
Try CREATE FUNCTION dbo.ufTest ...
I assume your default schema can't be dbo and it's ending up in a different schema. Otherwise the only explanation I can think of is you might need to grant permissions on it.
Script out the UDF and check the schema name. It's probably not dbo. I would change the UDF definition to specifically include dbo. In other words:
CREATE FUNCTION dbo.ufTest
Had the exact same problem and mine got fixed by simply restarting SQL Server Management Studio.
Just posting this in case anyone else did everything right and is still not able to call his function.
I just had an issue where this was the error and all of the advice on this column was failing as well.
Be sure double check your function declaration type and usage of that type.
I declared a return-type table and tried to call it with Select functionName() where I needed to use SELECT * FROM functionName()
As a last resort if any of the above and especially #jrdev22's answer did not help you (and left you stumped why), restart the SQL Server service in Configuration Manager since restarting the SSMS alone sometimes does not reset everything (e.g. similar to when creating a new login instance but not being able to login with it).
SQL Server Configuration Manager> SQL Server Services > SQL Server > Restart
Try calling it with a select instead of a set. And you checked that out belongs to the dbo schema?
It appears it might be a bug in the query editor. The Function appears in the tree in the right place but even naming the function dbo.xxxxxx the function doesn't appear in the query editor until you close and open a new session, then it appears if you type in dbo.
If you change the name of the function the old non existing fuction is avalable but not the new name. Refresh doesn't fix this only closing the session and starting a new one.
Why I say this might be a bug is that the permissions properties for Table function includeds a blue link to the schema properties but the Scalar functions it doesn't. So it may be a deeper lying bug in the way the schema is set up in the first place for which there may be a work around. Or maybe the schema in the database I am working on has not been set up correctly.
Hopefully someone else can shine some light on this issue.
If you are unable to find the function that you have just created there are two reasons for it.
you are using the wrong function name you need to add dbo.function name to get it.
I've also found one more issue like even though correct name is entered and also it is existing in the object explorer after refreshing you are unable to find it when you are trying to use the function.
In this case simply close the sql server and reopen it and you should be able to see the function.

Resources