ASP Classic - TSQL function call failiing - sql-server

I'm working on a quick ASP Classic form that calls a TSQL proc. The first one that I created works great since it doesn't send any values in. Now I'm working on my second one and it looks a bit like this:
exec update_allocation(#Anum='164360',#mTeam='5',#Team='9',#Perc='14',#Bill=140000,#Mons=164360)
Also tried as:
exec update_allocation('164360','5','9','14',140000,164360)
First one gives me an error of:
Microsoft OLE DB Provider for SQL Server error '80040e14' Incorrect
syntax near '#Anum'.
The second gives me:
Microsoft OLE DB Provider for SQL Server error '80040e14' Incorrect
syntax near '164360'.
I'm not sure what to make of these errors. The issue must be the parameters, but not sure how they should be sent in.

The comments are correct but here is a bit of explanation as to why that is the case.
Both errors;
Microsoft OLE DB Provider for SQL Server error '80040e14' Incorrect syntax near '#Anum'.
and
Microsoft OLE DB Provider for SQL Server error '80040e14' Incorrect syntax near '164360'.
are actually quite descriptive and pinpoint where the issue is. Usually in a situation like this an error is highlighting a problem just before the word or phrase in an error description in these cases the #Anum parameter and the 164360 value. If we follow this through the previous character in both cases is an open bracket (.
Unlike functions in T-SQL, Stored Procedures do not require brackets around their parameters and doing so will raise an error (like the ones above) to correct this remove the brackets from both statements.
With named parameters;
EXEC update_allocation #Anum='164360', #mTeam='5', #Team='9', #Perc='14', #Bill=140000, #Mons=164360
Without named parameters;
EXEC update_allocation '164360', '5' ,'9' ,'14' ,140000 ,164360
Either method is acceptable syntax but personally I find it more useful to use the named parameter approach as that allows you to pick and choose what parameters to pass and even exclude ones that have defaults. You can still do this with the nameless approach but ordinal position of the values is more important.
#sean-lange makes a valid point here about SQL Injection vulnerability, consider using the ADODB.Command object to execute stored procedures rather then just calling the Execute() method on a ADODB.Connection object.
Here is an example of calling Stored Procedures using Classic ASP and the ADODB.Command object.
Answer to Using Stored Procedure in Classical ASP .. execute and get results
Answer to How to use ASP variables in SQL statement

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.

MS Access/OleDB: No value given for one or more required parameter <- How to debug?

This has to be by far the worst error message I've ever seen in any context. Coming from SQL Server and now revamping a legacy Access database, this error just keeps coming again and again and it's a complete trial and error every time, being a huge time waster.
Any helpful tips on how to debug this error effectively? Like giving even the slightest hint on where to look? I'm using C#.
Well, if using say sql server, one would fire up SQL studio, and try the sql that way.
And the same approach works for Access. Take your sql (debug.print it out), and then cut + paste the sql into the access query builder (in sql view mode). If you run the query, then for any missing field, it will spit out a prompt with the "name" of the column.
Access uses (when from Access) a prompt system for any column that is not in the table, and automatic prompts the user for the value. If you use odbc, or oleDB, then missing columns spit out that missing parameter (but without the name of the column as you note).
So, most easy is to fire up access and use the sql view in the query builder - paste in your sql.
You can use ADO to create the query, and then list all parameters the query expects:
ADODB.Connection conn = new ADODB.Connection();
conn.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Path\\To\\db.accdb");
ADODB.Command cmd = new ADODB.Command
{
CommandText = "SELECT * FROM tblUsers WHERE username = bob",
//Oops, forgot to quote the username, results in No value given for parameter error
ActiveConnection = conn
};
foreach(ADODB.Parameter param in cmd.Parameters)
{
Console.WriteLine(param.Name); //bob
}
Console.ReadLine();
This requires a reference to ADO, which can be entered through the COM references.
You could also use late binding to prevent the extra reference, which might be desirable if you only use this as debug code but want it in your project, see this Q&A.
Since OLEDB doesn't support named parameters, you unfortunately can't use OLEDB for this afaik.
Of course, you can rewrite this to a function that takes a query string and returns expected parameters as a string, and then use that function in the immediate window when debugging.

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

Declaring XML output parameters with ADODB in ASP Classic

I'm working with an ASP Classic legacy code base attempting to call an existing SQL Server stored procedure that declares output parameters with an XML data type.
However, every time I try to execute the stored proc I get this error:
0x80040e14 - Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC SQL Server Driver][SQL Server]Implicit conversion from data type xml to varchar is not allowed. Use the CONVERT function to run this query.
I've experimented with a list of ADODB types, trying to find one that works, and haven't had any luck. I've also grepped our legacy code base for examples of other stored procs with xml output parameters being called, but wasn't able to find any.
Is there a way around this without updating the stored procedure to not use an XML type for its output parameter? For example, could it be possible to declare a null output parameter type or just ignore this parameter?
Thanks for any suggestions.

"could not find stored procedure"

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.

Resources