Can't find the same questions as mine I'm hoping you guys can help me.
I have this query for my SP what I need to do is check if the company Id is the same based on the parameter given by the user.
I have two parameters which are paramOrgA and paramOrgB which will be supplied by the user.
What I did was repeat the same query and set the returned value in two variables which is
orgAId and orgBId then use the IF statement to compare the value.
is there another way to accomplish this or should I say optimized way to do this?
Below is the query I wrote.
BEGIN
declare orgAId int;
declare orgBId int;
declare orgStatus bool;
SET orgAId = (SELECT c.Id
FROM Members as `m`
INNER JOIN Company as `c`
ON m.CompanyId = c.Id
WHERE m.Id = paramOrgA);
SET orgBId = (SELECT c.Id
FROM Members as `m`
INNER JOIN Company as `c`
ON m.CompanyId = c.Id
WHERE m.Id = paramOrgB);
IF (orgAId = orgBId) THEN
set orgStatus = true;
ELSE
set orgStatus = false;
END IF;
select orgStatus as 'CompanyStatus';
END IF;
END
One query will do
SELECT count(distinct c.Id) > 1 as 'CompanyStatus'
FROM Members as `m`
INNER JOIN Company as `c` ON m.CompanyId = c.Id
WHERE m.Id IN (paramOrgA, paramOrgB)
Even fewer keystrokes:
SELECT COUNT(DISTINCT CompanyId) > 1 as 'CompanyStatus'
FROM Members
WHERE Id IN (paramOrgA, paramOrgB)
Related
I'm trying to perform a simple update in SQL between 2 tables from different DB's. The challenge is that in order for the value to be updated it must meet certain conditions. I have used the join statements to meet the conditions and when I go to test the value from table B it is not being updated into table A. Here is what I've done so far.
USE [dbo]
GO
CREATE PROCEDURE
(
#User_ID = INT,
#Batch_ID VARCHAR(32)
)
DECLARE #locid int
SELECT #locid
FROM OtherDB.dbo.User AS UL
WHERE UL.User_ID = #User_Id
and User_Type = 1;
UPDATE M
SET
M.Number = W.Number
FROM dbo.tableA AS W
JOIN dbo.tableB AS B ON B.ID = W.ID
JOIN dbo.tableC AS C ON C.ToolA = B.ToolA
JOIN dbo.tableD as D ON D.Zone = W.Zone_Name
JOIN OtherDB.dbo.tableMax AS M ON M.LID = #locid
AND M.Tool = C.Other_Tool
AND M.Zone = D._Other_Zone
AND M.Station = W.Station
WHERE W.User_ID = #User_ID
AND W.Batch_ID = #Batch_ID
SET NOCOUNT OFF;
The update statement is updating table M in otherDB, not table A on the current database.
You might revise the stored procedure to be
Update A
Set A.Number = W.Number
From dbo.tableA A
....
I don't know why my case code is having an error but when I execute it not using a parameter by replacing the #PersonId by 2 it runs well. I try to exchange2x the JOIN but I am still having an error. Anyone knows?.
case when ISNULL(dbo.EducationalBackground.SchoolId,'') = '' then
(Select a.SchoolName
from dbo.EducationalBackground as A INNER JOIN
dbo.PersonEducationalBackground as B on a.EducationalBackgroundId = b.EducationalBackgroundId INNER JOIN
dbo.EducationLevel as C on a.EducationalLevelId = c.EducationLevelId
where b.PersonId = #PersonId and a.EducationalLevelId in (2,3))
else (select dbo.school.SchooldName from dbo.School INNER JOIN dbo.EducationalBackground
ON dbo.School.SchoolId = dbo.EducationalBackground.SchoolId INNER JOIN
dbo.PersonEducationalBackground ON dbo.EducationalBackground.EducationalBackgroundId = dbo.PersonEducationalBackground.EducationalBackgroundId
where dbo.PersonEducationalBackground.PersonId = #PersonId)
Cheers. Thanks.
We have two table with this structures:
table_user : id,name
table_user_groups: user_id, group_id
some users has no group so they have no entry in table_user_groups.
I want to determine these users and insert a default group_id for them.
I think I have to use an INSERT query with a JOIN condition.
any suggestion.
thanks in advance.
The Select statement to get the users you want is that and below that is the cursor for the insert.
SELECT A.id FROM table_user A
LEFT OUTER JOIN table_user_groups B ON A.id = B.user_id
WHERE B.user_id = NULL
DECLARE #userid int
DECLARE cursor CURSOR FOR
SELECT A.id FROM table_user A
LEFT OUTER JOIN table_user_groups B ON A.id = B.user_id
WHERE B.user_id = NULL
OPEN cursor
FETCH NEXT FROM cursor
INTO #userid
WHILE ##FETCH_STATUS = 0
BEGIN
Insert into table_user_groups(user_id,group_id) VALUES (userid, 'DEFAULTUSERID')
END
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;
Try something like this:
> INSERT INTO table_user_groups(user_id, group_id)
SELECT tu.id, [DEFAULT ID]
FROM table_user tu
LEFT JOIN table_user_groups tug ON tug.user_id = tu.id
WHERE tug.id IS NULL;
Where [DEFAULT ID] is the value what you want to use as default for this users.
UNTESTED
use this :
INSERT INTO table_user_group
(SELECT id,'yourdefaultID' FROM table_user WHERE id
NOT IN (SELECT user_id FROM table_user_groups))
I hope someone can help. I have created this stored procedure to pull some infromation from different tables and when i tun it I get many duplicates, I was wondering if there was a way of sorting it?
use ICA
if exists(
select *
from INFORMATION_SCHEMA.ROUTINES
where SPECIFIC_SCHEMA = N'ICASchema'
and SPECIFIC_NAME = N'candidateOffer'
)
drop procedure ICASchema.candidateOffer
go
create procedure ICASchema.candidateOffer #candidateID INT
as
begin
select c.CandidateID,c.CandidateName, ca.CourseID, co.CourseName, o.OfferID, ot.OfferTypePoints
from [ICASchema].[tblCandidate] c
join [ICASchema].[tblCandidateOffer] o on c.CandidateID = o.CandidateID
join [ICASchema].[tblOffer] ot on o.offerID = ot.OfferType
join [ICASchema].[tblCandidateApplication] ca on ca.CourseID = ca.CourseID
join [ICASchema].[tblCourse] co on co.CourseName = co.CourseName
where c.CandidateID = #candidateID
--ORDER BY co.CourseName
GROUP BY co.CourseName, c.CandidateID,c.CandidateName, ca.CourseID, o.OfferID, ot.OfferTypePoints
end
go
Thanks
Graham
Following example is for creating a simple Proc_GetAllUsers stored procedure. This will solve your duplication issue.
DROP PROCEDURE IF EXISTS Proc_GetAllUsers
GO
CREATE PROCEDURE Proc_GetAllUsers( )
BEGIN
-- your sql queries
select * FROM users;
END
Go
try this:
select * from (select distinct c.CandidateID,c.CandidateName, ca.CourseID, co.CourseName, o.OfferID, ot.OfferTypePoints
from [ICASchema].[tblCandidate] c
join [ICASchema].[tblCandidateOffer] o on c.CandidateID = o.CandidateID
join [ICASchema].[tblOffer] ot on o.offerID = ot.OfferType
join [ICASchema].[tblCandidateApplication] ca on ca.CourseID = ca.CourseID
join [ICASchema].[tblCourse] co on co.CourseName = co.CourseName
where c.CandidateID = #candidateID
GROUP BY co.CourseName, c.CandidateID,c.CandidateName, ca.CourseID, o.OfferID, ot.OfferTypePoints) tablealias order by CourseName
This was the working solution.
[code]SELECT c.CandidateID, c.CandidateName, ICASchema.tblOffer.OfferTypePoints, ICASchema.tblCourse.CourseName
FROM ICASchema.tblCandidate AS c INNER JOIN
ICASchema.tblCandidateOffer ON c.CandidateID = ICASchema.tblCandidateOffer.CandidateID INNER JOIN
ICASchema.tblOffer ON ICASchema.tblCandidateOffer.CandidateOfferID = ICASchema.tblOffer.OfferID INNER JOIN
ICASchema.tblCourse ON ICASchema.tblOffer.OfferID = ICASchema.tblCourse.OfferTypeID
WHERE (c.CandidateID = '3')
[/code]
I am trying to call one stored procedure withing another - using an if statement.
I am getting an error so I believe that I have something out of sequence
CREATE PROCEDURE reportFreeCoolingTrackerCalls (
IN fromDate varchar (50),
IN toDate varchar (50),
IN timeZone varchar (50))
BEGIN
DECLARE startDate varchar (50);
DECLARE endDate varchar (50);
DECLARE mylogID Int;
SET startDate = FROM_UNIXTIME(fromDate/1000);
SET endDate = FROM_UNIXTIME(toDate/1000);
IF (l1.activityId = t2.activityId)
THEN CALL reportFreeCoolingTrackerError (
fromDate,
toDate,
timeZone );
ELSEIF (l1.activityId != t2.activityId)
THEN CALL reportFreeCoolingTracker (
fromDate,
toDate,
timeZone );
END IF;
SELECT l1.activityId,t2.activityId
FROM logs l
INNER JOIN groups g ON g.groupId = l.groupId
LEFT JOIN groups g1 ON g.parentId = g1.groupId
LEFT JOIN groups g2 ON g1.parentId = g2.groupId
LEFT JOIN groups g3 ON g2.parentId = g3.groupId
INNER JOIN activities a ON l.logId = a.logId
INNER JOIN log1644 l1 ON a.activityId = l1.activityId
INNER JOIN log1644 t2 ON t2.recordId = l1.recordid + 1
INNER JOIN items i ON l.logId = i.logId AND i.name LIKE '%KW%'
INNER JOIN users u ON l1.userId = u.userId AND i.name LIKE '%KW%'
WHERE i.itemID = "31985" AND l1.activityId = 1257
AND l1.started
BETWEEN startDate
AND endDate
ORDER BY l1.recordId,l1.started;
END //
DELIMITER ;
ERROR
Unknown table 'l1' in field list
I think this code is causing the trouble for you:
IF (l1.activityId = t2.activityId) --Here l1 and t2 are not known
THEN CALL reportFreeCoolingTrackerError (
fromDate,
toDate,
timeZone );
ELSEIF (l1.activityId != t2.activityId) --Here l1 and t2 are not known
THEN CALL reportFreeCoolingTracker (
fromDate,
toDate,
timeZone );
END IF;
as in this line l1 is not known. Similarly t2 is also not known
Table aliases (such as INNER JOIN log1644 l1 ON... are only visible in the statement (in your case a SELECT) that contains them.
You'll need to refer to the full table name outside of that select.
As for the logic.... you'll need to populate the values you need in the SP call by using a select, and populating them into variables. Alternatively, use a select subquery in the call itself, but that would be a maintenance headache!