Convert this MySQL to Sybase - mysql

I have the following MySQL:
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'sum(case when perm = ''',
perm,
''' then 1 else 0 end) AS ',
perm
)
) INTO #sql
FROM perms;
SET #sql = CONCAT('SELECT role, ', #sql, '
FROM perms
GROUP BY role');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
However, I have to use Sybase 12, does anyone know how to convert this?

Try this:
declare cur_perm cursor for
select perm from perms
go
declare #sql varchar(4000)
declare #perm varchar(4000)
set #sql = ' '
open cur_perm
fetch cur_perm into #perm
while ##sqlstatus=0 begin
select #sql = #sql+'sum(case when perm = '''+#perm+''' then 1 else 0 end) AS '''+#perm+''', '
fetch cur_perm into #perm
end
close cur_perm
deallocate cursor cur_perm
select #sql = 'SELECT '+ #sql+' role
FROM perms
GROUP BY role'
exec(#sql)

Related

How to print more than one values in Pivot Table?

I Have a table Attendances
and then I Make a Report using this Query
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(case when `tanggal` = ''',
`tanggal`,
''' then `in_time` end) AS `',
`tanggal`, '`'
) ORDER BY `id_employee` ASC SEPARATOR ',\n'
) INTO #sql
FROM `attendances` ;
SET #sql2 = CONCAT('SELECT id_employee, ', #sql, ' FROM attendances GROUP BY id_employee');
PREPARE stmt FROM #sql2;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Output Query :
Question : I Want To Print in_time and out_time after clause then ? Is it Possible ?
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(case when `tanggal` = ''', `tanggal`, ''' then CONCAT(in_time,''-'', out_time) end) AS `', `tanggal`, '`'
) ORDER BY `id_employee` ASC SEPARATOR ',\n'
) INTO #sql
FROM `attendances` ;
SET #sql2 = CONCAT('SELECT id_employee, ', #sql, ' FROM attendances GROUP BY id_employee');
PREPARE stmt FROM #sql2;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Generate result in CSV file using prepared statement query in laravel

I have created this query to generate report and its working fine but i want to download the result of this query in .CSV file. How to execute this query in laravel ?
Query :
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when wm.SubPointId = ''',
wm.SubPointId,
''' then Balance else 0 end) AS ',
REPLACE ( ws.SubPointType, ' ', '' )
)
) INTO #sql
FROM wallet_master wm, wallet_subpoints ws WHERE wm.SubPointId = ws.SubPointId;
SET #sql = CONCAT('SELECT MobileNo, ', #sql, ', sum(Balance) as `TotalBalance`
FROM wallet_master wm
GROUP BY MobileNo');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Unable to pass parameter value in Concat select Prepared statements

I found the stored procedure does not execute only this portion:
SET #sql
= CONCAT('SELECT TraineeID, ', #sql, '
from tbl_submit_coursefee c
where c.BatchID='BID'
group by c.TraineeID');
in where clause '' quotes do not allow and without quotes query return empty but if I put the parameter value in where clause straight then it works. I'm really stack with this.
Here is my prepared statement (working fine):
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT ('MAX(IF(BillNo = ''', BillNo, ''', CRA, NULL)) AS `Inv.', BillNo, '`')
) INTO #sql
FROM tbl_submit_coursefee
WHERE BatchID="ID-Welding/FMMTTC-01M/R8/01";
SET #sql
= CONCAT('SELECT TraineeID, ', #sql, '
from tbl_submit_coursefee c
where c.BatchID="ID-Welding/FMMTTC-01M/R8/01"
group by c.TraineeID');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
when put this statement in a stored procedure (not working):
DELIMITER ##
DROP PROCEDURE GetRetainment ##
CREATE PROCEDURE vtproject.GetRetainment
(IN `BID` VARCHAR(100))
BEGIN
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT ('MAX(IF(BillNo = '', BillNo, '', CRA, NULL)) AS `Inv.', BillNo, '`')
) INTO #sql
FROM tbl_submit_coursefee
WHERE BatchID=BID;
SET #sql
= CONCAT('SELECT TraineeID, ', #sql, '
from tbl_submit_coursefee c
where c.BatchID='BID'
group by c.TraineeID');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END ##
DELIMITER ;
If I select #sql before prepare stmt FROM #sql then output as below:
SELECT TraineeID, MAX(IF(BillNo = 1, CRA, NULL)) AS `Inv.1`,MAX(IF(BillNo = 2, CRA, NULL)) AS `Inv.2`,MAX(IF(BillNo = 3, CRA, NULL)) AS `Inv.3`,MAX(IF(BillNo = 4, CRA, NULL)) AS `Inv.4`,MAX(IF(BillNo = 5, CRA, NULL)) AS `Inv.5`,MAX(IF(BillNo = 6, CRA, NULL)) AS `Inv.6`,MAX(IF(BillNo = 7, CRA, NULL)) AS `Inv.7` from tbl_submit_coursefee c where c.BatchID=BID group by c.TraineeID
you need to escape the quotes,
like this :
SET #sql
= CONCAT('SELECT TraineeID, ', #sql, '
from tbl_submit_coursefee c
where c.BatchID=''',BID,'''
group by c.TraineeID');
according with doc : http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
I found the solution in another way myself.
BEGIN
SET #sql = NULL;
SET #bid=BID;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT ('MAX(IF(BillNo = ', BillNo, ', CRA, NULL)) AS `Inv.', BillNo, '`')
) INTO #sql
FROM tbl_submit_coursefee
WHERE BatchID=#bid;
SET #sql
= CONCAT('SELECT TraineeID, ', #sql, '
from tbl_submit_coursefee c
where c.BatchID=#bid
group by c.TraineeID');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END

Create table from a previous prepared statemt

Hi i've got something like this as part of a more complex query:
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Rather than executing the stmt itself i want to create a table from the result of the execute.
Create table A as select * from (Execute stmt)
gives me error
EDIT:
Here's the entire stuff:
Use catdatabase;
SET SESSION group_concat_max_len = 1000000;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(CASE WHEN columnA = "' ,columnA, '"THEN 1 ELSE 0 end) AS "' ,columnA, '"'))
INTO #sql
FROM
tableB;
SET #sql = CONCAT('SELECT columnB, Count(*) total, ', #sql, '
FROM tableA inner join tableB on tableA.columnC = tableB.columnE where tableA.columnD <> "catpoop"
GROUP BY columnB');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
As I can see you want to pivot your data and wants to save into temporary table.
I have updated your query, Please check out this:
Use catdatabase;
SET SESSION group_concat_max_len = 1000000;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(CASE WHEN columnA = "' ,columnA, '"THEN 1 ELSE 0 end) AS "' ,columnA, '"'))
INTO #sql
FROM
tableB;
SET #sql = CONCAT('Create temporary table temp SELECT columnB, Count(*) total, ', #sql, ' FROM tableA inner join tableB on tableA.columnC = tableB.columnE where tableA.columnD <> "catpoop"
GROUP BY columnB');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
If you get any error in this just let me know.
set #table='t2';
SET #table1 = 'test';
set #a = concat('create table ',#table,' select * from ',#table1);
prepare stmt1 from #a;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
https://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
updated:
SET SESSION group_concat_max_len = 1000000;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('SUM(CASE WHEN columnA = "' ,columnA, '"THEN 1 ELSE 0 end) AS "' ,columnA, '"'))
INTO #sql
FROM
tableB;
SET #sql = CONCAT('SELECT columnB, Count(*) total, ', #sql, '
FROM tableA inner join tableB on tableA.columnC = tableB.columnE where tableA.columnD <> "catpoop"
GROUP BY columnB');
set #new_sql = concat ('create table A',#sql);
PREPARE stmt FROM #new_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Select table from database where value is X

So far I've found out that this gives me a list of all the tables which has the column name "store_id" - but I only want it to select the columns if "store_id" = 4, how could I do this?
Right now I use this to find the tables which has the "store_id" column.
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('store_id')
AND TABLE_SCHEMA='db1';
You can it with dynamic SQL like this
SET #sql = NULL;
SELECT GROUP_CONCAT(DISTINCT
CONCAT('SELECT ''', TABLE_NAME,
''' table_name FROM ', TABLE_NAME,
' WHERE store_id = 4')
SEPARATOR ' UNION ALL ')
INTO #sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'store_id'
AND TABLE_SCHEMA = SCHEMA();
SET #sql = CONCAT(#sql, ' ORDER BY table_name');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Sample output:
| TABLE_NAME |
-------------|
| table1 |
Here is SQLFiddle demo
Now you can simplify things on calling end by wrapping it up into a stored procedure
DELIMITER $$
CREATE PROCEDURE list_tables(IN _column_name VARCHAR(64), IN _column_value VARCHAR(256))
BEGIN
SET #sql = NULL;
SELECT GROUP_CONCAT(DISTINCT
CONCAT('SELECT ''', TABLE_NAME, ''' table_name
FROM ', TABLE_NAME,
' WHERE ', _column_name, ' = ''', _column_value, '''')
SEPARATOR ' UNION ALL ')
INTO #sql
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = _column_name
AND TABLE_SCHEMA = SCHEMA();
SET #sql = CONCAT(#sql, ' ORDER BY table_name');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
And use it like this
CALL list_tables('store_id', '4');
Here is SQLFiddle demo
Use IF clause in MySQL.
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
IF(store_id = 4)
COLUMN_NAME = store_id
END IF;
OR
You can use case statement.
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE CASE WHEN store_id = 4
THEN COLUMN_NAME = store_id