Calling stored procedure from another procedure with where clause - sql-server

I have a table tblemployee with this sample data:
srno name Lastname
------------------------
1 Ibrahim shaikh
2 ibrahim mohammed
3 ibrahim khan
4 paul haymen
And I have a stored procedure SPGetEmp. When I execute the procedure like this:
SPGetEmp 'ibrahim',''
it is returning this result data:
srno name Lastname
------------------------
1 Ibrahim shaikh
2 ibrahim mohammed
3 ibrahim khan
Now I want to filter the data from another stored procedure, like now I want to filter the data with last name, for example 'shaikh'.
Result should be:
srno name Lastname
-----------------------
1 Ibrahim shaikh
What should I do?

You'll need to insert the results into a temp table or variable, and then execute a SELECT query with the desired WHERE clause. For example:
DECLARE #results TABLE(
srno int
, name nvarchar(30)
, Lastname nvarchar(30)
);
INSERT INTO #results
EXEC SPGetEmp 'ibrahim','';
SELECT srno, name, Lastname
FROM #results
WHERE Lastname = 'shaikh';
Consider adding the criteria to the SPGetEmp proc as that will perform better if often executed.

You cannot filter or join the result of stored procedure becase what it returns is not a table even if you "see" it as a table.
But you can rewrite it as an inline table valued function and then you can use it as a "parameterized" view, i.e. you can select from it, filter it, join it, like this:
create function dbo.ufn_GetEmp (#name varchar(100))
returns table
as
return
(
select srno, name, Lastname
from tblemployee
where name = #name
);
go
select *
from dbo.ufn_GetEmp('ibrahim')
where lastname = 'shaikh';

Related

How to delete the multiple columns from the SQL table in SQlite3?

My database contains 'n' number of columns:
name Phone_no Person_1 Person_2 Person_3 Person_4
john 123 1 2 3 4
Nolan 1234 23 34 1 5
If the Phone_no is 1234 then I want to delete the columns Person_1, Person_3.
I know about the column numbers(Column 3( Person_1), Column 5( Person_3 )) which has to be deleted.
Is there any way to delete the multiple columns through a single SQL statement.
Is this what you want?
update mytable set person_1 = null, person_5 = null
where phone_no = 1234
I understand that by delete columns person_1 and person_3 where phone_no is 1234 you mean set values to null in columns person_1 and person_3 where phone_no is 1234.
If you want to actually remove the columns, then it's a different question. In SQLite, you need to recreate the table:
create table tmp_table(
name varchar(50), -- adapt the datatypes and lengths to your requirement
phone_no int,
person_2 varchar(50),
person_4 varchar(50)
);
insert into tmp select name, phone, person_2, person_4 from mytable;
drop table mytable;
create table mytable(
name varchar(50),
phone_no int,
person_2 varchar(50),
person_4 varchar(50)
);
insert into mytable select name, phone, person_2, person_4 from tmp_table;
drop table tmp_table;
Is there any way to delete the multiple columns through a single SQL statement.
Another way is to run a single SQLite3 statement multiple times:
https://stackoverflow.com/a/16162224/3426192
e.g: SQLite 2021-03-12 (3.35.0) now supports: DROP COLUMN

Get multiple scope_identity while inserting data with table-valued parameter

I am inserting multiple rows into Table1 using table-valued parameter.
Now I want to insert Table1's scope identity to Table2 with some values.
How do I achieve that?
You can use the Output Clause clause for that, lets look at a sample
suppose your Table1 looks like this
Table1(Table1_ID int identity, Name varchar(100), Email varchar(100), ...)
Now lets insert and catch all new ID's and Names :
declare #OutputTbl table (ID INT, Name varchar(100))
insert into Table1(Name, Email)
output inserted.Table1_ID, inserted.Name into #OutputTbl(ID, Name)
VALUES ('john doe', 'john#somewhere.com'),
('Anna', 'Anna#1com')
select * from #OutputTbl
the result in #OutputTbl will be
ID Name
-- --------
18 john doe
19 Anna
Now you can off course insert all rows from #OutputTbl into another table if you so desire
insert into Table2 (Table1_ID, Name)
select ID, Name
from #OutputTbl

SQL Distinct function din't work

Here i have a table called tblemployee which consists id,name and salary column.The name and salary column consists five rows, name column consists 3 different name (i.e each name in name column does not match with another name) while the salary column consists the same integer value (i.e 40,000 in each row of salary column).
Table tblemployee structure
name|salary
-----------
max |40000
rob |40000
jon |40000
Now what i want is that, i want all the names from name column but only one salary value from salary column as shown below:
name|salary
-----------
max |40000
rob |
jon |
Sql Server query i have tried which didn't give the expected output
select DISTINCT salary,name from tblabca
Declare #tblemployee table (name varchar(25),salary int)
Insert Into #tblemployee values
('max',40000),
('rob',40000),
('jon',40000),
('joseph',25000),
('mary',25000)
Select Name
,Salary = case when RN=1 then cast(Salary as varchar(25)) else '' end
From (
Select *
,RN = Row_Number() over (Partition By Salary Order By Name)
,DR = Dense_Rank() over (Order By Salary)
From #tblemployee
) A
Order by DR Desc,RN
Returns
Name Salary
jon 40000
max
rob
joseph 25000
mary
"GROUP BY" and group_concat would suit your case. Please try like this
select salary, group_concat(name) from tblabca group by salary;
Reference: GROUP BY, GROUP_CONCAT
You will never get the result as you stated. Because DISTINCT operator works on a SET. not on individual column. In a relational database you will only work with sets.
So the combination of Salary and Name will be treated as Distinct.
But if you want you can get names in comma concatenated list like below
SELECT SALARY
, STUFF ((SELECT ','+NAME From TableA T2
WHERE T1.SALARY = T2.SALARY FOR XML PATH('')
),1,1,'') FROM TABLEA T1
As others already stated, you are definately not looking for DISTINCT operator.
The distinct operator will work upon the entire result set, meaning you'll get result rows that are unique (column by column).
Although with some rework you might end up with the result you want, do you really want the result in such a not uniform way? I mean, getting a list of names on the name column and only one salary on the salary column do not look like a nice result set to work with.
Maby you should work on your code to account for the change you want to make in the query.
declare #tblemployee Table(
id int identity(1,1) primary key not null,
name nvarchar(MAX) not null,
salary int not null
);
declare #Result Table(
name nvarchar(MAX) not null,
salaryString nvarchar(MAX)
);
insert into #tblemployee(name,salary) values ('joseph' ,25000);
insert into #tblemployee(name,salary) values ('mary' ,25000);
insert into #tblemployee(name,salary) values ('Max' ,40000);
insert into #tblemployee(name,salary) values ('rob' ,40000);
insert into #tblemployee(name,salary) values ('jon' ,40000);
declare #LastSalary int = 0;
declare #name nvarchar(MAX);
declare #salary int;
DECLARE iterator CURSOR LOCAL FAST_FORWARD FOR
SELECT name,
salary
FROM #tblemployee
Order by salary desc
OPEN iterator
FETCH NEXT FROM iterator INTO #name,#salary
WHILE ##FETCH_STATUS = 0
BEGIN
IF (#salary!=#LastSalary)
BEGIN
SET #LastSalary = #salary
insert into #Result(name,salaryString)
values(#name,#salary+'');
END
ELSE
BEGIN
insert into #Result(name,salaryString)
values(#name,'');
END
FETCH NEXT FROM iterator INTO #name,#salary
END
Select * from #Result

How to Show a particular row first from a select query

I have a query results like below
name id
a 2
b 3
c 7
h 9
i 1
i need to show the results like below
Expected Result
name id
c 7
a 2
b 3
h 9
i 1
The id field is Primarykey so based on that i want to show this
so far i tried with order by but it only give the option to order like decenting or acenting.
Please help me to do this
I had done something like below
create procedure proc1
#id int;
begin
select name,id from tablname order by id
end
I had also tried like but it also fails . Please help me to solve this
Try this:
CREATE PROCEDURE proc1 (#id int)
AS
SELECT name,id
FROM tablname
ORDER BY CASE WHEN id=#id THEN 0 ELSE id END
Note: This will work assuming the values of your id columns are always greater then 0.
I tried this and it gives exactly what is your required output.
declare #name varchar(10)
set #name = 'c'
create table #temp (id int ,name varchar(10))
insert into #temp(id,name) values(1,'i')
insert into #temp(id,name) values(2,'a')
insert into #temp(id,name) values(3,'b')
insert into #temp(id,name) values(7,'c')
insert into #temp(id,name) values(9,'h')
Select name,id from #temp ORDER BY CASE WHEN name=#name THEN '' ELSE name END
Drop table #temp

Query to add up multiple names from TVP

I have a table of name value pairs like this
Properties
- EmployeeId
My table for employee looks like this:-
EmployeeId FirstName Middle LastName
1 John Smith
2 Rick Steve James
3 Maddy Y Angela
Now my TVP can contain multiple or single EmployeeId's
I want to add up the full names of all the person in TVP in a single parameter.
i.e.
For eg. If I receive 1 and 3 in TVP
My output should be John Smith, Maddy Y Angela.
Do I need to use cursors in this?
What is the best way to accomplish this?
EDIT
The Scipt For the TVP is as below:-
CREATE TYPE [dbo].[Employee] AS TABLE( [EmployeeId] [uniqueidentifier] NULL)GO
The desired output is the Full Names of Employees in the TVP comma seperated.
Assuming you have a table type like:
CREATE TYPE dbo.TVPTypeName AS TABLE(EmployeeId INT PRIMARY KEY);
Then you can just perform a join:
CREATE PROCEDURE dbo.foo
#bar dbo.TVPTypeName READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT STUFF((SELECT ','
+ e.FirstName
+ COALESCE(' ' + NULLIF(RTRIM(e.Middle),''), ' ')
+ e.LastName
FROM dbo.Employees AS e
INNER JOIN #bar AS b
ON e.EmployeeId = b.EmployeeId
FOR XML PATH(''),TYPE).value('.[1]','nvarchar(max)'),1,1,'');
END
GO
CREATE TYPE dbo.IntArray AS TABLE(IntValue INT PRIMARY KEY);
GO
CREATE TABLE dbo.Employee(
EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
MiddleName NVARCHAR(50) NULL,
LastName NVARCHAR(50) NOT NULL
);
GO
INSERT INTO dbo.Employee VALUES ('John',NULL,'Smith');
INSERT INTO dbo.Employee VALUES ('Rick','Steve','James');
INSERT INTO dbo.Employee VALUES ('Maddy','Y','Angela');
GO
CREATE PROCEDURE dbo.CocoJambo(#pEmployee dbo.IntArray READONLY)
AS
BEGIN
DECLARE #Result NVARCHAR(MAX);
SELECT #Result=ISNULL(#Result,'')+','+FirstName+ISNULL(' '+MiddleName,'')+' '+LastName
FROM dbo.Employee e INNER JOIN #pEmployee p ON e.EmployeeID=p.IntValue;
SELECT STUFF(#Result,1,1,'') AS [Result];
END;
GO
-- Test
DECLARE #p dbo.IntArray;
INSERT INTO #p VALUES (1);
INSERT INTO #p VALUES (3);
EXEC dbo.CocoJambo #pEmployee=#p;
/*
Result
-------------------------
John Smith,Maddy Y Angela
*/

Resources