Passing a date parameter from Excel to SQL Server stored procedure - sql-server

Looking for some help with this as me and my 'Mentor' are having some issues with resolving the problem
I have a stored procedure in SQL Server 2016 that works fine when dates are entered. However I need an Excel sheet to execute the stored procedure, which currently is also working fine.
The issue is when I'm trying to use a parameter with Excel to pass through the dates to the SQL query/stored procedure that I need to bring back. I'm struggling to link the two up to each other
The second answer here by mono código > how to pass parameters to query in SQL (Excel) - should work for me, however when I get to the part which explains 'The code in the next section assumes that you already have a parameter in your query (Connection Properties->Definition->Command Text) in the form “WHERE (DB_TABLE_NAME.Field_Name = ‘Default Query Parameter')” (including the parentheses). Clearly “DB_TABLE_NAME.Field_Name” and “Default Query Parameter” will need to be different in your code'
I'm unsure if this is due I just simply do not know how to write this part, or it's to do with how I'm trying to link up the cells within excel to the parameters within SQL
For some more context, the parameters are dates - to and from, which will be in an excel sheet, which will then feed through to a query, which is where the execute code for the stored procedure is.
If any help or guides or walkthrough's could be given I'd very much appreciate them!
Thanks,
Wil-Liam

Related

Running Sql server Stored Procedure with excel parameter and without using VBA

I would like to excecute an sql server stored procedure with a date parameter from excel, the date parameter is an input in a cell of the spreadsheet.
Is it possible to do this without using VBA ?
My goal is to get dynamic results every time when i set a new date value in the excel file and the examples that i found are using vba when it is about using the dynamic parameters from the excel file.
Please help if there is a way to do that without vba.
Thanks in advance
Yes.
Start by using Microsoft query to set up a connection to your database, and as the desired number of parameters... Parameters can be added by putting conditions on the query and entering [] in the value box.
Finish creating the connection then edit the connection, and change the SQL to:
Dbo.procedurename ?
The question mark is placeholder for parameters. Link the parameter to cell, and set it to auto update on cell change and your good to go.

Suppressing output from a stored procedure in SQL Server 2008 R2 and newer

I am currently working with a stored procedure that performs some background processes and then returns one of two results tables.
If it works ok I get a one column table that says success, if it doesn't then I get a four column table with various error data.
While this is fine if you just execute the code from .net, I now need to execute this from within another stored procedure. While I don't need the output, I do need the background processes to take place. I'd usually insert the output into a table, but can't in this case as the columns in the output varies dependent on the result, and as such cannot define a table that it can insert into.
Easiest answer would be to rewrite the outputs of the background SP to be consistent but this isn't an option. I've even tried wrapping this inside a UDF but the stored procedure can't be called from with a function.
Whatever solution I finally use it must work on versions from SQL Server 2008 R2 up to 2016.
Does anybody have any suggestions?
Many thanks,
Mat.
I would image you could create a SP that inserts the result of the inner SP into a temporary table using the hack below.
Insert results of a stored procedure into a temporary table
If that blocks the ouput then you can return no data.

SSRS 2014 issue with dataset after deployment

My report is running without a sweat on my dev environment. I was a happy man.
When deployed on another server, I have the following issue (see below) (less happy now )
The error is:
The variable name '#choix_de_l_adresse' has already been declared.
Variable names must be unique within a query batch or stored
procedure.
I've checked the parameters for the dataset adresse1. Nothing substantial came out (see image below).
I thought it may be related to a case issue with my sql (see below) but nothing striking jumps off
DECLARE #choix_de_l_adresse VARCHAR(38)
SELECT JohnJack.Siren,
CASE WHEN JohnJack.Adresse1_Eco IS NULL
THEN '' WHEN JohnJack.Adresse1_Post IS NULL
THEN '' WHEN (JohnJack.Adresse1_Eco IS NOT NULL OR JohnJack.Adresse1_Post IS NOT NULL)
AND #choix_de_l_adresse = 'Adresse Postale'
THEN Adresse1_Post
ELSE Adresse1_Eco END AS adresse1
FROM JohnJack
The only workaround I've been able to find out is to delete the dataset and to recreate it from scratch.
Update: When I'm using ssrs query builder to run the query batch, it is running fine
Do you think using a stored procedure would help? I read somewhere that text was not a good fit and when possible, a stored procedure would be better.
Have you seen the following issue and were you be able to fix it without deleting and recreating your dataset? If the answer is yes, how did you do it?
I managed to solve my issue.
I created a stored procedure, put my sql code inside and ... voilà ! all the rows I was looking for, were retrieved

Crystal Reports parameter sending NULL instead of chosen value

I have a CR 2008 report reading from SQL Server 2008 R2 that used to contain 3 parameters and worked like a champ. The business requested the addition of a fourth parameter. I added the variable to the stored procedure, tested and got the results I was looking for. I then created the parameter as a static list of values as we do not need all values available. I went to the record select expert and lined up my parameter with the appropriate database field. Ran the report, got an error on an incorrect amount of parameters. Refreshed the database; the report ran but returned no results. Checked my parameter values against the stored procedure. Once again, the stored procedure returned results. Went back to Crystal and viewed the query. Lo and behold, the query is sending in NULL for the new parameter even though a specific value was chosen on the prompt. I even went so far as view a tutorial on adding parameters to be sure I did it correctly. Anyone have any thoughts on why the parameter isn't being read properly?
I think you need to right click on Database fields from Field Explorer > Database expert > see this picture
that will surely work coz sometimes crystal reports stored procedure need to be reset or refresh to reflect any changes on the SQl code

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