How to stop showing messages from procedures in SQL Server? - sql-server

How to stop showing messages from procedure?
Currently, the problem is I have created a stored procedure Procedure A. It executes another procedure B from its code, and the B procedure executes yet other procedures C,D,E from its code.
The problem is when the A procedure executes, it shows 4 result sets like 0,1 or another values. But I want to show only the result from procedure A. How can I achieve this? I can't change the other procedures B,C,D,E because they also perform their individual tasks.
If you need clarification please ask.

You can try the followings:
if any of the sub procedures is returning only one row set and it is static (same columns with the same type are always returned), you can materialized the result set in temporary tables (or table variables)
For example, let's say that procedure X returns a table with two int columns. You
materialized the result like this:
CREATE TABLE #X
(
A INT
,B INT
);
INSERT INTO #X
EXEC usp_X;
Add additional parameter to the sub procedures or use any of the existing ones to not return the row set(s) if certain option is passed.
For example, add #MiscSettings parameter to your existing procedure X:
ALTER PROCEDURE usp_X AS
(
#Param01 INT
,#Parame02 VARCHAR(12)
,...
,#MiscSettings NVARCHAR(MAX) = NULL
)
The parameter is not mandatory, so you are not going to break any existing reference. Then in the procedure you can check if [DoNotReturnResultSet] string is passed in the #MiscSettings to not return the result sets. Existing references will continue to work because by default the row sets are returned.
IF Option Is Not Passed
BEGIN;
SELECT ...
END;
In both ways you can suffer if someone change the code of the sub routines. For example, if a type of returned column is changed, or someone add additional row set without checking if your special option is passed.
Note, in the second technique, if you do not like to add additional parameter, you can use some of the existing strings (for example). Just check if the string contains your option and then replace it.

Related

Function returning a results set and an integer in SQL Server

I'm facing a quite annoying barrier enforced by SQL Server and would like to check if there is an elegant solution for this.
I have a sequence of procedures' invocations (meaning, A calls B which calls C). The procedures are due to return different results sets, where (for instance) "A" generates its result using a set of records returned by "B".
Now, SQL Server does not allow to have nested INSERT INTO ... EXEC <stored procedure> so, to cope with this limitation, I converted the lowest procedure into a function that returns a table and hence INSERT INTO ... SELECT * FROM <function call>.
Now, there are situations in which the FUNCTION cannot return a result due to conditions of the data, and I would like the function to return a sort of code indicating the result of the execution (e.g. 0 would mean success, 1 would mean "missing input data").
Since SQL Server does not allow functions with OUTPUT parameters, I can't think of any elegant way of conveying these two outputs.
Can anyone suggest an elegant alternative?
there are situations in which the FUNCTION cannot return a result due
to conditions of the data, and I would like the function to return a
sort of code indicating the result of the execution
You really should use THROW to indicate the result of execution, which also precludes using a table-valued function.
So you need to use a stored procedure. To avoid the restriction on nested INSERT .. SELECT you can use temporary tables to pass data back to the calling procedure. EG
create or alter procedure foo
as
begin
if object_id('tempdb..#foo_results') is null
begin
print 'create table #foo_results(id int primary key, a int);';
THROW 51000, 'The results table #foo_results does not exist. Before calling this procedure create it. ', 1;
end
insert into #foo_results(id,a)
values (1,1);
end;
Can anyone suggest an ELEGANT alternative?
I'm not sure any of the alternatives is elegant.

Why does my Dataset display no fields in ReportServer, and can I get it to do so?

I opened a report I started in BIDS in MS SQL Server Report Builder 3.0, as I read an answer here on SO that said that was the easiest way to create a table containing all the values in a Dataset.
So I opened my .rdl file there, selected the Insert tab, then Table > Table Wizard, and the dataset from the "Choose an existing dataset in this report or a shared dataset" list.
When I select the "Next" button of the wizard, though, all lists are empty (Available fields, Column groups, Row groups, Values).
If I select "Next" again, I get, "The values field list must contain at least one field."
Those are auto-populated, though, and, as written above, are as empty as a politican's brain.
Is it because my dataset is a StoredProc, and returns data from a temp table? If so, is there a workaround?
Note: I also tried the Matrix > Matrix Wizard, with the same results.
UPDATE
Also and doubtless relatedly (no pun intended), when I try to run the report from within ReportBuilder, I see:
What a revoltin' development!
UPDATE 2
And when I return to BIDS to work on the project and try to add an Expression in a Matrix, in the Edit Expression dialog, on selecting the Dataset of interest, I get, " dataset has no fields."
Ay, caramba!
UPDATE 3
In response to lrb's answer: I don't know if my SP is really unparseable or not; it does return values from a temp table - Here is the end of it:
SELECT PLATYPUSDESCRIPTION, WEEK1USAGE, WEEK2USAGE, USAGEVARIANCE,
WEEK1PRICE, WEEK2PRICE, PRICEVARIANCE, PRICEVARIANCEPERCENTAGE
FROM #TEMPCOMBINED
ORDER BY PLATYPUSDESCRIPTION;
Could that (using a temp table) be the problem?
UPDATE 4
When adding an Expression to a textbox like so:
=Fields!PLATYPUSDESCRIPTION.Value
...I get the following fingerwag on the Preview tab:
The definition of the report 'bla' is invalid. The Value expression for the textbox 'textbox4' refers to the field 'PLATYPUSDESCRIPTION'. Report item expressions can only refer to fields within the current data set scope or, if inside an aggregate, the specified data set scope.
Surely there's a way to use results from temp tables in an SSRS report, no es cierto?
This will happen when the query or stored procedure can not be parsed with certainty. For example, if your data set is a store procedure that returns something like the following:
IF(#SomVariable=1)
SELECT 1,2,3,4
ELSE
SELECT 'A','B','C'
The above logic in a SP would be horrible, however, the field name and datatypes can not be determined. The same holds true in other edge case scenarios.
What you can do for a work around is to trick the parser by modifying your sp and offering up a clean return statement, then changing the sp back to its original form. Since the metadata is persistent until the next refresh, your values will hold. NOTE : If the problem occurs when returning temporary tables in your dataset see #4 below.
1. Modify your existing stored procedure
ALTER PROCEDURE MyProcedureThatDoesNotParse()
AS
BEGIN
/*COMMENT OUT CURRENT SP LOGIC
...
*/
SELECT
MyField1=1,
MyField2='String',
MyField3=0.01
END
2. IN SSRS Refresh the fields for your dataset.
NOTE : You will see MyField1,MyField2 and MyField3 in the fields list.
3. Revert the changes to your stored procedure.
4. For queries or SP's that return a local #temporary table, global ##temporary table or a table valued #variable, it seems that aliasing the temp structure works. I.E
SELECT * FROM #TABLE --Does not always parse in SSRS
SELECT * FROM #TABLE T --Seems to be able to be parsed by SSRS
Change command type on the report builder, choose " text " and write exec yourprocedurename. It will work

Execute sql stored procedure with array input from kettle table input step

I am having a sql stored procedure that takes 2 inputs and returns a resultset based on the given input.
I am using the input1 in sql IN clause like,
WHERE myCol IN(#input1)
when executing the store procedure from kettle table input step,
If i give a single value for the input1, it works fine.
EXEC sp_procedureName #input1='07423', #input2='2014-09-02'
If i give multiple values like below, it results empty resultset.
EXEC sp_procedureName #input1='07423,07022,07033', #input2='2014-09-02'
How can i pass multiple values as a parameter to my procedure.
the transformation will execute the procedure and insert the result set into another table using table output step.
What you need to do inside your stored procedure is split the comma delimited list of numbers into a list of single values in rows. Once you have this you can use WHERE myCol IN(SELECT v FROM #r), or use an INNER JOIN instead of IN. One point you might have to watch for is that you have leading zeros - if you want to keep those you will need to use strings instead of integers.
There are lots of articles around on the subject of splitting delimited values into rows, you could start here.

Adding a float default value in stored procedure but it says that it expect parameter

I am trying to input a tuple of room here is my code for insert
create proc spInsertToRoom
#room_name varchar(50),
#room_status varchar(50)='Available',
#room_rate float
AS
BEGIN
INSERT INTO tblRoom(room_name,room_status,room_rate)
SELECT #room_name,#room_status,#room_rate
END
I want the status to be automaticly available. Then when I input a value
it just keeps on saying
exec [spInsertToRoom]'A102',3500
Procedure or function 'spInsertToRoom' expects parameter '#room_rate',
which was not supplied.
But when I try this
exec [spInsertToRoom]'A103',#room_rate=4000
It worked!
I'm Just wondering why is it needed to to input the #room_rate=4000 whereas what I saw on youtube is a person just input a variable just like my former code?
In this case you need to name the parameters in your call. With your code what is happening is it will implicitly convert 3500 to varchar(50) and then not find a value for room_rate.
exec [spInsertToRoom] #room_name = 'A102', #room_rate = 3500
I would make a couple recommendations. First is not to use float when you want the number to be precise. Float is an approximate datatype. Something like NUMERIC(9, 2) would be a better choice.
My second recommendation is to drop the prefix from your names. It is just noise when looking for a procedure name and it isn't likely to get confused with something else. If that were my system the name for this proc would be Room_Insert. That way the procs will sort by the object they are dealing with and the verb is at the end. Room_Update, Room_Delete etc will all be next to other in the list of procedures when sorted alphabetically (like in SSMS).

How does Visual Studio dataset designer work when creating table adapter based on procedure

I need to update dataset table adapter in Visual Studio 2010. It is based on a stored procedure. The store procedure has parameters
#IDportfolio INT
, #Date varchar(50) = NULL
, #IDorder int = NULL
, #IDsession nvarchar(300) = NULL
, #User varchar(100) = NULL
, #UDNsXML NVARCHAR(MAX) = NULL
, #DEBUG INT = 0
The table adapter had this methods:
Fill,GetData (#IDportfolio, #Date, #IDorder, #IDsession, #User, #UDN)
So I needed to refresh it. In the procedure there is one IF statement that gets executed only if #DEBUG is set to 1. Inside this IF statement there are some SELECT statements used for internal debugging. Value 1 is obviously not default value as can be seen in the signature, yet designer, when refreshing the methods, acts like it is sending value 1 for #DEBUG parameter.
So instead of returning values that should be returned, it returns wrong set of values, and designer tries to make methods based on these returned values.
Why is designer working like that and is there some default value for INT parameters, or is he ignoring default values? I have noticed similar behavior in Entity Framework too.
OK well the (VS) designer needs to determine the sent values (parameters) and the returned values (columns and data types). (to generate associated classes). It uses what could loosely be called reflection. To do this the stored procedure must conform to certain rules, there are 4 i've discovered, off the top of my head, here are the important 3:
Only one row set can be returned (this is explicitly stated by MS somewhere)
The row set must be persistent outside the stored procedure i.e. it cant come from a table variable. (this is explicitly stated by MS somewhere)
It must be deterministic. (this is explicitly stated by MS somewhere)
Using a debug parameter, which the calling softwaer will never set to 1 will allow you to run the proc for testing and return various value at different points in the stored procedure. However for the VS designer, because these rows are not (by defualt) returned, the proc will still behave when analysed by VS.

Resources