I have a table
Table1
+-------+-----------+----------+
| EmpNo | FirstName | LastName |
+-------+-----------+----------+
| 123 | Bob | Smith |
| 456 | John | Smith |
| 789 | Bill | Smith |
+-------+-----------+----------+
I would like to make a stored procedure to either delete the entire table or just one entry from the table based on a parameter passed in.
Something similar to
Delete from Table1
Case where #DeleteAll = 1
Then where EmpNo is not null
Else where EmpNo = #employee
or
delete from Table1
If #DeleteAll = 1
Then where EmpNo is not null
Else
where EmpNo = #employee
select * from Table1
Delete from Table1
where #DeleteAll = 1
or EmpNo = #employee
Try this:
IF #DeleteAll = 1
BEGIN
DELETE
FROM TABLE1
WHERE EmpNo IS NOT NULL
END
ELSE
DELETE
FROM TABLE1
WHERE EmpNo = #employee
;
Related
Without using the pivot/unpivot function or a union, is it possible to change this data:
+----+--------+---------+
| id | name | surname |
+----+--------+---------+
| 1 | john | smith |
| 2 | jack | brown |
+----+--------+---------+
into this:
+----+-------------+
| id | data |
+----+-------------+
| 1 | john |
| 1 | smith |
| 2 | jack |
| 2 | brown |
+----+-------------+
Well you could use a cursor
drop procedure if exists p;
delimiter $$
CREATE PROCEDURE `p`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
begin
declare vid int(8);
declare vname varchar(15);
declare vsurname varchar(15);
declare done int default 0;
declare cname cursor for select id,name,surname from t id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cname;
cursorloop:loop
fetch cname into vid,vname,vsurname;
if done = true then
leave cursorloop;
end if;
insert into t1 (id,name) select vid,vname;
insert into t1 (id,name) select vid,vsurname;
end loop cursorloop;
close cname;
end $$
delimiter ;
drop table if exists t,t1;
create table t( id int, name varchar(20), surname varchar(20));
create table t1(id int, name varchar(20));
insert into t values
( 1 , 'john' , 'smith'),
( 2 , 'jack' , 'brown');
call p();
MariaDB [sandbox]> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 1 | john |
| 1 | smith |
| 2 | jack |
| 2 | brown |
+------+-------+
4 rows in set (0.00 sec)
But why would you not use a union?
Use cross join and case statement to either get name or surname.
select rn as id, (case when id=rn then name else surname end) as `data`
from
(
select t1.id, t1.name, t2.surname, t2.rn
from myTable t1
cross join (
select surname, (#r2:=#r2+1) as rn
from myTable, (select #r2:= 0) r) t2
) tab
Result:
id data
1 jon
1 smith
2 brown
2 jack
i need help about sql query..
Sorry but I do not know much about sql, and I'm bad with english.
I have sql table like this.
| ID | Data |
| 1 | abc |
| 1 | def |
| 1 | ghi |
| 2 | jkl |
| 2 | mno |
| 3 | pqr |
| 3 | stq |
and i want result like this
| ID | Data |
| 1 | abc, def, ghi |
| 2 | jkl, mno |
| 3 | stq, def |
or like this
| ID | Data1 | Data2 | Data3 |
| 1 | abc | def | ghi |
| 2 | jkl | mno |
| 3 | pqr | stq |
I hope someone can help me. Thanks.
Try the below query, it should work
select
t1.id,
Data=STUFF((select ', '+t2.Data from test t2 where t1.id=t2.id for xml path('')),1,2,'')
from test t1
group by t1.id
i try this query in sql server
For Sql server:
select id,
stuff((select ','+data from #temp t2 where t1.id=t2.id for xml path ('')),1,1,'') as val from #temp t1
group by id
for mysql:
SELECT id,GROUP_CONCAT(data)
FROM table
GROUP BY id;
For mysql:
group_concat is your friend.
This function returns a string result with the concatenated non-NULL
values from a group. It returns NULL if there are no non-NULL values.
SELECT id, group_concat(data) as data from my_table group by id
this produces results of the type in your first sample. The second sample output is more complicated,
Use Stuff function:
DECLARE #tbl1 AS TABLE
(
Id INT,
Data VARCHAR(50)
)
INSERT INTO #tbl1 VALUES(1,'abc')
INSERT INTO #tbl1 VALUES(1,'def')
INSERT INTO #tbl1 VALUES(1,'ghi')
INSERT INTO #tbl1 VALUES(2,'jkl')
INSERT INTO #tbl1 VALUES(2,'mno')
INSERT INTO #tbl1 VALUES(3,'pqr')
INSERT INTO #tbl1 VALUES(3,'stu')
SELECT
Id,
(
SELECT
STUFF
(
(
SELECT
',' + Data
FROM #tbl1 B
WHERE A.Id=B.Id
FOR XML PATH('')
),
1, 1, ''
)
) AS Data
FROM #tbl1 A
GROUP By A.Id
I have 3 tables: ak_class, ak_objects, ak_class_object
ak_class:
class_id | class_description | class_name |
1 | some description | some name |
2 | some description | some name |
3 | some description | some name |
ak_objects:
object_id | object_description | object_name |
1 | some description | some name |
2 | some description | some name |
3 | some description | some name |
ak_class_object:
class_object_id | class_id | object_id |
1 | 1 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
I need to fill in the ak_class_object with a class_id from ak_class table and object_id from ak_objects table.
The question is how can I update (I need to update as there is some wrong data currently) the class_id from the ak_class table with all the ids? I was thinking of using it with JOIN ut I don't know which id to use to Join them as class_id is only to be updated
UPD: I was trying to do it like this, but it didn't work:
DELIMITER $$
DROP PROCEDURE class_object_1$$
CREATE PROCEDURE class_object_1()
BEGIN
DECLARE i INT DEFAULT 0;
WHILE (i < 250000) DO
UPDATE ak_class_object
SET class_id = SELECT DISTINCT class_id from ak_class, object_id = SELECT DISTINCT class_id from ak_objects;
SET i = i + 1;
END WHILE;
END$$
I am writing the generic syntax, change the table names and column name as per your requirements.
update table1 inner join table2
on table1.id = table2.fk_id
set table1.column = table1.columnUpdated
Hi I need to be able to join two tables and return the second table
as columns to the first table. I need to create
(dependent first name, dependent last name, and dependent relationship)
based on the max number depid (which can be dynamic).
thank you in advance
table 1
+-------------+-------------+------------+
| employeeid | first name | last name |
+-------------+-------------+------------+
| 1 | bill | johnson |
| 2 | matt | smith |
| 3 | katy | lewis |
+-------------+-------------+------------+
table 2
+-------------------------------------------------------------------+
| employeeid |dependent id | First Name | Last Name | Relationship |
+-------------------------------------------------------------------+
| 1 1 mary johnson spouse |
| 1 2 aaron johnson child |
| 2 1 eric smith child |
+-------------------------------------------------------------------+
expected output
+------------+------------+-----------+----------------------+---------------------+------------------------+----------------------+---------------------+------------------------+
| employeeid | first name | last name | dependent first name | dependent last name | dependent relationship | dependent first name | dependent last name | dependent relationship |
+------------+------------+-----------+----------------------+---------------------+------------------------+----------------------+---------------------+------------------------+
| 1 | bill | johnson | mary | johnson | spouse | aaron | johnson | child |
| 2 | matt | smith | eric | smith | child | | | |
| 3 | katty | lewis | | | | | | |
+------------+------------+-----------+----------------------+---------------------+------------------------+----------------------+---------------------+------------------------+
You Can do This with dynamic SQL & XML Path example SQL below
--Table 1
CREATE TABLE #TMP1 (EMP_ID INT, NAME Char(10) )
INSERT INTO #TMP1 VALUES (1,'One')
INSERT INTO #TMP1 VALUES (2,'TWO')
INSERT INTO #TMP1 VALUES (3,'Three')
--Table 2
CREATE TABLE #TMP2 (EMP_ID INT, DP_ID INT,FNAME Char(10),Rel Char(10) )
INSERT INTO #TMP2 VALUES (1,1,'Spouse One','Spouse')
INSERT INTO #TMP2 VALUES (1,2,'Child One','Child')
INSERT INTO #TMP2 VALUES (2,1,'Child TWO','Child')
Declare #CNT Int
, #Ctr int = 0
, #SQL VarChar(MAX)
--Get Max Dependent ID
SELECT #CNT = MAX(DP_ID) from #TMP2
--For Verification
SELECT #CNT
--Build Dynamic SQL to get the dataset
SET #SQL = 'SELECT Emp_ID '
While #Ctr < #CNT
Begin
Set #Ctr = #Ctr+1
SET #SQL = #SQL + ', ( SELECT FName+'+''''+''''+' FROM #TMP2 Where #TMP1.EMP_ID = #TMP2.EMP_ID and #TMP2.DP_ID = '+Convert(VarChar(2),#Ctr)+' For XML Path ('+''''+''''+') ) as FName'+Convert(VarChar(2),#Ctr)
SET #SQL = #SQL + ', ( SELECT Rel+'+''''+''''+' FROM #TMP2 Where #TMP1.EMP_ID = #TMP2.EMP_ID and #TMP2.DP_ID = '+Convert(VarChar(2),#Ctr)+' FOR XML Path ('+''''+''''+') ) as Rel'+Convert(VarChar(2),#Ctr)
End
SET #SQL = #SQL+' FROM #TMP1 '
--For Verification Print the Dynamic SQL
Select #SQL
--Execute the dynamic SQL
EXEC(#SQL)
I have two tables:
Table A with system values
------------------------
id | val_1 | val_2 |
------------------------
1 | 11 | 22 |
------------------------
Table B with user values
-----------------------
uid | set | val_3 |
-----------------------
21 | 1 | 11 |
-----------------------
68 | 2 | 22 |
-----------------------
83 | 1 | 11 |
-----------------------
I'd like to update val_3 in Table B with the values of val_1 and val_2 of Table A, according to the values of set in Table B.
So if I change: Table A val_1 => 333, Table A val_2 => 666, the update query changes Table B to:
uid | set | val 3 |
-----------------------
21 | 1 | 333 |
-----------------------
68 | 2 | 666 |
-----------------------
83 | 1 | 333 |
-----------------------
Is this possible in one query?
Now I do:
$result=$mysql->query('SELECT val1,val2 from TABLE A WHERE id=1');
UPDATE TABLE B set val3=$result[0] WHERE set=1
UPDATE TABLE B set val3=$result[1] WHERE set=2
Maybe I could do a CASE WHEN but I don't know how to do it without a corresponding row value for Table A.
Do an update using a JOIN:-
UPDATE TableB
CROSS JOIN TableA
SET TableB.val_3 = CASE WHEN TableB.`set` = 1 THEN TableA.val_1 ELSE TableB.`set` = 2 THEN TableA.val_2 END
WHERE TableA.id=1
AND TableB.`set` IN (1,2)
Try this:
UPDATE TableB
SET val3 =
CASE set
WHEN 1 THEN (SELECT val1 FROM TableA WHERE id=1)
WHEN 2 THEN (SELECT val2 FROM TableA WHERE id=1)
END
UPDATE Table_B
SET val_3 =
CASE set
WHEN 1 THEN (SELECT val_1 FROM Table_A WHERE id=1)
WHEN 2 THEN (SELECT val_2 FROM Table_A WHERE id=1)
END