MySQL select from dynamic database and table query - mysql

Stored Proc Definition:
DECLARE dbName varchar(255);
DECLARE tableName varchar(255);
DECLARE fullPath varchar(255);
DECLARE conditions varchar(255);
SET dbName = idbname;
SET tableName = itablename;
SET fullPath = CONCAT("'",dbName,"'",'.',"'",tableName,"'");
SET checkExists = 0;
I am creating a stored proc where the dbname and tablename are dynamic, however I am stuck on the select aspect of this query.
I am trying to repalce the _test.user with values passed into the stored proc.
SELECT count(*) INTO checkExists FROM `_test`.`user` WHERE id = 1;
However this line throws an error
SELECT count(*) INTO checkExists FROM fullPath WHERE id = 1;
Error:
Procedure execution failed
1146 - Table 'dbname.fullpath' doesn't exist
I have also tried CONCAT() like this
set conditions = CONCAT('SELECT count(*) INTO ',checkExists, ' FROM ', fullPath, ' WHERE id=', 1);
However I can't figure out even how to use this in a select? Help is appreciated.

I like to do these modifications using replace(). Something like this:
replace(replace('SELECT count(*) INTO checkExists FROM `<dbname>`.`<tname>` WHERE id = 1',
'<tname>', v_tablename
), '<dbname>', v_databasename
)
You may also want to use v_fullpath somewhere. I'm not really sure what query you actually want to create.
I'm not sure why you have a variable called checkExists, when it seems to be the destination file. However, I would suggest that you prepend all your local variables with something to distinguish them from column names.

Related

MySQL procedure with query string

I am a newbie to using query strings in mysql and I have tried to write this procedure to drop tables under certain conditions. I don't reall know what I'm doing wrong and need help with getting the procedure to work or for someone to point me in the right direction. Thanks.
BEGIN
DECLARE String scheduler = 'select status from mysql.scheduler where id=0' ;
DECLARE String auftragpos = 'SELECT count("SchemaName") FROM "SYS.Tables" where "SchemaName" = dwh and "Name" = lexware_fk_auftragpos';
DECLARE String auftrag = 'SELECT count("SchemaName") FROM "SYS.Tables" where "SchemaName" = dwh and "Name" = lexware_fk_auftrag';
IF(auftragpos > 1)
BEGIN
drop table "dwh.lexware_fk_auftragpos";
END
IF(auftrag > 1)
BEGIN
drop table "dwh.lexware_fk_auftrag";
END
END
The syntax for declaring a variable is
DECLARE variablename datatype;
So it should be
DECLARE auftragpos INT;
Then you need to assign the result of the query to the variable. You do that by putting the SELECT query in parentheses.
You also should not quote table names, and you need to quote the literal strings you're using for the schema and table names you're searching for.
SET auftragpos = (
SELECT count(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dwh' AND TABLE_NAME = 'lexware_fk_auftragpos');
But there's really no need to do this. If you want to prevent an error from DROP TABLE, just add the IF EXISTS option.
DROP TABLE IF EXISTS dwh.lexware_fk_auftragpos;

What's wrong with the stored procedure

I have a table called Std_Components which acts like an index for list of components with associated tables. The column AssociatedTable holds the name of table that actually contains the component data.
Please check images below -
Here is table data for Std_SteeringPumps
I am trying to create a stored procedure that will copy Std_Components table as well as all associated tables with new name. For ex. Lets say if i provided 001 as a parameter to this stored procedure i should be able create new tables like C001_Components, C001_SteeringPumps and so on.
This is what I have done so far:
ALTER PROCEDURE [dbo].[sgi_sp_CreateTablesForNewCompany]
-- Add the parameters for the stored procedure here
#CompanyId varchar(5)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- declare variables
declare #qry as varchar(2000)
declare #compTblName as varchar(100)
set #compTblName = 'C'+#companyId +'_Components'
-- Check if table already exists
IF object_id(#compTblName) is not null
return
-- Create main component index table by copying standard component table --
set #qry = 'Select * into '+#compTblName+' From Std_Components;';
--print #qry
--execute (#qry)
set #qry =#qry + 'Update C'+#companyId +'_Components Set AssociatedTable=''C'+#companyId +'''+substring(AssociatedTable,4,200);';
--print #qry
--exec #qry
-- Create all child tables --
Select * Into #TempTbl From dbo.Std_Components
Declare #Id int
While (Select Count(*) From #TempTbl) > 0
Begin
declare #rowTableName as varchar(50)
declare #compNewTbl as varchar(50)
Select Top 1 #rowTableName=AssociatedTable, #Id = Id From #TempTbl
set #compNewTbl = 'C'+#companyId + substring(#rowTableName,4,200);
set #qry = #qry + 'Select * into '+#compNewTbl+' From ' + #rowTableName + ';'
--print #qry
--exec #qry
Delete #TempTbl Where Id = #Id
End
print #qry
exec #qry
END
Here is the output of the print statement for the query it generates -
Select * into C001_Components From Std_Components;
Update C001_Components Set AssociatedTable='C001'+substring(AssociatedTable,4,200);
Select * into C001_SteeringPumps From Std_SteeringPumps;
But when the stored procedure is executed, I get the following error -
Msg 203, Level 16, State 2, Procedure sgi_sp_CreateTablesForNewCompany, Line 56
The name 'Select * into C001_Components From Std_Components;Update C001_Components Set AssociatedTable='C001'+substring(AssociatedTable,4,200);Select * into C001_SteeringPumps From Std_SteeringPumps;' is not a valid identifier.
Can anybody help me out resolve this issue.
Thanks for sharing your time and wisdom.
The error you're getting is because the EXEC statement (the last line of the stored procedure) needs to have brackets around the #qry variable so that it becomes
exec(#qry)
Without the brackets it's treating the entire SQL string as stored procedure name.
The non valid indentifier is around the AssociatedTable part
Set AssociatedTable='C001'+substring(AssociatedTable,4,200); will not run as there is no scope for AssociatedTable to substring - the string needs to contain the name of the table completely to be able to be executed
Instead of
exec #qry;
You need
exec sp_executesql #qry;
You'll also need to change the type of #qry to NVARCHAR. Note that because of the dynamic sql, the proc is prone to SQL Injection and other escaping issues (i.e. ensure that #CompanyId is validated)

How to pass list of items as parameter to a stored procedure

I have a stored procedure
create PROCEDURE [dbo].[SP]
(
#OrderList varchar(500)
)
AS
Begin
select *
from table
where id in ('+ #OrderList +')
Here I am passing orderlist....
When I execute like this
exec sp 'iss005,iss006'
I am not getting data
but when I hardcode in sp like this ...
select * from table where id in ('iss005','iss006')
then am getting data...
Thank you
Unfortunately it won't work that way. If you change your procedure to something like the following, this will work:
Create Procedure dbo.SP
#OrderList varchar(500)
AS
Declare #SQL VarChar(1000)
Select #SQL = 'SELECT * FROM table '
Select #SQL = #SQL + 'WHERE id in (' + #OrderList +')'
Exec ( #SQL)
GO
Looking more into your query, your ID's value varchar, so the procedure will fail as you'll still be getting :
WHERE id in (iss005,iss006)
when you want :
WHERE id in ('iss005','iss006')
You would need to either pass in the quote values, e.g. :
#OrderList = 'iss005','iss006'
Or work out some SQL to split the #OrderList by comma and use the QUOTENAME() function to add the quotes to the new variable.
I strongly recommend in this case the use of XML parameters, will give you a lot of flexibility.
Your XML might be something like
<ids>
<id>iss006</id>
<id>iss005</id>
</ids>
Your procedure should be something like this:
create PROCEDURE [dbo].[SP]
(
#OrderList XML
)
AS
Begin
select * from table
where id in (
select ParamValues.ID.value('.','VARCHAR(50)')
FROM #OrderList.nodes('/ids/id') as ParamValues(id)
)
Besides the use of store procedures outputs I also would recommend the use of functions but that is up to you.
Regards.
I had the same kind of requirement. i was getting list of user in a int list variable and i need to get all the order of those user. I have use a very simple trick which had solve my issue. please find the code.
public DataTable GetAllOrderData(List<int> UserID)
{
try
{
string listofuser = String.Join(",", UserID.ToArray());
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("#USERID", listofuser)
};
return SqlDBHelper.ExecuteParamerizedSelectCommand("GetOrderByUserID", System.Data.CommandType.StoredProcedure, parameters);
}
finally { UserID = null; }
}
And this is the stored procedure
CREATE PROCEDURE [dbo].[GetOrderByUserID] (#USERID varchar(700))
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare #SQL VarChar(1000)
Select #SQL = 'SELECT *,ORM.OrganisationName FROM OrderTransaction ORT LEFT JOIN OrganisationMaster ORM ON (ORT.OrganisationID=ORM.OrganisationID) '
Select #SQL = #SQL + 'WHERE ORT.CreatedBy IN (' + #USERID +')'
Exec ( #SQL)
END

SELECT INTO local variable undeclared?

I have the following in an sql file I am executing...
DECLARE rowcount INT;
SELECT COUNT(*) INTO rowcount
FROM VRG_PROBLEM_ACCOUNT PA
WHERE NEW.CustName = PA.CustNAME
AND NEW.AreaCode = PA.AreaCode
AND NEW.PhoneNumber = PA.PhoneNumber;
And yet I'm getting
ERROR 1327 (42000): Undeclared variable: rowcount
In another file I am doing the same type of SELECT...INTO localvariable and it works.
Local variables can only be declared inside of stored routines. You can use #-variables instead, which don't need to be declared at all:
SELECT COUNT(*) INTO #rowcount
FROM ...
How about re-writing your query a bit?
DECLARE rowcount INT;
SET rowcount = (
SELECT COUNT(*)
FROM VRG_PROBLEM_ACCOUNT PA
WHERE PA.CustNAME = NEW.CustName
AND PA.AreaCode = NEW.AreaCode
AND PA.PhoneNumber = NEW.PhoneNumber);
UPDATE 1*
I do not know your full code, but please keep in mind that DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

Using MySQL to run the same query on Mulitple Databases

We have 100+ databases with identical schemas on the same server. I need to run the identical select statement on all databases and return the results. I have looked thru several posts about altering many dbs and have tried this:
SELECT CONCAT
('SELECT * FROM ', a.table_schema, '.tableIwant
GROUP BY CONCAT(emp_id, "-", other_id)
HAVING count(CONCAT(emp_id, "-", other_id)) >1')
FROM information_schema.tables a
WHERE a.table_schema LIKE 'table_prefix_%'
GROUP BY a.table_schema;
But all this returns is a list of queries.
I know that I could do this using PHP - build an list of all the dbs with one query, then a WHILE loop over the result set, etc. But I want to do this from MySQL, is there a way?
UPDATE:
Thanks to #KayakJim I have tried to create a Stored Procedure to solve this:
DELIMITER //
CREATE PROCEDURE checkDuplicates()
BEGIN
DECLARE bDone INT;
DECLARE dbname VARCHAR(20);
DECLARE count1, count2 INT;
DECLARE curs CURSOR FOR SELECT table_schema FROM INFORMATION_SCHEMA.TABLES WHERE table_schema like 'table_%' GROUP BY table_schema;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
CREATE TEMPORARY TABLE IF NOT EXISTS db1.tblResults (
name VARCHAR(20),
countDupes INT,
countDupesAll INT
) ENGINE = MEMORY
;
OPEN curs;
SET bDone = 0;
REPEAT
FETCH curs INTO dbname;
SELECT SQL_CALC_FOUND_ROWS * FROM dbname.table
GROUP BY CONCAT(emp_id, "-", other_id)
HAVING count(CONCAT(emp_id, "-", other_id)) >1;
SET count1 = FOUND_ROWS();
SELECT SQL_CALC_FOUND_ROWS * FROM dbname.table
GROUP BY CONCAT(emp_id, "-", other_id, "-", param)
HAVING count(CONCAT(emp_id, "-", other_id, "-", param)) >1;
SET count2 = FOUND_ROWS();
UNTIL bDone END REPEAT;
CLOSE curs;
SELECT * FROM db1.tblResults;
END;
//
CALL checkDuplicates();
The problem now is that when I execute, it returns an error 1049: Unknown database 'information_schema'. When I take that query and run it alone I get no errors, and it returns a list of databases that match the WHERE clause.
The solution you posted using PHP could be done inside a Stored Procedure, which would keep the logic inside MySQL as you are desiring.