Last active
March 12, 2024 08:30
-
-
Save Kcko/c9b36548830a10f65d26d2ac3d3d2715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 1 | |
SET @table = "pexeso"; | |
SET @sql:= CONCAT('SELECT * FROM',' ', @table); | |
PREPARE stmt FROM @sql; | |
EXECUTE stmt; | |
-- DEALLOCATE PREPARE stmt; # neni povinne | |
-- 1b, lze dosadit parametry pres ? | |
SET @id = 1; | |
PREPARE stmt FROM 'SELECT * FROM category WHERE id = ?'; | |
EXECUTE stmt USING @id; | |
DEALLOCATE PREPARE stmt; | |
-- 2, Dynamically Building SQL based on Parameters | |
CREATE DEFINER=`root`@`localhost` | |
PROCEDURE `GetRecentActivity`(input VARCHAR(15)) | |
BEGIN | |
SET @input = input; | |
if @input="asc" then | |
SET @sort = " order by activity_log_key asc"; | |
elseif @input = "desc" then | |
SET @sort = " order by activity_log_key desc"; | |
else | |
SET @sort =""; | |
end if; | |
SET @query = CONCAT('select * from activitylog ',@sort,' limit 0, 5'); | |
PREPARE stmt FROM @query; | |
EXECUTE stmt; | |
DEALLOCATE PREPARE stmt; | |
END | |
-- 3, more than one column | |
SELECT GROUP_CONCAT(COLUMN_NAME) FROM information_schema.`COLUMNS` C | |
WHERE table_name = 'MyTb' AND COLUMN_NAME LIKE '%whatever%' INTO @COLUMNS; | |
SET @table = 'MyTb'; | |
SET @s = CONCAT('SELECT ',@columns,' FROM ', @table); | |
PREPARE stmt FROM @s; | |
EXECUTE stmt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment