How to pass the CHAR as the input to the IDENTIFIED BY inside CREATE USER - sql

I am attempting to create a database user from within a procedure in MariaDB 5.5.40
I can successfully do the following:
DROP PROCEDURE IF EXISTS testUserCreate;
DELIMITER //
CREATE PROCEDURE testUserCreate
(IN sDbUser CHAR(16), IN sDbPass CHAR(40))
BEGIN
CREATE USER sDbUser#'%';
END;
//
DELIMITER ;
So the procedure has no problems with using the parameterised user name, however, when i try to do the same with the password, like so:
DROP PROCEDURE IF EXISTS testUserCreate;
DELIMITER //
CREATE PROCEDURE testUserCreate
(IN sDbUser CHAR(16), IN sDbPass CHAR(40))
BEGIN
CREATE USER sDbUser#'%' IDENTIFIED BY PASSWORD PASSWORD(sDbPass);
END;
//
DELIMITER ;
I get the error:
ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'PASSWORD(sDbPass);
Yes, i have tried to change it to IDENTIFIED BY sDbPass and other variations i could think of.
The GRANT generates the exact same error if i attempt to add IDENTIFIED BY with the variable in it.
The question is: Is it possible, and if so how, to pass the char procedure variable into the IDENTIFIED BY clause inside the CREATE USER.

Ok, i guess that for whatever reason it's impossible. If somebody else will be looking for it, you will have to manually edit the mysql.user (and maybe mysql.db) table:
DROP PROCEDURE IF EXISTS testUserCreate;
DELIMITER //
CREATE PROCEDURE testUserCreate
(IN sDbUser CHAR(16), IN sDbPass CHAR(40))
BEGIN
INSERT INTO mysql.user (Host, User, Password) VALUES('%', sDbUser, PASSWORD(sDbPass));
FLUSH PRIVILEGES;
END;
//
DELIMITER ;

Related

Can I have a scalar function and a stored procedure with the same name?

We use a stored procedure to generate an internal number (accessible via the OUTPUT keyword).
As you cannot use the stored procedure inside a statement, I thought it would be a good idea to just create a function which just calls the stored procedure with its parameters and returns the OUTPUT value. So I could directly use it for statements.
Is it possible to name a stored procedure and a scalar function the same in one database, and is this a good idea?
To simply answer the question (thanks to Martin):
No, I can't.
The error message raised is Error 2714 ("There is already an object named 'Foo' in the database.").
A complete name of any relational object in a database is a name in two parts :
the schema name, that is in fact a container inside the database
the object_name, that is an unique name relative to the schema
As a demo you can do :
CREATE SCHEMA S_SQL;
GO
CREATE USER U_SQL WITHOUT LOGIN WITH DEFAULT_SCHEMA = S_SQL;
GO
GRANT CONTROL TO U_SQL;
GO
EXECUTE AS USER = 'U_SQL';
GO
CREATE FUNCTION F_FOO()
RETURNS CHAR(3)
AS
BEGIN
RETURN 'foo';
END;
GO
REVERT;
GO
CREATE PROCEDURE F_FOO
AS
RETURN 'oof';
GO
SELECT ROUTINE_NAME, ROUTINE_TYPE
FROM INFORMATION_SCHEMA.ROUTINES;
You will have the same object name, but in two differerent schemas...

SQL Server : weird Invalid object name

I am trying to create a stored procedure and I get this error:
Msg 208, Level 16, State 6, Procedure SP_MergeStagedPoliticalPartyAgents, Line 1 [Batch Start Line 0]
Invalid object name 'SP_MergeStagedPoliticalPartyAgents'.
SQL is as follows, minus the content
CREATE OR ALTER PROCEDURE SP_MergeStagedPoliticalPartyAgents
AS
BEGIN
-- Content removed for brevity
END
If I alter the name in anyway, i.e. adding an extra s, or removing the s at the end. It works 100% fine, so my question is what is it with this particular name that I am using that is causing it to fail?
Does SQL Server have a name validation regex that this name is violating? Is it a reserved name?
Weirder addition IMO: for the sake of testing if I go:
CREATE PROCEDURE SP_MergeStagedPoliticalPartyAgents
AS
BEGIN
-- Content removed for brevity
END
It runs fine and creates the stored procedure on top of that from then onwards the CREATE OR ALTER PROCEDURE SP_MergeStagedPoliticalPartyAgents statement works fine.
It seems as if a stored procedure with this name doesn't exist it will fail on the CREATE OR ALTER PROCEDURE SP_MergeStagedPoliticalPartyAgents but pass on the CREATE SP_MergeStagedPoliticalPartyAgents if it is being initialized for the first time.
Note:
I already have other stored procedures created in the same mannerism that are fine
It is not a permissions issue as I am able to create stored procedures fine.
Running the script via VS causes the same issue so it is not related to SMSS
It also isn't the content. If I replace the content with a basic select * from table it still has the issue.
Just after I posted the issue, I found the solution.
https://bornsql.ca/blog/remember-this-if-you-want-to-use-sp_/
Create or alter
You can quite easily create a stored procedure with the prefix, both in the master database and any user databases you have, using CREATE PROCEDURE. However what Erik shows is that if you already have a stored procedure with a name that has the sp_ prefix in the master database, and then use CREATE OR ALTER syntax to create a stored procedure in a user database with the same name, you’ll get an “Invalid object name” error in the user database:*
I found that someone had created these stored procedures in the master database also.
I've got the exact same issue. SQL 2019 (15.0.4053.23)
I have run the exact same stored procedure on another database with no issues, yet on this particular database it errors. Changing the name works, removing the ALTER and only using CREATE works. if I CREATE the procedure and then use CREATE OR ALTER, it works.
CREATE OR ALTER PROCEDURE dbo.sp_bt_annl_stmt_members_select
AS
SELECT * FROM sysusers
Msg 208, Level 16, State 6, Procedure sp_bt_annl_stmt_members_select,
Line 1 [Batch Start Line 0] Invalid object name
'dbo.sp_bt_annl_stmt_members_select'.
CREATE PROCEDURE dbo.sp_bt_annl_stmt_members_select
AS
SELECT * FROM sysusers
Commands completed successfully.
Running the CREATE OR ALTER again works
CREATE OR ALTER PROCEDURE dbo.sp_bt_annl_stmt_members_select
AS
SELECT * FROM sysusers
Commands completed successfully.

What have i gotten wrong for this stored procedure not to execute?

Here is the stored procedure and how I created it
CREATE OR REPLACE STORED PROCEDURE SimpleSelect()
AS
BEGIN
SELECT * FROM Permissions
END;
/
CALL SimpleSelect();
When creating the procedure I get the error
"Success with some compilation errors"
and when running the CALL command I get that the SQL command not properly ended.
It is important to know which database do you use. Some of this advice's will come in handy in many databases but some will not...
Do you have a table called Permissions already created ? If not, create it.
Please put a ; after the SELECT statement. Like this:
SELECT * FROM Permissions;
In MySQL this will not return error:
CREATE PROCEDURE SimpleSelect()
BEGIN
SELECT * FROM Permissions;
END;
/
When you fix your procedure the call command will work just fine...
Cheers!
Here is the DEMO

DB2 stored procedure gave syntax errors

I am creating a stored procedure for db2. But it is giving an error saying that
"SQL Error [42601]: An unexpected token "END-OF-STATEMENT" was found
following "SS_TOKEN_BAK". Expected tokens may include: " END IF"..
SQLCODE=-104, SQLSTATE=42601, DRIVER=4.23.42".
Following is my stored procedure.
CREATE OR REPLACE PROCEDURE TOKEN_CLEANUP_SP
BEGIN
DECLARE batchSize INTEGER;
-- ------------------------------------------
-- CONFIGURABLE ATTRIBUTES
-- ------------------------------------------
SET batchSize = 10000; -- SET BATCH SIZE FOR AVOID TABLE LOCKS [DEFAULT : 10000]
-- ------------------------------------------------------
-- BACKUP IDN_OAUTH2_ACCESS_TOKEN TABLE
-- ------------------------------------------------------
IF EXISTS (SELECT TABLE_NAME FROM TABLES WHERE TABLE_NAME = 'IDN_OAUTH2_ACCESS_TOKEN_BAK')
THEN
DROP TABLE IDN_OAUTH2_ACCESS_TOKEN_BAK;
END IF;
END/
Is anyone face this type of issue. Any help on this would be much appreciated.
Verify that you have the end-of-statement delimiter configured correctly for whatever tool submits the 'CREATE OR REPLACE' procedure. Different tools have different ways to configure the block terminator (alternatively known end of statement delimiter). For command line scripts, use --#SET TERMINATOR / at the start of the file, other ways are possible.
Your code sample shows / as the block terminator, so you might want to use that character as the block delimiter. The semi-colon terminates statements inside the block.
Separately you should see that your code won't compile if the specified table does not exist in the implied schema at compilation time, because you are using static SQL. You may want to use dynamic SQL instead for the drop table statement (search for 'EXECUTE IMMEDIATE' examples).

Error while creating procedure in db2

I got error while creating procedure in db2.
Error is - Expected tokens may include: psm_semicolon.. SQLCODE=-104
Help Me....
CREATE PROCEDURE update_new()
LANGUAGE SQL
BEGIN
CREATE TABLE TEMP(METADATA_KEY varchar(40),NEW_METADATA_KEY varchar(40));
END;
In whatever tool you're using, change the statement terminator to something other than semicolon and put that terminator at the end of the CREATE PROCEDURE statement.
For example, if using the command line processor, save this to a file (note the "#" symbol at the end:
CREATE PROCEDURE update_new()
LANGUAGE SQL
BEGIN
CREATE TABLE TEMP(METADATA_KEY varchar(40),NEW_METADATA_KEY varchar(40));
END#
then execute the file: db2 -td# -f myproc.sql
The reason for doing this is that semicolons are always used as terminators within the procedure code, so you must use something else to terminate the CREATE PROCEDURE statement.