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
Related
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
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';
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
I have data in flatfile structure which I need to Insert into two tables. The structure is:
ID FName SName DOB Response1 Description1 Response2 Description2 Response3 Description3
3 Bill John 01/01 Yes Fault NULL NULL NULL NULL
4 Cat Bill 01/01 Yes FaultX Emer FaultYX Zeber Nuhgt
The two tables where the above data will be inserted:
Persons table -> ID, FName, SName, DOB
PersonsRelations table -> ID, Response, Description where Response1, 2 etc is NOT NULL.
I have started the tsql query but not sure how to complete/achieve this. The query should read row after row and foreach create a new row in Persons table and insert the related responses & descriptions as new rows in PersonsRelations table. So for example for record with ID = 4 there will be 3 related new row entries in PersonsRelations table.
This should do the trick, you can use the APPLY operator to unpivot a table.
create table #tobeinserted
(
ID int,
FName varchar(50),
SName varchar(50),
DOB date,
Response1 varchar(50),
Description1 varchar(50),
Response2 varchar(50),
Description2 varchar(50),
Response3 varchar(50),
Description3 varchar(50)
);
create table #persons
(
ID int,
FName varchar(50),
SName varchar(50),
DOB date
);
create table #personsRelations
(
PersonId int,
Response varchar(50),
Description varchar(50)
);
insert into #tobeinserted (ID,FName,SName,DOB,Response1,Description1,Response2,Description2,Response3,Description3)
values (3,'Bill','John','20140101','Yes','Fault',NULL,NULL,NULL,NULL),
(4,'Cat','Bill','20140101','Yes','FaultX','Emer','FaultYX','Zeber','Nuhgt');
insert into #persons (id,fname,sname,dob)
select id+6000000, fname, sname, dob
from #tobeinserted
insert into #personsRelations (PersonId, Response, Description)
select t.id+6000000, a.response, a.description
from #tobeinserted t
cross apply
(
values(Response1,Description1),(Response2,Description2),(Response3,Description3)
) as a(response, description)
where a.response is not null
select * from #persons;
select * from #personsRelations;
drop table #personsRelations;
drop table #persons;
drop table #tobeinserted;
Below is a table store in database:
ID Age SectionID
111 29 NULL
100 30 NULL
To update Column Age with with a text file(see below for sample data)
ID Age SectionID
111 29 231
100 30 456
The text file comes without the column names.
I tired using the import wizard but seems its adding all the columns to the table again. I only need to add the SectionID column to the table. Can anyone tell me how to select the settings to make that happen? Thanks!!!!!!
You can use a BULK INSERT to insert the data into a temporary table, do an UPDATE and delete the temporary table.
Example:
CREATE TABLE #TEMP (
ID INT,
Age INT,
SectionID INT)
GO
BULK INSERT #TEMP
FROM 'HereGoesYourFileName'
WITH
(
FIELDTERMINATOR =' ',
ROWTERMINATOR = '\n'
)
GO
UPDATE ot
SET ot.SectionID = t.SectionID
FROM OriginalTable ot JOIN #TEMP t ON ot.ID = t.ID
GO
DROP TABLE #TEMP
GO