Parameter values in SSRS - reporting-services

I'm using SSRS to build report. Format of my query is as follows
declare #Date smalldatetime
select #Date = '20140421'
exec API_someAPI
#DateStart = #Date
,#DateEnd = #Date
select ...
where t1.Column < #Date
the problem is when enter this code in query editor, to form all necessary parameters, it asks for #DateStart and #DateEnd, but not for #Date. Shortly, it generates two parameteres, but I need #Date parameter, and it does not gebnerate it. I've tried to create it manually, and changed text as follows
..
select #Date = #Date
..
and created a new parameter, but query didn't work that way. How can I solve this problem?

Related

How do I assign DateTime field to a variable

I want to assign value of a query to a datetime variable in MySql Stored Procedure.
I'm trying this -
DECLARE myDate DATETIME;
SET myDate = (SELECT date1 FROM myTable WHERE Id = var_myId);
And this
DECLARE myDate DATETIME;
SELECT date1 into myDate FROM myTable WHERE Id = var_myId;
Both don't seem to work as I am not getting the desired result after running the proc.
EDIT
Problem is in this statement -
initial_Date DATETIME; -- param1
interval INTEGER; -- param2
SET var_date2 = DATE_ADD(initial_Date , INTERVAL interval MINUTE);
When I select var_date2 I get null as result.
I find two issue with your stored procedure.
First, you are naming a variable using an sql key word interval. You should rename with an word which is not an sql key word.
The other is you are not setting an out put variable. You can see the MySQL tutorials on stored procedure.
You can use and try the code below:
delimiter //
create procedure test(in initial_time datetime, in minuteInterval integer(2),
out final_time datetime)
begin
set final_time = date_add(initial_time, interval minuteInterval minute);
end//
delimiter ;
For testing you can try:
call test(now(), 60, #final_time);
select #final_time;
Here is the screenshot of my test with mysql 5.5.21:

Is it possible to set the select list dynamically in MSSQL server2008?

I have a requirement to populate the select list dynamically, Could you please suggest any solution?
Example:
DECLARE #DATE CHAR
SET #DATE='E.TERMINATION_DATE'
--SET #DATE='E.HIRE_DATE'
SELECT E.ID,E.FIRSTNAME,E.LASTNAME, #DATE FROM DBO.EMPLOYEES E
Is it possible to set the column name in select query?
For some query i need to set the variable #DATE as TERMINATION DATE and for some query i need to set #DATE as E.HIREDATE.
When i will run the query based on the TERMINATION_DATE then i will comment in the other option.
or do you have any other workaround?
You can use the Dynamic sql. But you should read this blog to know about it.
DECLARE #DATE VARCHAR(100),
#SQL VARCHAR(MAX)
IF YOURCONDITION
BEGIN
SET #DATE='E.TERMINATION_DATE'
END
ELSE
BEGIN
SET #DATE='E.HIRE_DATE'
END
SET #SQL = 'SELECT E.ID,E.FIRSTNAME,E.LASTNAME,' + #DATE +' FROM DBO.EMPLOYEES E'
exec sp_executesql #SQL
you can use variables in select query but you should assign that query to some variable and execute that variable
because in SSMS at first it will checks(run time) then it executes. In our scenario the variable is assigning at execution only
so we need to assign it to variable as #coderofcode told
DECLARE #DATE VARCHAR(100),
#SQL VARCHAR(MAX)
SET #DATE='E.TERMINATION_DATE'
SET #SQL = 'SELECT E.ID,E.FIRSTNAME,E.LASTNAME,' + #DATE +' FROM DBO.EMPLOYEES E'
exec(#sql)
I was writing a function for which i need to use execution_date for some case and for some other case i need to use hire_date.
So i think i can now populate this things dynamically and it's a great invention for me. Thanks all specially Giorgi, coder of code and koushik.
I am using this pseudo code here.
--Setting the condition parameters
DECLARE #HIRE_DATE DATETIME,
SET #HIRE_DATE= SELECT MIN(HIRE_DATE) FROM E.EMPLOYEES WHERE E.PERSON_ID>30000
--based on condition setting the variable dynamically.
IF #HIRE_DATE IS NULL
BEGIN
SET #DATE='E.TERMINATION_DATE'
END
ELSE
BEGIN
SET #DATE='E.HIRE_DATE'
END
--setting the sql to execute.
SET #SQL = 'SELECT COUNT(E.NR) AS ACTIVITIES,E.CLI_FIRSTNAME,E.CLI_LASTNAME,E.DEBTORNR,' + #DATE +' FROM DBO.EMPLOYEES E
GROUP BY E.CLI_FIRSTNAME,E.CLI_LASTNAME,E.DEBTORNR,' + #DATE
exec(#sql)

I am getting an error to define again the scalar variable even when i have defined it in SQL Server stored procedure

I have written the code as follows:
CREATE PROCEDURE [dbo].[TEST22]
#database_name VARCHAR(200),
/*Enter the Start Date and End date in the format MM/DD/YYYY*/
#Start_Date VARCHAR(200),
#End_Date VARCHAR(200)
AS
BEGIN
DECLARE #querystring VARCHAR(MAX)
--JOINS THE VENDOR MASTER FILE WITH THE PAYMENTS FILE TO IDENTIFY VENDORS WHO DO
-- NOT APPEAR IN THE PAYMENTS FILE FOR THE GIVEN PERIOD OF ANALYSIS
SET NOCOUNT ON;
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('dummy1'))
DROP TABLE dummy1
DECLARE #Test Nvarchar(50)
SET #test =N'"Test1"'
SET #querystring = 'select #test as Test1, getdate() as time1
into dbo.dummy1
from sys.tables'
EXEC (#querystring)
END
and when I run this code I am getting the following error:
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "#test".
Please suggest what the error is?
SET #querystring ='select #test as Test1, getdate() as time1
When this query is executing, it is executing as a standalone query, in which #test is not defined
EXEC (#querystring)
during this execution, the parameter #test must be defined.
In my opinion what you wish to achieve is
SET #querystring = 'select ' + #test + ' as Test1, getdate() as time1 ..... '
This concatenation will put the value of #test in the query and the new query will be
SELECT N'"Test1"' as Test1, getdate() as time1....
Glad to help! Please remember to accept the answer if you found it helpful.
As #Manish points out, #test isn't defined within the scope of your EXEC statement. However, a cleaner way to work with it than their proposal is to use sp_executesql:
SET #querystring = 'select #test as Test1, getdate() as time1
into dbo.dummy1
from sys.tables'
DECLARE #Parms nvarchar(max)
SET #Parms = N'#test nvarchar(50)'
EXEC sp_executesql #querystring,#Parms,#test
This keeps it as a separate parameter within the inner scope of the EXEC and so avoids any potential concerns about SQL Injection that arise whenever you concatenate SQL code and data.

Is it possible to set current date in stored procedure parameter?

I want to set GetDate() as default parameter value. But it shows error. Below is my block of code:
Create Proc XYS
(
#myDate datetime = GetDate() //error
)
AS
SET #myDate = GetDate() // i can do this but neither i want this nor I want to
pass current date from front end or upper layer
....
....
As far as i know functions/dynamic values are not supported as this level, instead we are allowed hardcoded values. Any workaround?
Would suggest using a value (e.g. 0 = minimum datetime) and then checking for it in your stored proc:
Create Proc XYS
(
#myDate datetime = 0)
AS
BEGIN
IF #myDate = 0
SET #myDate = GetDate()
// ...
END
I'm not saying it's the best idea in the world but it works for me:
CREATE VIEW dbo.V_GetDate
AS
SELECT GETDATE() AS TheDate
then
Create Proc XYS
AS
declare #myDate datetime
Select #myDate = TheDate from dbo.V_GetDate

Passing a Scalar reference into another Scalar

In SQL Server, I have a stored procedure that accepts a date parameter. That date paramater is then passed along to another stored procedure. However, the 2nd stored procedure parameter is named as the first parameter. and then I need to I have 2 different scalar variables:
DECLARE #date date, #sql varchar(100);
SET #date = '2012-07-01';
SELECT #sql = ('EXEC pStoredprocedure #date = '''+#date+'''')
I need the #sql scalar to contain this text so the stored procedure can call the other stored procedure:
EXEC pStoredprocedure #date = '2012-07-01'
But, when I run my code above, I get this error:
Msg 402, Level 16, State 1, Line 4
The data types varchar and date are incompatible in the add operator.
It's almost like I need to escape the #date operator. Can this be done?
Side note: Yes, I'm trying to dynamically pass along some variables. I understand the dangers of doing so, but doing it this way is much simpler...
The date variable is being used in string concatenation, so it should be treated as a string, either through its declaration, a convert function, or a cast. I tried this:
DECLARE #date varchar(20), #sql varchar(100);
SET #date = '2012-07-01';
SELECT #sql = ('EXEC pStoredprocedure #date = ''' + #date + '''')
PRINT #sql
and got this:
EXEC pStoredprocedure #date = '2012-07-01'
I am stuck on 2005, so I don't have the date datatype, but when I tried to use datetime, I got this error:
Msg 241, Level 16, State 1, Line 4 Conversion failed when converting
datetime from character string.
You can also try
DECLARE #date datetime, #sql varchar(100);
SET #date = '2012-07-01';
SELECT #sql = ('EXEC pStoredprocedure #date = ''' + CONVERT(varchar(20), #date, 102) + '''')
PRINT #SQL;
and get
EXEC pStoredprocedure #date = '2012.07.01'