I am writing a stored procedure to fetch details based on CATEGORY ID passed into it, if the CATEGORY ID passed to it is not null, i am fetching based on CATEGORY ID , if the CATEGORY ID is null i am fetching all the details whose CATEGORY ID is except 3, can anyone help me in resolving this problem.
My procedure is like:
CREATE procedure [dbo].[SearchAssetdetails]
(
#assetCategory as int = null,
#assetType as int = null,
#assetDescription as nvarchar(200) = null,
#purchaseDate as datetime = null,
#validUpto as datetime = null)
as
begin
select ad.CategoryID,ad.AssetTypeId,ad.AssetDetailId,ad.AssetDescription,ad.Cost,ad.IsOwn,ad.LastModifiedby,ad.LastModifiedDatetime,ad.Location,ad.NoofLicences,ad.PurchaseDate,ad.SerialNumber,ad.ValidUpto,ad.VendorId,ad.Version,ad.WarrantyExpirationDate
from AssetDetails ad where
(ad.AssetDescription like (case when #assetDescription is not null then '%'+#assetDescription+'%' else ad.AssetDescription end)
and ad.CategoryID=(case when #assetCategory is not null then #assetCategory else ad.CategoryID end)
and ad.AssetTypeId=(case when #AssetType is not null then #AssetType else ad.AssetTypeId end)
and ad.PurchaseDate=(case when #purchaseDate is not null then #purchaseDate else ad.PurchaseDate end)
and ad.ValidUpto=(case when #validUpto is not null then #validUpto else ad.ValidUpto end)
) end;
Due to the AND condition in where clause, your inputs to other parameters may be mismatching for input category id as null but replaced with 3.
First, select records with other parameters and see if any of them fetches a record with category id 3.
I suggest you show us records that have category id as 3, to give us a better picture on data.
Else there is no error I believe.
Related
Im having trouble using this code here:
select *
from toadd
where pcode = '' or pcode is null or
brand = '' or brand is null or
description = '' or description is null;
I am trying to return values that doesn't have null or blank value within them columns above.
My problem is: I have coded the query to only return values that are only null or blank values see if it actually works and then Ill use a delete statement based on that query.
But it still returns columns with filled data, even tho I have said not to show any values that are filled in the query.
Not sure why its doing this?
All my columns types are set up as VARCHAR I have mix data with text and numbers.
Here is my table layout:
Table: outerb
Columns:
id int(11) AI PK
pcode varchar(255)
brand varchar(255)
description varchar(255)
size varchar(255)
barcode varchar(255)
This is what it looks like currently
enter image description here
this is my expected result:
enter image description here
change outer or condition to and
select * from toadd where (pcode = '' or pcode is null) and
( brand = '' or brand is null) and
(description = '' or description is null)
If you want rows where all of the tested columns are not empty or not null
select *
from toadd
where !(pcode = '' or pcode is null) AND
!(brand = '' or brand is null) AND
!(description = '' or description is null);
If you want to exclude rows where one or more of the columns are empty or null
select *
from toadd
where !(pcode = '' or pcode is null) OR
!(brand = '' or brand is null) OR
!(description = '' or description is null);
You can get the desired result using the following SQL query
SELECT * FROM `outerb `
WHERE Length(`pcode `) > 0
AND Length(`brand `) >0
AND Length(`description `) > 0
I am working on this peculiar sql stored proc where the business case is as follows:
Business Case:
Table Specialties contains all the Specialties and there is a bit field for each record telling if it's Active or Inactive. We always display only the active records from that table in form of dropdown. Users may select a Specialty which can later on be deactivated. New requirement is to be able to pull that Inactive record along with all the active records in the result set.
Here's how I thought I should do this:
If no specialty is assigned to the person I am pulling up then the dropdown is going to be populated by all active records.
If there is a inactive specialty associated with the person I am pulling up then I send that specialtyID in stored proc as a parameter and return that inactive records along with active records to populate the dropdown.
Below is what I got so far:
So far if I dont pass in any specialtyId then I am returning active specialty records which is working. When I send in a specialtyId parameter then it just returns that one inactive record but not rest of the other active records. I need the rest of the active records too along with that one inactive record.
DECLARE #specialtyId INT = null;
BEGIN
IF isnull(#specialtyId,'')=''
BEGIN
SELECT SpecialtyID AS Id, Specialty AS Name
FROM dbo.Specialties
WHERE IsActive = 1
ORDER BY Specialty;
END
ELSE
BEGIN
SET #specialtyId = #specialtyId ;
SELECT s.SpecialtyID AS Id, s.Specialty AS Name
FROM dbo.Specialties s
WHERE specialtyId = #specialtyId
GROUP BY s.Specialty, s.SpecialtyID
HAVING (Specialty IS NOT NULL)
AND (max(SpecialtyID) IS NOT NULL)
ORDER BY Name;
END
END
It seems to me that this can be done with no IF whatsoever:
SELECT s.SpecialtyID AS Id,
s.Specialty AS Name
FROM dbo.Specialties s
WHERE specialtyID = #specialtyId
OR IsActive = 1;
You can do this all in the where clause. In the case below it checks to see if the #specId is pass in and selects only that ID or Inactive records OR if the #specId is 0 then just select the active records.
IF OBJECT_ID('tempdb..#tmptest') IS NOT NULL
DROP TABLE #tmptest
CREATE TABLE #tmptest
(
SpecialtyID INT IDENTITY(1,1) NOT NULL PRIMARY KEY
, SpecialtyName VARCHAR(50) NOT NULL
, IsActive BIT NOT NULL DEFAULT (0)
)
INSERT INTO #tmptest
VALUES
('Peditrician', 1)
, ('Rad Tech', 1)
, ('Surg Nurse', 1)
, ('Peds Nurse', 1)
, ('Cardio Doctor', 0)
, ('Cardio Nurse', 1)
, ('Test Doctor', 1)
DECLARE #SpecID INT = 0
SELECT *
FROM #tmptest
WHERE
(
(#SpecID > 0 AND (SpecialtyID = #SpecID OR IsActive = 0))
OR
(#SpecID = 0 AND IsActive = 1)
)
I have two queries which are unrelated to each other, first query returns 4 column whereas the second one returns only 1 column.
so how to combine it?
query 1-
$sql = "select postlist.* from postlist order by postlist.id desc ";
query 2-
$sql1 = "select count (commentlist.id) as 'comments',commentlist.id,commentlist.name,commentlist.comment from postlist,commentlist where postlist.id=commentlist.post_id";
current query-
$sql = "select postlist.*, count (commentlist.id) as 'comments' from postlist LEFT JOIN commentlist ON postlist.id=commentlist.post_id order by postlist.id desc ";
Basically, I want to return all records from postlist, whether the commentlist table has any related comments or not.
Here is a database design
drop table if exists postlist;
create table postlist (
id integer not null primary key autoincrement,
post varchar(1000) not null,
name varchar(80) not null,
title varchar(80) not null
);
drop table if exists commentlist;
create table commentlist (
id integer not null primary key autoincrement,
post_id integer not null,
comment varchar(80) not null,
name varchar(80) not null
);
The get() will cast it to a Collection, that is a lot more powerful than an array. You can append it, iterate over it and more.
Have a bash at that. Hopefully it should be what you need: http://laravel.com/docs/4.2/eloquent#collections
$items = DB::select($sql)->get();
$items1 = DB::select($sql1)->get();
$items = items->toArray();
$items1 = items1->toArray();
$items = array_merge($items, $items1);
I am stucked with following issue,
I ve delcared a varibale (DOUBLE) in a stored procedure, and i need to assign a value (item price) from a table (item) to this declared variable. however, i need to get this value from a select query which uses case inside, the item price could be in 2 columns based on a logic i have to find the correct item price. Please help me solve this as when i execute it gives me a error,
Here how is layered,
DECLARE no_more_users INT DEFAULT 0;
DECLARE user_id INT DEFAULT 0;
DECLARE cart_id INT DEFAULT 0;
DECLARE cart_item_id INT DEFAULT 0;
DECLARE user_gift_id INT DEFAULT 0;
DECLARE itemPrice DOUBLE DEFAULT 0.0;
SELECT
CASE
WHEN sale_price=0 OR sale_price IS NULL THEN (price - ( price * discount ))
ELSE sale_price
END
INTO itemPrice
FROM item WHERE item_id = p_item_id ;
DECLARE checked_in_users CURSOR FOR
SELECT DISTINCT ul.user_id
FROM user_location ul
LEFT JOIN location_section ls ON ul.location_section_id = ls.location_section_id
INNER JOIN user u ON ul.user_id = u.user_id
INNER JOIN user_profile up ON u.user_id = up.user_id
INNER JOIN location_event le ON ul.location_event_id = le.location_event_id
WHERE ul.location_id = p_location_id AND ul.location_event_id = p_event_id
AND ul.checked_out_on IS NULL AND (ul.checked_in_on BETWEEN le.start_time AND le.end_time )
AND u.status = 1 ;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_users = 1;
OPEN checked_in_users;
FETCH checked_in_users INTO user_id;
read_loop: LOOP
some more code...
please note, sale_price, price, and discount are coulmns of item table, The logic is if sale_price is null or value is 0, then i actual sale price should be obtained from price coulmn. Finally what i need is asign corect item price in to previously declared varibale.
Any help will be highly appriciated.
There are two foms to the CASE statement, one with expressions and one with values. You are mixing them up.
with values:
CASE variable
WHEN value_1 THEN foo
WHEN value_2 THEN bar
END
with expressions:
CASE
WHEN expression_1 THEN foo
WHEN expression_2 THEN bar
END
Try
CASE
WHEN sale_price=0 OR sale_price IS NULL THEN (price - ( price * discount ))
ELSE sale_price
END
Also, you do not need the "AS correct_price" when you are using an INTO
Your select query has some problems.
The version of CASE you are are using - ie CASE var WHEN val1 THEN - doesn't work when trying to match a NILL, because in SQL null is not equal to null.
Also, you can't combine values with conditions as you hAve.
Instead, use the version if CASS that simply conditions. There are other suntactic problems. Converting your broken query then gives:
SELECT
CASE
WHEN ifnull(sale_price, 0) = 0 THEN price - (price * discount)
ELSE sale_price
END
INTO itemPrice
FROM item
WHERE item_id = p_item_id;
Note: if your table has s column called itemPrice, you must choose another name from your variable. Mysql gets confused :/
I have a table called Tb_patientBeds.
Now I want to retrieve the records set as occupied, unoccupied or all based on the status column in this table.
Here are my other columns:
patientBedID int IDENTITY(1,1) NOT NULL,
patientBedType [varchar](20) NULL,
BedCharge [varchar](20) NULL,
status [varchar](20) NULL,
I wrote the query like
select * from Tb_patientBeds where [status]= case
when [status]= '0'
then 'occupied'
when [status]='1'
then 'unoccupied' else 'All'
end
The query is not returning records, it's showing empty records.
Could anybody help me in this regard?
Try this:
SELECT
CASE
WHEN [status] = 1 THEN
'unoccupied'
WHEN [status] = 0 THEN
'occupied'
ELSE
'All'
END,
*
FROM Tb_patientBeds