Parameter as a result - sql-server-2008

I am looking for a way to use a parameter as a result that can be plugged in to another select statement later down the line.
This is the CTE select statement that I am able to pull by manually changing all four of the dates listed to what I want
SELECT CurrentYearEmp.PRCo,
CurrentYearEmp.Employee,
CASE
WHEN CurrentYearEmp.PREndDate <> '2016-11-20'
THEN '2016-11-20 00:00:00'
WHEN CurrentYearEmp.PREndDate = '2016-11-20'
THEN '2016-11-20 00:00:00'
END AS 'ParameterPREndDate'
I am able to replace the first half of the WHEN statement with a parameter like this
SELECT CurrentYearEmp.PRCo,
CurrentYearEmp.Employee,
CASE
WHEN CurrentYearEmp.PREndDate <> #PREndDate
THEN '2016-11-20 00:00:00'
WHEN CurrentYearEmp.PREndDate = #PREndDate
THEN '2016-11-20 00:00:00'
END AS 'ParameterPREndDate'
But it will only produce the results I am looking for if the parameter is 2016-11-20. I want to be able to have #PREndDate as '2017-02-14' it will post 2017-02-14 as a result of the select statement.

You can use a Table Valued Function as HABO has already said. Here is a simple example for one:
/*
--Create some dummy data in a db
CREATE TABLE SomeData
(
DataId INT IDENTITY
, Val VARCHAR(8)
, Dt DATE
)
INSERT INTO dbo.SomeData (Val, Dt) VALUES ('A', '2017-1-1'),('B', '2017-1-2'),('C', '2017-1-3'),('D', '2017-1-4'),('E', '2017-1-5')
--Create a table valued function
CREATE FUNCTION ReturnData (#StartDt DATE, #EndDt Date)
RETURNS TABLE
AS
Return
Select *
From SomeData
WHERE DT between #StartDt and #EndDt
*/
Select *
From ReturnData('1-1-2017', '1-3-2017')
The best thing IMHO about Table Functions is they can join to existing things as they are well formed objects in the database. When you do Procedures and Dynamic SQL, you get a result set but that does not mean your code can be joined. Table Functions can be cross applied to run off of cte's and they can be joined to other tables. So essentially they are better for reuse but they have rules around them so you cannot do more advanced things like dynamic sql in them(as far as I know unless they changed it recently) and other things. But if you want a reusable data set with rules governing input, that is pretty much exactly what they are made for.

Related

SQL Query - Select a value, then use it again in following statements

I've tried looking it up, and while I think this should be possible I can't seem to find the answer I need anywhere.
I need to lookup a date from one table, then store it for use in a following query.
Below is statements that should work, with my setting the variable (which I know won't work, but I'm unsure the best way to do/show it otherwise - bar maybe querying it twice inside the if statement.)
I'm then wanting to in the latter statement, use either the date given in the second query, or if the date from the first query (that I'm thinking to set as a variable) is newer, use that instead.
startDateVariable = (SELECT `userID`, `startDate`
FROM `empDetails`
WHERE `userID` = 1);
SELECT `userID`, SUM(`weeksGROSS`) AS yearGROSS
FROM `PAYSLIP`
WHERE `date` <= "2021-11-15"
AND `date` >= IF( "2020-11-15" > startDateVariable , "2020-11-15" , startDateVariable )
AND `userID` IN ( 1 )
GROUP BY `userID`
Naturally all dates given in the query ("2021-11-15" etc) would be inserted dynamically in the prepared statement.
Now while I've set the userID IN to just query 1, it'd be ideal if I can lookup multiple users this way at once, though I can accept that I may need to make an individual query per user doing it this way.
Much appreciated!
So turns I was going about this the wrong way, looks like the best way to do this or something similar is by using SQL JOIN
This allows you to query the tables as if they are one.
I also realised rather then using an IF, i could simply make sure i was looking up newer or equal to both the date given and the start date.
Below is working as required. And allows lookup of multiple users at once as wanted.
SELECT PAYSLIP.userID, employeeDetails.startDate, SUM(PAYSLIP.weeksGROSS) AS yearGROSS
FROM PAYSLIP
INNER JOIN employeeDetails ON employeeDetails.userID=PAYSLIP.userID
WHERE PAYSLIP.date <= "2021-11-15"
AND PAYSLIP.date >= "2020-11-15"
AND PAYSLIP.date >= employeeDetails.startDate
AND PAYSLIP.userID IN ( 1,2,8 )
GROUP BY PAYSLIP.userID
See here for more usage examples: https://www.w3schools.com/sql/sql_join.asp
However along the lines of my particular question, it's possible to store variables. I.E.
SET #myvar= 'Example showing how to declare variable';
Then use it in the SQL statement by using
#myvar where you want the variable to go.

Slow sql statement when using variables

I have the following SQL statement running against a MariaDB 10.1.26 with ~2.000 rows with instant results.
select value, datetime from Schuppen
where (value = (select min(value) from Schuppen where (measure = 'temp')
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00'))
and datetime between '2018-11-01 00:00:00' and '2018-11-02 00:00:00';
When I use the following statement with variables for the datetime fields, the execution takes ~5.5 seconds.
set #startdate = cast('2018-11-01 00:00:00' as datetime);
set #enddate = cast('2018-11-02 00:00:00' as datetime);
select value, datetime from Schuppen
where (value = (select min(value) from Schuppen where (measure = 'temp')
and datetime between #startdate and #enddate))
and datetime between #startdate and #enddate;
The more data rows I have, the longer it takes to execute the statement. Seems like the variables change the behaviour of the statement somehow.
What's wrong here?
I use MySQL Workbench and #variables are very useful to query/search different tables for a given attribute. I ran into a similar issue. After scouring through different threads and trying different things, it worked well when I set the #variable to be of exactly the same type and same encoding as the column in the table(s) that I am searching for that variable.
For example:
SET #keyword = CONVERT(CAST("KEYWORD" AS CHAR(8)) USING ASCII);
In this case, the search column cname in my table customer is of type CHAR(8) and encoded using ASCII:
SELECT * FROM CUSTOMERS WHERE cname=#keyword;
If you have multiple tables to query, where cname is CHAR(10) in one and CHAR(8) in another, then you can do the following:
SET #keyword = "KEYWORD";
SELECT * FROM CUSTOMERS WHERE cname=CONVERT(CAST(#keyword AS CHAR(8)) USING ASCII);
SELECT * FROM EMPLOYEES WHERE cname=CONVERT(CAST(#keyword AS CHAR(10)) USING ASCII);
The problem is that the query optimizer does a bad job on finding a suitable index when using variables. This is a known issue.
If you use EXPLAIN on both queries, you will see the difference. Just try to avoid variables when not necessary.
For the first query, the optimizer "sees" the chosen values and decides an index can be perfectly used to satisfy the selected range more efficiently.
For the second query, the optimizer is unaware of the two values that define the range, and decides to fall back to a FULL SCAN instead.

How to retrieve a parameter value based on a result set from stored procedure in SSRS 2014?

I would like to know how do I select a parameter based on the result set from a stored procedure.
My code looks something like this:
CREATE PROCEDURE usp_UserInformation (#ReportRunDate DATE = NULL)
AS
WITH CTE AS (
SELECT -- some fields
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID
WHERE RegisteredDate = DATEADD(MONTH, DATEDIFF(MONTH, -1, #ReportRunDate) - 2, -1)
--I'll output the result set after applying all the logic like
SELECT UserID, UserName, DOJ
FROM CTE
Now, when the user selects the date on the report, I created another parameter and trying to retrieve records from the "Available Values" using 'Get values from a query'. I would like to show the list of UserIDs from the above query result set and allow the user SELECT based on that.
UPDATE: I have created another stored procedure as there is no other alternative. Now my question is, how do I pass the second stored procedure value to the first one in WHERE clause?
CREATE PROCEDURE usp_UserInformation (#ReportRunDate DATE = NULL)
AS
WITH CTE AS (
SELECT -- some fields
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID
WHERE RegisteredDate = DATEADD(MONTH, DATEDIFF(MONTH, -1, #ReportRunDate) - 2, -1)
--I'll output the result set after applying all the logic like
SELECT User_ID, UserName, DOJ
FROM CTE
WHERE User_ID = EXEC usp_Get_UserID
I am looking for something like this where the user can select the ID first and any date of his choice in the report.
PS: I am not allowed to write in-line SQL and only SPs are allowed. I don't want to create two SPs for the same report.
Use this stored procedure as the source of available values for the UserID parameter, and make sure that the UserID parameter comes AFTER the ReportDate parameter. Then the UserID available values will not be populated until after the user chooses ReportDate. Then this procedure will run using the user-selected ReportDate.
Also I think you need to remove the #User_ID parameter from your DataSet parameters (the last screenshot). Your stored procedure only has the one ReportRunDate parameter. The parameters in your SSRS dataset must match the parameters in the stored proc you are calling.
Regarding this error:
The server did not provide a meaningful reply; this might be caused by
a contract mismatch, a premature session shutdown or an internal
server error.
It sounds like your stored procedure code was changed after you last edited your SSRS report, and now it doesn't return the expected values. Use a profiler trace to see what command is sent from SSRS to your SQL Server, then execute that command in SSMS and look at the results. What are the column names being returned? Are they the same names in the Fields tab of your dataset? Are they the same names as your Value Field and Label Field in your report parameter?
I've ended up using a stand alone SQL with advice from many experts to get the User_ID. I've added another parameter to the stored procedure and passing the second value to the parameter from the standalone SQL.
CREATE PROCEDURE usp_UserInformation (#ReportRunDate DATE = NULL, #User_ID VARCHAR(10))
AS
WITH CTE AS (
SELECT -- some fields
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID
WHERE RegisteredDate = DATEADD(MONTH, DATEDIFF(MONTH, -1, #ReportRunDate) - 2, -1)
AND UserID = #User_ID
--I'll output the result set after applying all the logic like
SELECT UserID, UserName, DOJ
FROM CTE
And, my standalone SQL looks something like:
SELECT DISTINCT User_ID
FROM MyTable
ORDER BY User_ID

Stored procedure to execute a query and return selected values if the query returns only 1 result

So my query is the following, which may return many results:
SELECT P_CODE, NAME FROM TEST.dbo.PEOPLE
WHERE NAME LIKE '%JA%'
AND P_CODE LIKE '%003%'
AND DOB LIKE '%1958%'
AND HKID = ''
AND (MOBILE LIKE '%28%' OR TEL LIKE '%28%')
I would like to integrate this into a Stored Procedure (or View?) so that it will only return a result if the query results in exactly 1 row. If there's 0 or > 1, then it should return no results.
If you just want to return an empty resultset in cases other than 1:
;WITH x AS
(
SELECT P_CODE, NAME, c = COUNT(*) OVER()
FROM TEST.dbo.PEOPLE
WHERE NAME LIKE '%JA%'
AND P_CODE LIKE '%003%'
AND DOB LIKE '%1958%'
AND HKID = ''
AND (MOBILE LIKE '%28%' OR TEL LIKE '%28%')
)
SELECT P_CODE, NAME FROM x WHERE c = 1;
Otherwise, you'll have to run the query twice (or dump the results to intermediate storage, such as a #temp table) - once to get the count, and once to decide based on the count whether to run the SELECT or not.
Effectively you want something akin to FirstOrDefault() from the Linq-to-SQL implementation but done on the server-side which means you will need to execute the query in a stored procedure, dumping the results into a temp table variable and then access ##ROWCOUNT afterwards to get the number of rows that were returned and then decide whether or not to forward the results on to the caller. If you do, be sure to use TOP 1 in the query from the temp table so that you only get a single result out as you desire.
UPDATE:
I described the alternate solution from what Aaron describes in his answer (which I like better).
Removed unnecessary TOP specifier in solution specification.

Oracle function and query return different results

I am using oracle 10g database.
Function is :
create or replace FUNCTION FUNC_FAAL(myCode number,firstDate date
, secondDate date)
RETURN INTEGER as
rtr integer;
BEGIN
select count(*) into rtr
from my_table tbl where tbl.myDateColumn between firstDate and
secondDate and tbl.kkct is null and tbl.myNumberColumn = myCode ;
return (rtr);
END FUNC_FAAL;
This function returns 117177 as result.
But if I run same query in the function seperately ;
select count(*)
from my_table tbl
where tbl.myDateColumn between firstDate and secondDate
and tbl.kkct is null and tbl.myNumberColumn = myCode ;
I get different result 11344 (which is the right one).
What can be the problem ?
Thanks.
You've obfuscated your code, and I suspect hidden the problem in the process.
I suspect your code is more like
create or replace FUNCTION FUNC_FAAL(myNumberColumn number,firstDate date
, secondDate date)
RETURN INTEGER as
rtr integer;
BEGIN
select count(*) into rtr
from my_table tbl where tbl.myDateColumn between firstDate and
secondDate and tbl.kkct is null and tbl.myNumberColumn = myNumberColumn ;
return (rtr);
END FUNC_FAAL;
where the parameter or local variable has the same name as the column in the table. In the SQL, the table column takes precedence and so the variable isn't used and the column is compared to itself, giving a greater number of matches.
It is best to prefix variables and parameters (eg v_ and p_) to avoid such problems.
the function could be in a schema which also has the table in question. it could be working with respect to that table. when you are running the query independently, you could be using a table in a different schema. this is one possibility.
if this is the case, specifying the table name as a fully qualified one (schema.table) should solve the problem.
I'd run TKPROF to see what SQL you are actually processing in the database, specifically to see how the date variables are being recognised.