Crystal Reports using multiple results from a Stored Procedure - sql-server

I have a stored proc in sql-server and one of the parameters it returns is a string with the query parameters. I display those query parameters at the top of the report. That works great if something is found, not so great if nothing was found.
We have tried returning two query results, one the data set that I will make the report from (which includes the query parameters), the other the query parameter string. Crystal appears to only see the first data set, and this very old discussion (http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=42462) says that is not something that will work. But that was over 5 years ago, and I'm hoping things have changed.
The problem, is if nothing is returned, the report is so blank that the person doesn't even know what query parameters they used. If they could see that they queried something that doesn't return any results, that would be useful.
So, if I have at the end of my stored proc:
SELECT * FROM [#ResultSet]
select #SearchCriteria as SearchCriteria
I'd like to be able to display the SearchCriteria even if there is nothing in the #ResultSet. Can it be done with this version of Crystal? Is there another way to do this?

Unless as stated by the first answer the results of one procedure have the same number of columns of another procedure (this includes type), if this is the case you can UNION the results or UNION ALL the results (if you want duplicates) to get ONE resultant set.
If the types or columns are not the same then you cannot do this. The only other option you can do is to merge all the relevant data into a temp table and then return the results from that temp table (SELECT * FROM #temp)

How are you currently able to display the parameters when results are found?
You haven't mentioned how you are using the Crystal Report in your environment.
Typically, I've done criteria display by passing the parameters to the Crystal Report as Report Parameters, and then using them in fields. This assumes you are calling it from a client application in some way.
Another option is to load the results into client datatables and binding to that as a datasource, it's certainly possible to handle the multiple result sets that way.

Related

Take data from different datasets and inserting them into a SQL table from SSRS report

I have a SSRS report with 20 different datasets with some calculated columns in each.
I want to take few fields from all data sets including some calculated columns and insert them into a SQL table.
I want to do this for each month so that I can see the trends during a period. Is there any way to do that with out editing the data sets?
Can I refer the fields that I need by referring to Textbox4 and insert them into a SQL table? What is the easy way to do without touching data sets?
There is most likely alot better solution than using SSRS to update a SQL database. I am not proposing this as the best solution but rather a way to achieve what was asked.
You could create a dataset that runs a stored procedure you can pass the data as parameters to. The Sproc would do the insert into your chosen table and you can pass in the parameters from your original dataset however you see fit. You could even setup a second report with the Stored procedure Dataset that you can call on command by having an action event on an item to call the report. (I had a subreport embedded in a column of a tablix configured so it would only update with values from that row for instance).
To clarify:
Create a subreport that accepts the data you want to insert as a parameter for each column
Instead of adding a normal Dataset, have it call a stored procedure that inserts as you require
Add the subreport to your main report to be called once its run and configure the required parameters to be passed through.
There will be better, more efficient, cleaner ways to do this, but I found the above to work for my purposes since I was limited by time and resources. But I would still recommend you seek other solutions if possible.

SSRS truncating parameter

I have an SSRS report that populates a parameter with a stored procedure. This query works as expected. When the parameter is used in running the report, the parameter is being truncated. I choose value ABCD, but the report returns values for ABC. The stored procedure I am passing the parameter to runs perfectly in SSMS and returns ABCD data. When I test the query in the query designer or I run the report, I get ABC data. How do I get SSRS to pass in the entire parameter?
Parameters are strings without a set length. There's nothing to truncate your values. Have you checked the values to make sure that the Value and Label are the same?
The label is what you see (ABCD) while the value is what is actually passed in the parameter. I don't know if that would be your problem if it works on your local machine though.
If that doesn't work you can try deleting the parameter and recreating it - it shouldn't work but has before.
I had something like this happen to me before, try going into the stored proc you have and creating an output table for your parameters results to go into that is constant, i.e.
declare #mytable table (returnval varchar(50))
make sure you make one column in the table for the results of your query you will return and make sure you make the data type a varchar with enough pace to hold any possible return selection values. Note: do not use nvarchar as I find this still sometimes truncates the values and cuts things off.
you will want to execute the query the same way for the proc you originally did but this time inserting the values into your temp table i.e.
insert into #mytable
select * from table.name
this will insert all values into your temp table, also this table now has a set value length for all return values that will not change, you could try and do this the same way with the original query by using a set length field for the table the problem is there are many factors that exist that can change this, here are a few examples.
If you have 2 select statements with a union to get results from both that you want to use for final results then each field may have a set length and data type that differs from the other, sql server will give the best data type in the results but when it sends the data over to ssrs it can interpret it differently as another length value unless set.
You may have multiple data types you are using in the return field and ssrs is getting confused which to use i.e. interger values, varchar, text, etc. this sort of reinforces the one above.
Also another possibility is it could be happening on the SSRS side of things and not the proc, but by doing this method you eliminate out one possible cause which is the stored proc.
Also, check in the configuration settings of your report on the back end and make sure that the return value is set from the query as it should be but also make sure that there is no setting specified for a return data type, this sometimes happens if at report design you create the report first and use a static parameter for testing to get the report created first and then you specify later the parameter is a list from a query result set which I have had happen as well.
Finally one good practice each time you make a change to something is to ensure that the report is being generated each time and not showing a cached version of the report which makes it look like nothing changes on the report each time. the way to do this is to close the report each time or when you run the report also try and make sure you hit the reload on the page after you run the report to force a reload to make sure you see the differences each time.
I think if you do all of this you will either find the issue or eliminate it or both as I have so many times.

SQL Server - compare the results of two stored procedures that output multiple tables

So, similar to "SQL Server compare results of two queries that should be identical", I need to compare the output of two stored procedures to ensure the new version is generating equivalent output to the old version. The tricky part is that my SP outputs six tables of differing widths.
I started writing a hybrid version of them that would compare each of the tables individually, but it's a pretty complex SP, so I was hoping there was an easier way.
I tried using EXCEPT as in the linked question, but it looks like that will only compare one table to one other table.
Easy option 1: Output the stored procedure results to a text file (one per procedure version) and use a diff tool/editor to make sure they are the same.
Easy option 2: Write the stored procedure results to a table/temp table (per return table per procedure) and write sql to compare the results. Just count the rows in each result table and then do a count of the union (not union all) of both tables. Repeat for each result table.
You can capture multiple result sets in .NET (C# or VB) quite easily. You can create a DataAdapter and DataSet, and use the DataAdapter.Fill() method to populate the DataSet. Each result set will be stored as a DataTable within that DataSet. Then you just need to loop through the DataTables collection in each DataSet and compare them. You can find more info on this MSDN page: Populating a DataSet from a DataAdapter
This can be done in either SQLCLR if you want to run it as a stored procedure or user-defined function, OR it can be a stand-alone console application. Running it as a SQLCLR stored procedure is quite convenient, but given that you will be stored all results for all 6 result sets, and for both stored procedures that you are testing, that might require too much memory. In that case, the console app is the the way to go.
The only thing I can think of is add an additional parameter to your both of (New/old) stored procedures to handle which result it should return like.
Exec usp_proc #var1 , #var2 , #ResultSet = 1
The above execution should return the first result set and if you pass #ResultSet = 2 it should return second result set and so on.....
do this with both stored procedure and then compare the result sets group by group (using except will do the trick).

SSRS Subreport not seing values in only one dataset

I'm working on a series of reports which need to be shown in two languages so I've created a common header that I planned on embedding into the content of each report. I have two data sets, one that looks up a set of strings in English or French (depending on a parameter passed to the report), and another with numerical data.
The original header worked fine and when I extracted it into a separate report it also runs fine. I am just passing the name of the report to the subreport. When I run the main report, it seems that the values in my string dataset are all null (I've tried all of them) yet if I try data from my numerical dataset, the fields have values and get displayed as the title.
If I also insert a text field with the same expression to display the report title after the subreport, it works fine but the title still does not get passed to the subreport.
I cannot make any sense of why this would be happening, anyone have a clue?
I'm using Report Builder 2.0 and SQL Server 2008.
Thanks!
Alright, so I worked around this issue by simply passing my parameters on down to the subreports and letting them call the stored procs again. Not ideal since I now have to call that one stored proc 3 times but it probably won't be noticed.
I tried several other work arounds and received errors like rsAggregateInCalculatedFieldExpression, so my best guess is that this was caused by attempting to use an aggregate function (First) where it is not allowed but the error was never shown.

Traversing multiple CSV in SQL

I have a SQL Server 2008 database. This database has a stored procedure that will update several records. The ids of these records are stored in a parameter that is passed in via a comma-delimited string. The property values associated with each of these ids are passed in via two other comma-delimited strings. It is assumed that the length (in terms of tokens) and the orders of the values are correct. For instance, the three strings may look like this:
Param1='1,2,3,4,5'
Param2='Bill,Jill,Phil,Will,Jack'
Param3='Smith,Li,Wong,Jos,Dee'
My challenge is, I'm not sure what the best way to actually go about parsing these three CSVs and updating the corresponding records are. I have access to a procedure named ConvertCSVtoTable, which converts a CSV to a temp table of records. So Param1 would return
1
2
3
4
5
after the procedure was called. I thought about a cursor, but then it seems to get really messy.
Can someone tell me/show me, what the best way to address this problem is?
I'd give some thought to reworking the inputs to your procedure. Since you're running SQL 2008, my first choice would be to use a table-valued parameter. My second choice would be to pass the parameters as XML. Your current method, as you already know, is a real headache and is more error prone.
You can use bulk load to insert values to tmp table after that PIVOT them and insert to proper one.

Resources