Home  >  Article  >  Database  >  存储过程中使用use database_MySQL

存储过程中使用use database_MySQL

WBOY
WBOYOriginal
2016-06-01 13:56:362680browse

It's well known.Microsoft SQL Server是不允许在存储过程或触发器中使用USE数据库语句来改变当前数据库的。


怎么办呢?当然可以直接用[databasename].[user].[tablename]这样来访问,但是某些语句必须在当前数据库下执行,比
如sp_adduser等。
这里提供一个解决方案:
使用Exec(ute)语句来改变当前数据库,不过要注意的是,数据库环境的更改只在Execute语句结束前有效,所以你必须把
后面的语句和Execute放到一起。Example:
create proc test
as
exec('use pubs') exec sp_adduser 'test'
go
--Written by 怡红公子@ChinaASP.com
--http://www.chinaasp.com
exec test

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn