all.
I am using openquery to call remote stored procedure at the linked server.
If the procedure's work was successed - the result set contains a,b,c columns.
If there was inner error, it returns only error column in result set.
As I know, to select from openquery results - we need to call it with 'WITH RESULT SETS', but I dont know what result set from that two will be returned.
How can I correctly call the procedure through openquery and handle the error from returning column in error case?
Thank you.
With regards, Yuriy.
You may be able to use a TRY/CATCH, TRY one ResultSet, CATCH the other;
https://msdn.microsoft.com/en-us/library/ms179296.aspx
Related
I have a select statement that's working fine but I need one more value that comes from a stored procedure and I can't call it as a value in the select statement
select
recp.percent,
(call libraryEXT.get_rate_for_detail(detail_number))
from
mainTable.orders ORD
inner join
mainTable.receipts recp on ORD.rcptID = recp.rcptID
It works fine to only get recp.percent, but putting the next value in for calling the stored procedure says it doesn't expect "call"
If I run the stored procedure by itself it returns one record with the columns: name, rcptID, time and I need the rcptID from that
How can I properly call the stored procedure and make the value returned be my other value in the select statment?
As #mao said, this is not possible to call an SP in a select statement.
If you are the SP owner, try creating a table-function instead. This one can be called in a select statement.
Given the way you've used CALL libraryEXT.get_rate_for_detail(detail_number) it's apparently only returning a single value.
That being the case, you'd be better served by having it as a scaler User Defined Function (UDF).
Then this is what your code would look like:
select
recp.percent,
libraryEXT.get_rate_for_detail_UDF(detail_number) as rate
from
mainTable.orders ORD
inner join
mainTable.receipts recp on ORD.rcptID = recp.rcptID
I renamed the function just to be clear it's NOT the stored proc. If you created the stored proc, you could delete it and use the same name for the UDF.
I would like to be able to execute an explain statement on a query that has bind parameters. For example:
EXPLAIN SELECT * FROM metasyntax WHERE id = $1;
When I attempt to execute this, I get the following error:
ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1
I understand it's telling me that it wants me to supply a value for the query. However, I may not necessarily know the answer to that. In other SQL dialects such as Oracle, it will generate an explain plan without needing me to supply a value for the parameter.
Is it possible to get an explain plan without binding to an actual value? Thanks!
Assuming the parameter is an integer:
PREPARE x(integer) AS SELECT * FROM metasyntax WHERE id = $1;
Then run the following six times, where “42” is a representative value:
EXPLAIN (ANALYZE, BUFFERS) EXECUTE x(42);
Normally PostgreSQL will switch to a generic plan for the sixth run, and you will see a plan that contains the placeholder $1.
No.
The optimiser is allowed to change the query plan based on the parameter. Imagine if the parameter was null - it's obvious that no rows will be returned, so the DB may return an empty rowset instantly.
Just use a representative value.
I'm using PostgreSQL database.
I've a my plpgsql FUNCTION that returns a single record with an arbitrary number of columns.
Due to this arbitrariness I would need to use something like:
SELECT * FROM my_function(97)
But this doesn't work as Postgres gives me the following error:
a column definition list is required for functions returning "record"
But if I do:
SELECT my_function(97)
I can see the expected result but encapsulated in a single column.
Is there a way to fetch the expected result as a set of columns as intended by the function and not a single column encapsulating all of them?
When a function just RETURNS record or SETOF record (and no OUT parameters to go with it), PostgreSQL does not know the names and types of its elements and you are required to provide a column definition list with every call.
Avoid that if at all possible and return a well known (row) type instead. There are a several ways to declare the return type. See:
PostgreSQL: ERROR: 42601: a column definition list is required for functions returning "record"
Refactor a PL/pgSQL function to return the output of various SELECT queries
There are quite a few related questions on SO. Try a search!
When using a set returning function (setof) in the select list, on the left hand side of the FROM, the function returns a composite type. Using a function in the select list can be hard to avoid when using a table as input to a function.
A way to SELECT items from a single column of composite type follows:
SELECT
(my_function).field1,
(my_function).field2,
(my_function).field3
FROM
(SELECT my_function(*)
FROM sometable) t
You have a few options here:
Return a REFCURSOR and fetch from that cursor in the application. Note you can actually return multiple REFCURSORS if you need to return multiple result sets.
Return an XML document and parse it in the application.
Use a bunch of OUT variables, return RECORD, and determine which of these to select from
The basic problem is that the actual return results need to be known at planning time so you can't just return an arbitrary number of columns. The planner needs to know what is going to be returned.
In order to return a "set of columns" you will have define a return type as TABLE or SETOF in which case you actually return a SET of records which you should be able to SELECT from.
For more information about functions returning SETOF take a look at this link to documentation
I'm not certain that I follow what you're after, but does this work?
SELECT (my_function(97)).my_column
I have the command:
INSERT INTO tbl_media
(DateAdded) VALUES (GetDate())
SELECT CAST(##Identity AS int)
It works fine against a standard sql db but not against a CE db I get the following error:
SQL Execution Error.
Executed SQL statement...
Error Source: SQL Server Compact ADO.NET Data Provider
Error Message: There was an error parsing the query. [Token line number = 2, Token line offset = 31, Token in error = )]
Shame the error isn't more useful anyone know what could be going on?
Cheers
UPDATE::::::
After much messing around with visual studio editor (rubbish) I downloaded dataport and read the MSDN. It seems there are 2 problems...
1) SELECT CAST(##Identity AS int) is not valid sql
SELECT ##Identity is
2) SqlCe server does not like it when I put these two commands together:
INSERT INTO tbl_media
(DateAdded) VALUES (getdate())
SELECT ##Identity
If I do the insert and select at different times then it works. So how do I get round this? I cant do it at different times I need to know the ID of the objects as I create them!!!
UPDATE 2:
According to the very helpful Erik E you cant do 2 statements at the same time. So the following parses as correct but wont work:
INSERT INTO tbl_media
(DateAdded) VALUES (getdate());
SELECT ##Identity;
So what I really want to know is how do I guarantee that identities wont get mixed up when adding records?
I.e. what if someone creates a record while someone is getting the identity for one they have just inserted?
You have an extra ) I dont know if that will fix your error but look at VALUES you have
VALUES(GETDATE())) '<-- one ) extra.
Change it to this:
INSERT INTO tbl_media(DateAdded)
VALUES (GetDate())
SELECT CAST(##Identity AS int)
You can only run a single SQL statement per ExecuteNonQuery call. So you must spilt in 2 calls.
I'm new to this board. I have been driving myself crazy trying to find the answer to my problem.
I created some TSQL code that executes some dynamic SQL in a cursor within a stored proc. The cursor fetches some data from table x, builds a query based data retrieved in table x, runs the query, and returns data. Works like a charm.
When I add an 'insert into table' to capture the results I get an error: NOTE: only errors with SQL 2000 runs great on SQL 2008.
The operation could not be performed because the OLE DB provider 'MSDAORA' was unable to begin a distributed transaction.
OLE DB error trace [OLE/DB Provider 'MSDAORA' ITransactionJoin::JoinTransaction returned 0x8004d01b].
You should not be using a cursor for this. My guess would be a comflict with the cursor and the insert into table.
Please post code and the problem you are tyring to solve so that we can help you write it correctly.