If exists is not printing pseudo values in SQL Server stored procedure - sql-server-2008

I am using a simple if exists statement in my stored procedure, but in output it is not printing the pseudo column values if the records does not exist.
SQL Server 2012
CREATE PROCEDURE test1
#Empid NVARCHAR(20)
AS
BEGIN
IF EXISTS (SELECT TOP 1 * FROM employees WHERE id = #Empid)
BEGIN
SELECT id, name, salary, 'Newemp' AS status, 1 AS Code
FROM employees
END
IF NOT EXISTS (SELECT TOP 1 * FROM employees WHERE id = #Empid)
BEGIN
SELECT id, name, salary, 'Oldemp' AS status1, 0 AS Code1
FROM employees
END
END
Result:
If record exists, this is returned as expected
ID Name Salary Status Code
-------------------------------
123 kkr 1000 Newemp 1
If the record doesn't exist - this is the problem:
Id Name Salary Status1 Code1
-----------------------------
Desired value:
ID Name Salary Status1 Code1
----------------------------
Oldemp 0
If the records doesn't exist, it is not printing the pseudo column values. I have changed the column names and executed to make sure that its taking the correct columns, and yes it is taking the correct ones, but failing to print the values.
Please help!

I did it myself, but Big thanks for your answers.
CREATE PROCEDURE test1
#Empid NVARCHAR(20)
AS
BEGIN
Create #temp
(Id Nvarchar(100),
Name Nvarchar(100),
Salary Nvarchar(100),
Status Nvarchar(100),
Code Nvarchar(100)
)
IF EXISTS (SELECT TOP 1 * FROM employees WHERE id = #Empid)
BEGIN
insert into #temp (id, name, salary)
values (SELECT id, name, salary
FROM employees WHERE id = #Empid)
Insert into # temp (Status , Code)
('Newemp', 1)
END
IF NOT EXISTS (SELECT TOP 1 * FROM employees WHERE id = #Empid)
BEGIN
insert into #temp (id, name, salary)
values (SELECT id, name, salary
FROM employees WHERE id = #Empid)
Insert into # temp (Status , Code)
('Oldemp', 0)
END
Select * from #temp
END

These queries are based on the number of records in employees - it could be one, it could be none, and it could be many. If you always want to get a single row, you can omit the from clause and just select literals:
SELECT NULL AS id, NULL AS name, NULL AS salary, 'Oldemp' ASs status1, 0 AS Code1

Related

How to merge multiple select statement result into single select statement?

I have write stored procedure given below.
ALTER PROCEDURE StudentDemo #RollNo INT
/*
StudentDemo 504
*/
AS
BEGIN
DECLARE #CurrentID INT
--Get the Current RollNo's ID
SELECT #CurrentID = ID FROM StudentData WHERE RollNo = #RollNo
SELECT #CurrentID AS 'CurrentID'
--Previous ID
SELECT RollNO AS 'PreviousID' FROM StuDentData WHERE ID = #CurrentID - 1
--Next ID
SELECT RollNO AS 'NextID' FROM StuDentData WHERE ID = #CurrentID + 1
END
I want all the result of select statement in single one and put result in temporary table

how to write query for this reuslt

I have a employee table having column (empno,enme,salary,deptno)
I want to write a query which displays the following table:containing first column
deptno
,second column
sum of salaries of the employees of each department whose name start
with 'A'
and third column
total slary of all the employees of that department
Any one please help me how to write query for this scenario..?
A have simulated your situation with a table variable and below is the result
DECLARE #table TABLE (empno int,ename VARCHAR(100),salary DECIMAL(18,2),deptno int)
INSERT INTO #table SELECT 1,'shuki',450,100
INSERT INTO #table SELECT 2,'arban',500,100
INSERT INTO #table SELECT 3,'alamet',300,200
INSERT INTO #table SELECT 4,'andrea',150,200
INSERT INTO #table SELECT 5,'florim',450,200
SELECT deptno,SUM(CASE when ename LIKE 'A%' THEN salary ELSE 0 END ) SalaryEmpWithA,SUM(salary) TotalSalary FROM #table
GROUP BY deptno
Output is:
deptno SalaryEmpWithA TotalSalary
100 500.00 950.00
200 450.00 900.00

copy column data to next line on when condition in sql

attendance id status activity
1 0 xyz
2 1 abc
3 2 abc
4 1
I have a column in which id is a unique value for ex when status = 2 update the next attendance id and copy the activity of status = 2 to the next attendance id i.e. 4
How can I do that?
If you mean that you want to copy the status to the next datarow. You can use this statement. I've added some demo code to it.
CREATE TABLE #temp(attendance_id int identity(1,1), status int, acitivity nvarchar(10))
INSERT INTO #temp(status, acitivity)
VALUES(0, N'xyz'),(1,N'abc'),(2,N'abc'),(3,NULL)
-- see what happens
SELECT t.attendance_id, t.status as oldStatus, ISNULL(shifting.status,t.status),t.acitivity
FROM #temp t
LEFT JOIN (
SELECT status, ROW_NUMBER() OVER (ORDER BY attendance_id) +1 as rn
FROM #temp
) as shifting
ON t.attendance_id = shifting.rn
-- the update itself
UPDATE t
SET status = ISNULL(shifting.status,t.status)
FROM #temp t
LEFT JOIN (
SELECT status, ROW_NUMBER() OVER (ORDER BY attendance_id) +1 as rn
FROM #temp
) as shifting
ON t.attendance_id = shifting.rn
SELECT * FROM #temp
DROP TABLE #temp

how to write multiple select queries in single procedure?

I have to tables as:
table1:
UID | COLLEGE_NAME | COLLEGE_ADDRESS
------------------------------------
table2:
UID | COMPANY_NAME | COMPANY_ADDRESS
------------------------------------
i have 2 queries:
select * from table1 where uid='$uid';
select * from table2 where uid='$uid';
i want to write this two queries in one procedure.
structure for multiple select quires in single procedure:
CREATE PROCEDURE sample(l_uid INT) BEGIN
SELECT * FROM college_edu WHERE uid= l_uid;
SELECT * FROM work_experience WHERE uid= l_id;
END
Here's the statement to create STORED PROCEDURE.
DELIMITER $$
CREATE PROCEDURE procedureName(IN _uid VARCHAR(15))
BEGIN
SELECT UID, COLLEGE_NAME name, COLLEGE_ADDRESS address
FROM table1
WHERE uid = _uid
UNION ALL
SELECT UID, COMPANY_NAME name, COMPANY_ADDRESS address
FROM table2
WHERE uid = _uid
END $$
DELIMITER ;
notice that UNION has ALL keyword on it to add duplicate records on the result list. But if you prefer UNIQUE, remove the ALL keyword on the UNION.
below code may serve you purpose. Also the additional tbl column will let you know from which table your data is coming and it would help in further manipulation.
SELECT UID, COLLEGE_NAME name, COLLEGE_ADDRESS address , 1 as tbl
FROM table1
WHERE uid = _uid
UNION ALL
SELECT UID, COMPANY_NAME name, COMPANY_ADDRESS address , 2 as tbl
FROM table2
WHERE uid = _uid

complex sql update

The table MyTable contains 4 columns.
id, phone columns are unique.
id, phone, zipcode are never empty.
id - phone - salary - zipcode
1 - 61730459987 - $56052.45 - 02456
2 - 21249620709 - NULL - 10023
3 - 21200830859 - $39089.28 - 10023
...
10000 - 64600830857 - $46063.72 - 03795**
I need to remove NULLs by replacing them with salary_estimates, based on information from the same zip-codes.
As for the example above, NULL at row #2 could be replaced with $39089.28 (we can find from the table that another person with the same zip-code 10023 earns $39089.28, so it's OK to update NULL for #2 with $39089.28).
So, after we run this query, the number of empty cells could be reduced (although not up to zero).
Could anybody suggest an update query for this problem?
UPDATE
MyTable AS tu
JOIN
( SELECT zipcode
, AVG(salary) AS SalaryEstimate --- AVG, MAX, MIN
--- your choice
FROM MyTable
WHERE salary IS NOT NULL
GROUP BY zipcode
) AS te
ON te.zipcode = tu.zipcode
SET
tu.salary = te.SalaryEstimate
WHERE
tu.salary IS NULL
I am not sure it will work for mysql. But in MSSQL you can do like this :
UPDATE MyTable
SET salary = (select top 1 salary from MyTable where zipcode = t1.zipcode and salary is not null)
FROM MyTable t1
WHERE t1.salary is null
You need to check equivalent join case for mysql.
Ref. Test Query :
create table #temp (id int, salary decimal(18,2), zipcode varchar(10))
insert into #temp values (1,56052.45,02456)
insert into #temp values (2,NULL,1023)
insert into #temp values (3,39089.28,1023)
insert into #temp values (4,46063.72,03795)
insert into #temp values (5,NULL,02456)
UPDATE #temp
SET salary = (select top 1 salary from #temp where zipcode = t1.zipcode and salary is not null)
FROM #temp t1
WHERE t1.salary is null
select * from #temp
drop table #temp
You can do like this: if salary is null it will replace with salary of that zipcode.
isnull(salary,(Select top 1 salary from MyTable where zipcode=10023 ))