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