SQL Stored procedures and triggers cannot USE the USE method _ MySQL

Source: Internet
Author: User
SQL Stored procedures and triggers cannot USE the USE method bitsCN.com

As we all know, you cannot use SQL Stored procedures during triggering.
You can use [databasename]. [user]. [tablename] to access the database. However, some statements must be executed in the current database.

For example, sp_adduser.

I saw an article earlier, and I tested it.

Original article:

Here is a solution:

Use the Exec (ute) statement to change the current database. However, note that changes to the database environment are only valid before the execution statement ends.

The following statement and Execute are put together. Example:

Create proc test

As

Exec ('use pubs') exec sp_adduser 'test'

Go

Exec test

I tried it myself and there is no problem in executing the following statement:

Use the Exec (ute) statement to change the current database.

As follows:

Create procedure cs

AS

BEGIN

Exec ('use sjdbmis; select plucode from txtplu ')

END

GO

Another example:

Create procedure cs

AS

BEGIN

Exec ('use sjdbmis; exec sp_adduser ''test ''')

END

GO

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.