Calling a stored procedure for a column during select - sql-server

I have a stored procedure named "OUTPUT_DATE" that takes in a date, and then outputs the date in the standardized format specified in another section of the database.
I am currently working on creating a BIRT report using this, however would like to call this stored procedure while getting the data to format a specific column of data. My question is, is it possible to call a stored procedure on my select statement, or is there another method to do this using BIRT?
I am already aware of the functions build into BIRT to output specific date formats, but this does not currently work the way I want since our date format is specified in the database.
My preference would be something similar to this...
SELECT col1, col2, OUTPUT_DATE(dateCol) FROM the_table

You can, but you should create a user defined function (scalar valued) instead of your stored procedure - functions can be called the way you want to.
Document & sample from Microsoft

Related

Can you use T-SQL statements in an SSRS dataset?

I have a script that iterates over a CTE and executes a custom sp_executesql query for each line. I need to use the resulting table in an SSRS report, but datasets don't seem to allow for declare, cursor and exec statements.
Is there any way to make T-SQL work in SSRS ?
Yes, I almost always use an entire script in my datasets. It is only the query designer that does not support them.
Simply write your script in SSMS or whatever then paste it into the query editor. Remember to comment out any variable declarations that will be passed in from the report as parameters.
If you double-click the dataset name and make sure the query type is Text then you can copy the script directly in.
Notes:
DO NOT DECLARE any variables that need to be passed in from your report as parameters. I generally just comment these out in the dataset query.
e.g. If you were passing in an Employee ID from you report parameter then your dataset would NOT need that declaring so it would look something like this
SELECT * FROM myTable WHERE EmpID = #EmpID
Obvisouly you would need this declaring when you test the script in SSMS
Make sure all references to #Variables are spelled with the same case. SSRS is case sensitive when it comes to variable names.
Only the first result set your script outputs will be 'seen' my SSRS so make sure you only have one SELECT statement that outputs the final data.
T-SQL works fine in SSRS datasets. I regularly declare variables, use temp tables and other T-SQL statements, and have used cursor and exec statements in the past.
The problem is likely in what you are returning. The "Query" (entire SQL statement) should return only one table which should have a stable structure. Returning a row per iteration of a cursor, for example, will not work. Put them in a temp table and then return all at once after the cursor execution. You can't return different column types or names for different executions, either.
Also, don't declare the variables that you use as parameters.

Can Table value parameters (TVP) be created inside and used in a single stored procedure?

I have a stored procedure that needs to accept multiple values in a stored procedure. After looking for ways to do this it looks like Table Value Parameters (TVP) are recommended in SQL Server 2008+. I am using SQL Server 2014.
What I am wondering is if TVPs can be created and used in the same stored procedure, or do the TVPs need to be created outside the stored procedure that they are used in? I would like to be able to enclose everything inside a single stored procedure if possible, so I could call something like this from code.
exec spMyProcedure "bob, Sam, Phil, Carol"
Ideally I would like to deploy the script via Code First in the entity framework seeding.
Table Value Parameters are just Table Value Variables passed as a parameter.
So yes -- you can declare a Table Value Variable in your SP and use it in the same way you would use a Table Value Parameter.
Just like you can pass a string as a parameter or declare a string as a variable you can do the same thing with Table variables.
HOWEVER
Your example does not make sense. Here you are passing a string to a stored procedure. If that procedure is going to make that string into a table you should pass a TVP instead. That is the whole point of saying they are "recommended". What is recommended is using them instead of passing a string.

Update field from ssrs

Is there a way to update a field in a table from value already chosen in ssrs ?
I am looking to update a field year after choose the value in ssrs.
I agree with Nathan. However, you might be able to build a stored procedure and pass it a parameter from the report. The stored procedure would have an update statement that accepts your parameter as an input.
For example:
UPDATE Table
SET column = #parameter
You'll need to add a parameter to the SSRS report. If you build a stored procedure that accepts parameters they will show up in your report.
Old post, but wanted to provide an alternate solution if anyone runs across this question. You could also place your update query within the stored procedure you're calling. For example, if I wanted to update a field called DateReported prior to running my stored procedure to gather records, I could place it at the beginning of the stored procedure (before the select statement), and it would then execute prior.
You can do it before report rendering. After that there is no way to call stored procedure. You can not add a custom event on the report to fire in case of a selection.

How can I call a stored procedure without table names in HQL?

I am trying to fetch the current timestamp through a stored procedure in HQL. This means my code looks something like the following:
var currentTimestamp =
session.CreateQuery("SELECT CURRENT_TIMESTAMP()")
.UniqueResult<DateTime>();
This doesn't work. Specifically, it throws a System.NullReferenceException deep inside of the NHibernate HqlParser.cs file. I played around with this a bit, and got the following to work instead:
var currentTimestamp =
session.CreateQuery("SELECT CURRENT_TIMESTAMP() FROM Contact")
.SetMaxResults(1)
.UniqueResult<DateTime>();
Now I have the data I want, but an HQL query I don't. I want the query to represent the question I'm asking -- like my original format.
An obvious question here is "Why are you using HQL?" I know I can easily do with this session.CreateSQLQuery(...), hitting our MySQL 5.1 database directly. This is simply an example of my core problem, with the root being that I'm using custom parameter-less HQL functions throughout my code base, and I want to have integration tests that run these HQL parameter-less functions in as much isolation as possible.
My hack also has some serious assumptions baked in. It will not return a result, for example, if there are no records in the Contact table, or if the Contact table ceases to exist.
The method to retrieve CURRENT_TIMESTAMP() (or any other database function) outside of the context of a database table varies from database to database - some allow it, some do not. Oracle, for example, does not allow a select without a table, so they provide a system table with a single row which is called DUAL.
I suspect that NHibernate is implementing in HQL primarily features which are common across all database implementations, and thus does not implement a table-less select.
I can suggest three approaches:
Create a view that hides the table-less select such as 'create view dtm as select current_timestamp() as datetime'
Follow the Oracle approach and create a utility table with a single row in it that you can use as the table in a select
Create a simple stored procedure which only executes 'select current_timestamp()'

Crystal Reports using multiple results from a Stored Procedure

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.

Resources